I am a newbe about Laravel. In the View I have a form with a dropdown menu that have many participants. When the form is submitted the value of the participant (for example "7") goes to the controller, in which there is a query that should retrieve from the DB all the rows that have "7" in the column "id_participants". Unfortunately the column "id_participants" has strings in this format "3;8;65;4;34".
I tried in many ways to solve this problem, but I was only able to retrieve the rows that have a record equals to "7".
public function fetch_table(Request $request)
{
$this->validate($request, [
'participants' => 'required',
'start_date' => 'required|date',
'end_date' => 'required|date',
'rooms' => 'required',
]);
$start_date = $request['start_date'];
$end_date = $request['end_date'];
$participants = $request['participants'];
$room = $request['rooms'];
$rooms = Room::all();
$users = User::all()->where('is_active', '1')->sortBy('surname');
$meetings = Meeting::all()
->where('is_active', '1')
->where('id_room', $room)
->where('date', '<', $end_date)
->where('date', '>', $start_date)
->where('id_participants', $participants); //look at here
return view('reportArea', compact('users','rooms','meetings'));
}
So finally I should be able to do a similar query:
SELECT * FROM meetings WHERE id_participants like "%;7" or id_participants like "7;%" or id_participants like "%;7;%" or id_participants = 7.
Obviously 7 is only an example... the query should be dynamic.
Is anyone able to help me?
UPDATE:
I solved the issue after many attempts substituting the "meetings" query with the following:
$meetings = DB::table('meetings')
->where('is_active', '1')
->where('id_room', $room)
->where('date', '>', $start_date)
->where('date', '<', $end_date)
->where(function ($query) use($participants) {
$query->where('id_participants', $participants)
->orWhere('id_participants', 'like', '%;'.$participants)
->orWhere('id_participants', 'like', $participants.';%')
->orWhere('id_participants', 'like', '%;'.$participants.';%');
})
->get();
id_participantsdata format an option to you?