0

I have 2 model LeaveApplication and LeaveDetails model i would link to inner join leave with leaveDetails model

LeaveApplication Model

class LeaveApplication extends Model {

    public $table = 'leave_application';
    protected $fillable = ['user_id', 'start_date', 'end_date'];
    public function leaveDetails() {
        return $this->hasMany("App\LeaveDetails", 'application_id');
    }
}

This is LeaveDetails Model

class LeaveDetails extends Model {
    public $table = "leave_details";
    public $fillable = ['application_id','leave_date','leave_type'];
}

I try to access this using LeaveApplication::with('leaveDetails')->where('leaveDetails.leave_date','2016-10-10')->get()

It give me Error , This is not create inner join in eloquent.

I am also use whereHas('leaveDetails') and this not use inner join

1 Answer 1

1
LeaveApplication::with('leaveDetails')->where('leaveDetails.leave_date','2016-10-10')->get()

It won't work, because Laravel queries use few joins. If you work on eager loading, i suggest using constrained eager loading. Your Eloquent query will be looked like:

LeaveApplication::with(['leaveDetails' => function($query){
                                           $query->where('leave_date', '2016-10-10');
                                          }]);

If you're wondering what kind of query will be generated by Eloquent, try to dump it using toSql().

Sign up to request clarification or add additional context in comments.

5 Comments

it give all LeaveApplication data, it not checking data.
well, so actually you wish to retrieve LeaveApplication that has LeaveDetails that leave_date is '2016-10-10'? i thought you wish to get LeaveApplication along with it's LeaveDetails that have leave_date specified..
@MdHasiburRahaman What is it that you want to achieve exactly? What is your query aiming to accomplish? Please give a clear idea as to what you are trying to achieve?
I want to do this Query SELECT * FROM employee_leave_application INNER JOIN leave_details ON leave_details.application_id = employee_leave_application.id WHERE leave_details.leave_date = "2016-10-10" using Eloquent .
so, what information do you intend to get..? cause my query above will do just like your query but with two selects instead. one will be SELECT * FROM employee_leave_application followed by SELECT * FROM leave_details WHERE leave_date = "2016-10-10" and relation mappings done in memory by Laravel. So, you could get your leave details by something like $leaveApplications->first()->leaveDetails->first()->leave_date -- its a nested Collection.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.