Fetch all data by user id laravel 5.2

Report
Question

Please briefly explain why you feel this question should be reported .

Report Cancel

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 🙂

solved 0
1 Answer 12 views 0

Answer ( 1 )

    0
    December 6, 2016 at 11:03 pm

    Please briefly explain why you feel this answer should be reported .

    Report Cancel

    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('AppuserEducation');
    }
    

    Then of course the inverse for the Education model

    /** Education Model */
    
    public function user(){
        return $this->belongsTo('AppUser');
    }
    

    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;

    Best answer

Leave an answer

Browse

What is the capital of Egypt ? ( Cairo )

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>