Shark Shark - 1 year ago 98
MySQL Question

I created 3 tables users, meeting and request how to get the users who requested a meeting?

I Created 3 tables: users, meetings and requests which has relation with both tables, for the page meeting/3 meeting with the id 3, i want to show all the users who submitted a request in request table:

User:
users
id
name

Meeting:
meeting
id
name

Requests:
requests
id
meeting_id
request_id

$requests = App\Requests::all();
foreach ($requests as $request)
{
$user = App\User::where('id', $request->user_id)->first();
echo $user->name . '<br>';
}


this shows the user but not filtered by meeting id

How to get users from meeting page where there is record in requests table?

Answer Source

Ok, correct me if I'm wrong. You want to retrieve from db all the user requests for the meeting with id = 3.

Try with:

$requests = App\Requests::where('meeting_id', 3)->get();
foreach($requests as $request){
    echo $request->user->name;
    echo '<br>';
}

and in your models you should setup some relations like:

in your user model:

public function requests(){
    return $this->hasMany('App\Request');
}

in your request model:

public function user(){
    return $this->belongsTo('App\User');
}

You can also do something like:

$meeting = App\Meeting::find(3);
$users = $meeting->users;

This will allow you, if you setup properly relations between models, to retrieve directly the users that have submitted a request for that meeting.

check laravel's docs about relations: laravel relationships

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download