0

This is my Categories Table:

id  | name
 1    a
 2    b
 3    c
 4    d

In which I need to get the values of 1 and 2 in comma separated manner, i.e. a,b.

I tried something like this:

$ids = array('0'=>1,'1'=>2);
\DB::select('SELECT group_concat(name) as name from category where id = ?', ($ids));

But it always returns the first value (i.e., a)

I need the resultant values to be like this: a,b.

How could I do this either using normal query or by Laravel?

Thank you.

Rodia
  • 1,407
  • 8
  • 22
  • 29
Suganya Rajasekar
  • 685
  • 3
  • 14
  • 37

5 Answers5

3

What you want is a kind of concatenation that can be achieved by using GROUP_CONCAT like:

SELECT GROUP_CONCAT(name) FROM category where id IN(1,2)

Reference

Explanation: here GROUP_CONCAT(name) concat name as name1, name2 ... for the WHERE condition matched

Mayank Pandeyz
  • 25,704
  • 4
  • 40
  • 59
1

Try this using simple concat. You can concat as much as columns separated by commas.

Query - select concat(id,",",name) as name from category where id = ?

Rahul
  • 763
  • 1
  • 12
  • 45
1

Try this

$data = \DB::table("articles")
   ->select("title")
   ->whereRaw("find_in_set('find value',colum_name)")
   ->get();
Amit Meena
  • 161
  • 2
  • 5
0

POST or GET data field user_id = 1,2,3;

$user_array = explode(',',Input::get('user_id')); OR $user_array = explode(',',Input::POST('user_id'));
$all_user =   User::WhereIn('user_id',$user_array)->get();

Get all user which have User ID 1,2 and 3.

Prakash Pazhanisamy
  • 997
  • 1
  • 15
  • 25
abhishek kumar
  • 448
  • 4
  • 10
0

You can use Elequent ORM

Category::pluck('id')->join(',');
Sagar Roy
  • 433
  • 4
  • 12