How to Get Comma Separated Values from a Table in Laravel

Perform query with join and comma separated values in column in Laravel

Hope It will work for you. have a try

$docomuntOrders = \DB::table("orders")
->select("orders.*",\DB::raw("GROUP_CONCAT(documents.name) as docname"))
->leftjoin("documents",\DB::raw("FIND_IN_SET(documents.id,orders.file_id)"),">",\DB::raw("'0'"))
->where('user_id',getCurrentUser()->user_id)
->groupBy("orders.id")
->paginate(10);

if you try it dd($docomuntOrders) hope it will return desired result.

Comma separated column values in a laravel join query

Try using GROUP_CONCAT and join all three tables:

$users = DB::table('transactions')
->join('lhas_transactions as lt', 'transactions.id', '=', 'lt.transaction_id')
->join('loading_hire_agreements as lha', 'lt.lha_id', '=', 'lha.id')
->select(DB::raw('transactions.id, transactions.number, GROUP_CONCAT(lha.number ORDER BY lha.id) AS agreements'))
->groupBy('transactions.id', 'transactions.number')
->get();

How to comma separate properties from a Laravel returned object

If you didn't need the "and" mechanism, then implode with comma would do the job. For "and" mechanism use below code:

<ul>
@foreach ($books as $book)
<li>{{ $book->title }} by
@for ($i = 0; $i < count($book->author); $i++)
{{ $book->author[$i]->last_name }}
{{ $book->author[$i]->first_name }}
@if ($i == count($book->author) - 2)
 and 
@endif
@if ($i < count($book->author) - 2)

@endif
@endfor
</li>
@endforeach
</ul>

added non-breaking-space   wherever necessary to not get them linked to each other

Laravel query find comma separated and normal string

First, convert your comma-separated string to an array. And create raw query.

$fac = request('fac'); // single value or comma saperated string
$data = explode(',',$fac);
$str='';
$i=1; // to append AND in query

foreach ($data as $key => $value) {
$str .= 'FIND_IN_SET("'.$value.'" ,fac)';
if($i < count($data)){
$str .=' AND '; // use OR as per use
}
$i++;
}

Now $str will contain raw query

FIND_IN_SET("1", fac) AND FIND_IN_SET("7", fac)

and you can use in eloquent.

->whereRaw($str)->get();

How to filter records when filter is a comma separated list of comma separated values in Laravel query

You can simply use REGEXP like so:

$search = preg_replace('/\s*, \s*/', '|', $request->skills_id);

YourModel::whereRaw("skills_id REGEXP '{$search}'")->get();

If the incoming values are 1,2,3,4 without any space after the commas then adjust the regex pattern to be: '/\s*,\s*/'

Laravel 5: How to verify if values separated by comma from a database table field are contained in an array?

Tried to use a function inside the where statement where I loop the array of values and just query the model using like operator to check if field value matches the values from the array and in the end I got something like this:

$devices = explode(",", Input::get("devices"));
$products = Product::where(function ($q) use ($devices) {
foreach ($devices as $device) {
$q->orWhere("compatible_devices", "like", "%" . $device . "%");
}
})->get();

Works quite well so far.



Related Topics



Leave a reply



Submit