Cant get proper data in Controller Laravel belongsToMany Relation - Laravel / Eloquent [closed] - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Monday, December 13, 2021

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

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.

Simply want to get applicants for each posts for each employer id.

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);
}


source https://stackoverflow.com/questions/70318191/cant-get-proper-data-in-controller-laravel-belongstomany-relation-laravel-el

No comments:

Post a Comment