Two table relation

  • STEP

    1. create Student & Comment model

    2. add 'get_comment()' function in Student model and set relation to Comment model

    2. add 'get_student()' function in Comment model and set relation to Student model

    in Models/Student.php

    
    
    namespace App\Models;
    
    use Illuminate\Database\Eloquent\Model;
    
    class Student extends Model
    {
        
            public function get_comment()
            {
                return $this->hasMany('App\Models\Comment');
            }
    
    }
    

    in Models/Comment.php

    
    
    namespace App\Models;
    
    use Illuminate\Database\Eloquent\Model;
    
    class Comment extends Model
    {
            public function get_student()
            {
                // return $this->belongsTo('Model', 'foreign_key', 'owner_key'); 
                return $this->belongsTo('App\Models\Student','student_id','id');
            }
    
    }
    

    Get data in the controller

    
    $student = Student::find(1);
    dd($student->get_comment);
    

    Fetching Students who have atleast 1 comment:

    
    $student = Student::has('get_comment')->get();
            dd($student);
    

    Fetching Students who doesn't have comments:

    
    $student = Student::doesntHave('get_comment')->get();
            dd($student);
    

    Fetching Students who have atleast 2 comments:

    
    $student = Student::has('get_comment','>=',2)->get();
            dd($student);
    

    Fetching a Student with all his/her comments in descending order:

    
    $student = Student::where('id',1)->with(['get_comment' => function($query) {
                $query->orderBy('id','desc');
            }])->get();
            dd($student);
        

    Accessing only specific columns in a comment

    Change the comment Models/Student to

    
        public function get_comment()
        {
            return $this->hasMany('App\Models\Comment','student_id')->orderBy('id','desc');
        }
    

    then the code to get only specific column on a relation

    
    $student = Student::where('id',1)->with(['get_comment:id,student_id,comment'])->get();
    
    foreach ($student as $comment){ 
        dd($comment->get_comment);
    }
    

    Get a student with a specific condition in the comments table:

    
    $student = Student::whereHas('get_comment',function($query){
                $query->where('comment','like','%king%');
            })->get();
    dd($student);
        

    Get count of comments for each student

    
        $student = Student::withCount('get_comment')->get();
    dd($student);
        

    Get count of comments for each student order by comment_count desc

    
        $student = Student::withCount('get_comment')->orderBy('comment_count','desc')->get();
    dd($student);
        

    Get count of comments for each student with a specific critera

    
        $student = Student::withCount(['get_comment' => function($query) {
                $query->where('comment','like','%king%');
            }])->get();
    dd($student);