Convert Query Builder to Eloquent Builder - laravel

I can not extract the result in groupby, the query builder below works very well but I want it to Eloquent.
I want to display only the folders that are already in the relationship?
how to convert a query builder to Eloquent?
$folders = Folder::select('folders.id','folders.title')
->join('matters', 'matters.folder_id', '=', 'folders.id')
->join('tutorials', 'tutorials.matter_id', '=', 'matters.id')
->whereDate('tutorials.start', '=', date('Y-m-d'))
->where('tutorials.status','=','0')
->groupby('folders.id')
->get();

It seems Floder hasMany Matter, Matter hasMany Tutorial.
You can create the hasManyThrough relationship in Folder Model, and use whereHas to find the folders
$folders = Folder::whereHas('tutorials', function($query) {
$query->whereDate('tutorials.start', '=', date('Y-m-d'))
->where('tutorials.status','=','0');
})->select('title', 'id')->get();
if you don't have hasManyThrough relationship, you can still get folders like this one:
$folders = Folder::whereHas('maters.tutorials', function($query) {
$query->whereDate('tutorials.start', '=', date('Y-m-d'))
->where('tutorials.status','=','0');
})->select('title', 'id')->get();

Related

Laravel Query Builder in Model

I would like to know if is it possible to work in a model using query builder to make a join between 4 tables, I don't know how to use eloquent with 4 tables
this is in controller
$kurikulum = DB::table('siakad.tb_01')
->select('siakad.tb_01.*', 'siakad.krs_jadwal_aktiv.*', 'siakad.mata_kuliah.*', 'siakad.kurikulum_item.*', )
->join('siakad.krs_jadwal_aktiv', 'siakad.tb_01.staf_id', '=', 'siakad.krs_jadwal_aktiv.kdds')
->join('siakad.mata_kuliah', 'siakad.krs_jadwal_aktiv.kdmk', '=', 'siakad.mata_kuliah.kode')
->join('siakad.kurikulum_item', 'siakad.mata_kuliah.id', '=', 'siakad.kurikulum_item.mata_kuliah_id')
->get();
Please try this:
$kurikulum = DB::table('tb_01')
->select('tb_01.*', 'krs_jadwal_aktiv.*', 'mata_kuliah.*', 'kurikulum_item.*')
->join('krs_jadwal_aktiv', 'tb_01.staf_id','krs_jadwal_aktiv.kdds')
->join('mata_kuliah', 'krs_jadwal_aktiv.kdmk','mata_kuliah.kode')
->join('kurikulum_item', 'mata_kuliah.id','kurikulum_item.mata_kuliah_id')
->get();

Laravel eloquent relationships Multiple table

When I try to connect my table using a query. But I need the building relationship using Laravel eloquent.
This is my query
{
$students = Student::with('FeeScheem')
->leftJoin('fee_scheems', 'fee_scheems.fs_id', '=', 'students.stu_fee_scheem_id')
->leftJoin('individually_fee_scheems', 'individually_fee_scheems.id', '=', 'students.stu_ind_fee_scheem_id')
->leftJoin('fee_masters', 'fee_masters.fee_master_ind_fee_scheem_id', '=', 'individually_fee_scheems.id')
->leftJoin('fee_groups', 'fee_groups.fg_id', '=', 'fee_masters.fee_master_fg_id')
->leftJoin('school_fee_collectors', 'school_fee_collectors.fc_fg_id', '=', 'fee_groups.fg_id')
->where('students.stu_id', $students)
->get();
$this->students = $students;
}

Laravel Eloquent: how to get related records via whereHas() method?

For example, I can check every post with concrete date.
$users = User::whereHas('posts', function($q){
$q->where('created_at', '>=', '2015-01-01 00:00:00');
})->get();
But suppose, what to do, if I want to compare a post model date (created_at) with the date attribute of user model?
For example:
$users = User::whereHas('posts', function($q){
$q->where('created_at', '>=', ** $user->customDate ** ← look this);
})->get();
upd.
I use Eloquent outside of Laravel.
You can use a raw expression on the Eloquent sub query, by using $q->whereRaw.
In your example:
$users = User::whereHas('posts', function($q){
$q->whereRaw("created_at >= users.customDate");
})->get();
Unlike DB::raw, this can be used without Laravel's dependency injection of Illuminate\Database facade.
Assuming that your users table is just called users you can use DB::raw() to reference a value like you would in a normal query:
$users = User::whereHas('posts', function($q){
$q->where('created_at', '>=', DB::raw('users.customDate'));
})->get();
Don't forget to either import the DB facade or just change it to \DB::raw(...).
If you're using this outside of Laravel and don't have facades you can cut out the middleman and do:
$q->where('created_at', '>=', new Illuminate\Database\Query\Expression(('users.customDate'));
Hope this helps!

How to select instances for specific 'n' on m:n relationships?

I have two models Teacher and Category. These two have Many to Many relationship.
I want to get those teachers who have one category equal to "OLevels". Which method of eloquent is used for it or is there any other way I can get it?
Is there anyway to get it as:
$teachers = Teacher::where('category', '=', 'OLevels')->get();
You can use whereHas for that:
$category = 'OLevels';
$teachers = Teacher::whereHas('category', function($q) use ($category){
$q->where('name', $category);
})->get();
You could make use of eager loading with constraints
$teachers = Teacher::with(['category' => function($query)
{
$query->where('category', '=', 'OLevels');
}])->get();
Further info in the documentation.

How to select instances with multiple relations?

I have some models Featured_Course_Request, Course_Request, Response and Teacher. Featured_Course_Request hasOne Course_Request and Course_Request hasMany Response by Teacher.
I want to get the only Featured_Course_Requests on which logged in teacher has not responded (have no Response by logged in teacher.) How can I do it?
I am trying to achieve it with the following code but it is not giving correct output.
$featured_course_request = Featured_Course_Resquest::whereRaw('remaining_coins >= coins_per_click')->where('status', '=', 'open')
->whereHas('courseRequest', function($q) use ($teacher){
$q->whereHas('responses', function($qe) use ($teacher){
$qe->where('teacherID', '!=', $teacher->id);
});
});
You can target nested relations with the dot syntax: 'courseRequest.responses' further more you'll need whereDoesntHave instead of whereHas:
$featured_course_request = Featured_Course_Resquest::whereRaw('remaining_coins >= coins_per_click')
->where('status', '=', 'open')
->whereDoesntHave('courseRequest.responses', function($q) use ($teacher){
$qe->where('teacherID', '=', $teacher->id);
})
->get();

Resources