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
Get Total Number of Members in Discord Using PHP
Laravel Ajax Response Return HTML Elements
Remove Comma on Last Item of Foreach
What Is the Regular Expression to Validate a Comma Delimited List But Ending With '&' and a Word
Group Array Data on One Column and Sum Data from Another Column
Laravel App Storage Images Failed to Load and Redirect to 404
Unset Post Variables After Form Submission
Can You Assign Multiple Variables At Once in PHP Like You Can With Java
How to Get Filename from a Variable (Url) in PHP
Display Data from Database to Dropdown Codeigniter
Convert Accented Characters to Their Plain Ascii Equivalents
How to Add Class to Link in Wp_Nav_Menu
Setting Selected Option in Laravel Form
How to Get the Values of Input Using PHP
How to Install and Use PHP on Macos Via Brew
Install PHP Intl Extension on Macos
How to Get Multiple Value from Post Variable Using Same Name