简体   繁体   中英

Fetch all data by user id laravel 5.2

I am new in laravel app development. I have a userEducation table. where I store my user's educational background. There can be multiple rows for a single user. Everything is OK. But when I am fetching data from userEducation table for a single user, It returns all data. How can I get single user's all data from userEducation table.

Below this is my controller class

class userController extends Controller{
public function index(){
}

public function show($id){
}

public function edit(){
    $educations=userEducation::all();
    return view('auth.user_edit',['educations'=>$educations]);
}
}

Anyone help me plesae :)

You should create a hasMany relationship in your User model and a belongsTo relationship in your userEducation model.

/** User Model*/
public function educations(){
    return $this->hasMany('App\userEducation');
}

Then of course the inverse for the Education model

/** Education Model */

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

Then you can do $educations = Auth::user()->educations to get the educations that belong to the current user session. If you need it for a specific user, then do $educations = User::find($id)->educations;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM