0

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();
4
  • Is changing id_participants data format an option to you? Commented Oct 30, 2019 at 0:27
  • @elias No because in a meeting could participate many participants, not only one. I cannot imagine another data format. Commented Oct 30, 2019 at 0:40
  • 2
    Create a second table for storing participants. Use laravel relationships properly and you will avoid a lot of code. Commented Oct 30, 2019 at 0:45
  • 1
    You really should be using a many to many relationship. In general avoid adding arrays, objects etc to the database unless completely necessary, as the problem you are facing now is the reason we separate this. Check out database normalization, laravel foreign key migrations & relationships as well. Commented Oct 30, 2019 at 2:24

1 Answer 1

1

You can nest your queries: (related answer)

$meetings = Meeting::all()
    ->where('is_active', '1')
    ->where('id_room', $room)
    ->where('date', '<', $end_date)
    ->where('date', '>', $start_date)
    ->where(function($query){
        $query->where('id_participants', $participants);
        $query->orWhere('id_participants', 'like', '%;'.$participants);
        $query->orWhere('id_participants', 'like', $participants.';%');
        $query->orWhere('id_participants', 'like', '%;'.$participants.';%');
    });

However - as the comments point out - storing relations like this is very inefficient. I suggest you create a separate many-to-many table for participants. (it seems that's how they would be related)

https://laravel.com/docs/6.x/eloquent-relationships#many-to-many

Sign up to request clarification or add additional context in comments.

1 Comment

are you sure that it works? I saw this error copying and pasting your code: explode() expects parameter 2 to be string, object given

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.