Answer the question
In order to leave comments, you need to log in
How to get data from related Laravel tables?
Hello. There is a table that stores user_id and project_id.
To receive project participants, I wrote in the Prohect model
public function member(){
return $this->hasMany('App\Model\Project\ProjectMember');
}
И потом в контроллере пишу
$project = Project::find(some_id);
$project->member;
Answer the question
In order to leave comments, you need to log in
```public function members()
{
return $this->belongsToMany(User::class, 'project_users');
}```
Then ```->members```
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
public function projects()
{
return $this->belongsToMany('App\Project', 'project_user');
}
public function getFullNameAttribute()
{
return "{$this->firstname} {$this->lastname}";
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Project extends Model
{
public function members()
{
return $this->belongsToMany('App\User', 'project_user');
}
}
$id = 1;
$project = Project::find($id);
@foreach ($project->members as $member)
{{ $member->full_name }}
@endforeach
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question