How to get data from related tables in Laravel?

Hello. There is a table which stores user_id and project_id.
5a7285757f83a887066843.png
To obtain participants in the project I Prohect wrote
public function member(){
 return $this->hasMany('App\Model\Project\ProjectMember');
}

And then in the controller I write

$project = Project::find(some_id);
$project->member;

This returns me a list of id and how can I obtain information about the user(name name...)?
Thank you)
June 8th 19 at 17:12
3 answers
June 8th 19 at 17:14
Solution
``public function members()
{
return $this->belongsToMany(User::class, 'project_users');
}``

Then ``->member``
that is not true. You can, of course, but that's hogwash.

https://laravel.com/docs/5.5/eloquent-mutators#acc... - Gerda57 commented on June 8th 19 at 17:23
Thank you for help) And another question... I Have in the users table there is a field firstname lastname if I like something in the sample to combine them into the name field in the template not to write every time $user->firstname . "" . $user->lastname?? - Gabriel.Mitchell commented on June 8th 19 at 17:17
when a sample of the database concatenation to make, for example: User::select('id', DB::raw("CONCAT(first_name,' ', last_name) as full"))... - jett_Hilpert commented on June 8th 19 at 17:20
June 8th 19 at 17:16
Should return the list of IDs and a collection of models. Then access, for example in a loop:
@foreach ($project->members as $member) 
 {{ $member->name }}
@endforeach
June 8th 19 at 17:18
<?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

Find more questions by tags Laravel