samedi 11 décembre 2021

Cant get proper data in Controller Laravel belongsToMany Relation - Laravel / Eloquent

A user(employer) have many posts and many user(employee) can apply these posts . Point is employer should get which users applied for each its posts.

I tried $employees = Post::find(//anyNumber//)->people; it gives proper applicants infos but it should be dynamic for each employer user .

Tables..

applies   ->  | users_id(employee) | posts_id |
posts     ->  | id                 | user_id(employer)  | (other posts cols ... )
user_info ->  | id                 | (name col  etc... for employee)

Post Model..

public function people()
{
   return $this->belongsToMany(Info::class , 'applies', 'user_id' , 'posts_id');
}

Controller..

public function index()
{
    $user_id = Auth::id();
    $myPosts = Post::where('user_id',$user_id)->get();
    $employees = Post::find(//anyNumber//)->people; // this line

    dd($employees);
}


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire