I have code from query builder I want to convert to eloquent but it doesn't work
my query builder code
$topkabupatens = DB::table('sekolahs as S')
->select('K.name', DB::raw('count(S.kabupaten) as jumlah_kabupaten'))
->join('kabupaten_kotas as K', 'S.kabupaten', '=', 'K.id')>orderBy('jumlah_kabupaten', 'DESC')->groupBy('K.name')->limit(10)->get();
my query builder code
$topkabupatens = Sekolah::with('kabupaten')->withCount('kabupaten')>orderBy('kabupaten_count', 'DESC')->groupBy('name')->limit(10)->get();
where is my mistake?
my query builder code runs but not with eloquent
try with
$topkabupatens = Sekolah::select('kabupaten_kotas.name')->withCount('kabupaten_kotas.kabupaten')
->leftjoin('kabupaten_kotas', 'Sekolah.kabupaten', '=', 'kabupaten_kotas.id')
->orderBy('kabupaten_count', 'DESC')
->groupBy('name')
->limit(10)
->get();
Related
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();
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;
}
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();
I have the query:
$estadolocacaoname = "naolocado";
$consulta = DB::select("SELECT locacao.estadolocacao, automovel.marca, automovel.modelo
FROM locacao
INNER JOIN automovel
ON locacao.id = automovel.id
where locacao.estadolocacao = ? ",[$estadolocacaoname]);
How make paginate of LARAVEL?
Laravel's pagination is a breeze when you use the query builder:
DB::table('locacao')
->join('automovel', 'locacao.id', '=', 'automovel.id')
->select('locacao.estadolocacao', 'automovel.marca', 'automovel.modelo')
->where('locacao.estadolocacao', '=', $estadolocacaoname)
->paginate(15);
SELECT itemid,COUNT(itemid),itemname FROM `items`
WHERE DATE(`save_at`) ='2015-12-16'
GROUP BY itemid
Anyone know how to write this sql in laravel 5 using query builder ???
this is the code i used but it didn't work for me
$query = DB::table('items')
->select(itemid, DB::raw('COUNT(itemid) as noitems')),itemname)
->where( DATE(`save_at`),'=', 2015-12-16)
->group_by('itemid')
->get();
Check the below code.
$query = DB::table('items')
->select(itemid, DB::raw('COUNT(itemid) as noitems')),itemname)
->where( DB::raw('DATE(`save_at`)'),'=', '2015-12-16')
->groupBy('itemid')
->get();