how to show only last record with query builder in laravel - postgresql

I only want to display the latest data from the tanggal_ins that I have marked, how to?
This is my json data:
And this is my query:
$cuti = DB::table('t_cuti as cuti')
->join('m_karyawan as kary', 'kary.nik', '=', 'cuti.nik')
->join('m_rumah_sakit as rs', 'rs.kd_rs', '=', 'kary.kd_rs')
->join('m_unit as unit', 'unit.kd_unit', '=', 'kary.kd_unit')
->join('m_jabatan as jab', 'jab.kd_jabatan', '=', 'kary.kd_jabatan')
->leftJoin('t_cuti_adjust as adj', function ($leftJoin) {
$leftJoin->on('adj.nik', '=', 'cuti.nik')
->latest();
})
->select('adj.tanggal_ins','cuti.saldo_cuti','kary.nik','kary.nm_karyawan','rs.nm_rs','unit.nm_unit','jab.nm_jabatan')
->whereYear('adj.tanggal_ins', '<', $year)
->orWhere('adj.tanggal_ins', null)
->get();
Thanks.

Use orderBy.
->orderBy('id', 'desc')->first();
This will make descending order by ID.
->orderBy('id', 'desc')
and use first() instead of get().

Related

How to implement a join in a join with Eloquent

I have a question how to combine two joins in Laravel's eloquent. One join also belongs to the other.
The concept is you have timeslots and I want to know how many attendees there for that timeslot.
The challenge is: I want all available timeslots and a booking is only valid when it's done live, not canceled and completed.
With the query below the booking and counting part is fine, but I only get the timeslots with bookings done.
$this->selectRaw('timeslots.id, timeslots.time_from , timeslots.time_till, duration_minutes,
count(attendees.id) as bookedSlots,
capacity as total,
capacity - count(attendees.id) as leftTickets')
->where('timeslots.project_id', $projectId)
->where('invoices.type', '!=', 'IncompleteOrder')
->whereDate('time_from', '=', $date)
->leftJoin('attendees', function($query) {
$query->on('timeslots.id', '=', 'attendees.timeslot_id')
->where('attendees.is_live', '=', 1)
->where('attendees.is_cancelled', '=', 0);
})
->leftJoin('invoices', 'attendees.invoice_id', '=', 'invoices.id')
->groupBy('timeslots.id')
->get();
This was a/the solution:
$response = $this->selectRaw('timeslots.id, timeslots.time_from , timeslots.time_till, duration_minutes,
count(attendees.id) as bookedSlots,
capacity as total,
capacity - count(attendees.id) as leftTickets')
->where('timeslots.project_id', $projectId)
->whereDate('time_from', '=', $date)
->leftJoin('attendees', function($query) {
$query->on('timeslots.id', '=', 'attendees.timeslot_id')
->where('attendees.is_live', '=', 1)
->where('attendees.is_cancelled', '=', 0);
})
->leftJoin('invoices', function($query) {
$query->on('invoices.id', '=', 'attendees.invoice_id')
->where('invoices.type', '!=', 'IncompleteOrder');
})
->groupBy('timeslots.id')
->get();

Eloquent query, combine where and whereBetween

Is there any wrong with this eloquent query? I want to use a where and wherebetween. I dont get any items in return.
$start_date = "2021-02-1-01";
$end_date = "2021-02-28";
$date_array = array($start_date, $end_date);
$data = array(
'project_id' => $project_id,
);
return WasteDataModel::where($data)
->where(function ($query) use($date_array) {
return $query->whereBetween('date', $date_array);
})->get();
I have tried this aswell
return WasteDataModel::where($data)->whereBetween('date', $date_array)->get();
with no return
Use DATE method of MYSQL with DB::raw() to compare dates only
return WasteDataModel::where($data)
->whereBetween(DB::raw('DATE(date)'), $date_array)
->get();

Laravel nestedwhere Parse error: syntax error, unexpected '}'

Symfony\Component\Debug\Exception\FatalThrowableError: Parse error: syntax error, unexpected '}'
I followed this resources for nested where here: How to combine WHERE clauses in Eloquent but get the following error while running the codes. The line where it broke was the curly bracket of the callback function, what's wrong here?
$providers = DB::table('users')
->where('verified', '=', 1)
->where('status', '=', 1)
->where(function ($query) use ($search) {
$query->where(DB::raw('lower(name)'), 'LIKE', "%".strtolower($search)."%")
->orWhere(DB::raw('lower(username)'), 'LIKE', "%".strtolower($search)."%")
})
->where('city', '=', $t)
->take($limit)
->toSql();
You are missing a ;after your inner $query. I think that part could be this:
->where(function ($query) use ($search) {
$query->where(DB::raw('lower(name)'), 'LIKE', "%".strtolower($search)."%")
->orWhere(DB::raw('lower(username)'), 'LIKE', "%".strtolower($search)."%");
})
The code needs that semicolon at the end, because it is a line / statement in a function. It does not matter that this function is a callback.

Eloquent Left Join with more than one condition

I need to mount a left join with more than one condition, but I have not found the correct settings
LEFT JOIN palpite ON partida.partida_id = palpite.partida_fk AND palpite.usuario_fk = 20
eloquent code
$results = DB::table('partida')
->join('time as mandante', 'mandante.time_id', '=', 'partida.mandante_fk')
->join('time as visitante', 'visitante.time_id', '=', 'partida.visitante_fk')
->leftJoin('palpite', function ($join) {
$join->on('partida.partida_id', '=', 'palpite.partida_fk')
->on('partida.usuario_fk', '=', '20');
})
->select([
'partida.partida_id',
'partida.rodada',
'partida_data',
'partida.local',
'partida.mandante_fk',
'partida.visitante_fk',
'mandante.abreviacao AS mandante_abreviacao',
'mandante.nome AS mandante_nome',
'mandante.escudo60x60 AS mandante_escudo',
'visitante.abreviacao AS visitante_abreviacao',
'visitante.nome AS visitante_nome',
'visitante.escudo60x60 AS visitante_escudo',
'palpite.placar_mandante',
'palpite.placar_visitante'
])
->where('mandante.aposta', 1)
->orWhere('visitante.aposta', 1)
->orderBy('partida.partida_data', 'ASC')
->get();
Try this:
$results = DB::table('partida')
->join('time as mandante', 'mandante.time_id', '=', 'partida.mandante_fk')
->join('time as visitante', 'visitante.time_id', '=', 'partida.visitante_fk')
->leftJoin('palpite', function ($join) {
$join->on('partida.partida_id', '=', 'palpite.partida_fk');
$join->on('partida.usuario_fk', '=', '20');
})
->select([
'partida.partida_id',
'partida.rodada',
'partida_data',
'partida.local',
'partida.mandante_fk',
'partida.visitante_fk',
'mandante.abreviacao AS mandante_abreviacao',
'mandante.nome AS mandante_nome',
'mandante.escudo60x60 AS mandante_escudo',
'visitante.abreviacao AS visitante_abreviacao',
'visitante.nome AS visitante_nome',
'visitante.escudo60x60 AS visitante_escudo',
'palpite.placar_mandante',
'palpite.placar_visitante'
])
->where('mandante.aposta', 1)
->orWhere('visitante.aposta', 1)
->orderBy('partida.partida_data', 'ASC')
->get();

Fix Mongo query in Lumen

I have a query in the lumen. But it does not work. The query is :
return Order::whereBetween('source_longitude', [$minLon_try_one, $maxLon_try_one])
->whereBetween('source_latitude',[51.365807806703,51.454384193297])
->where('status','=','pending')
->where('created_at','<=', 2016-04-07 12:00:35)
->where('created_at','>=', 2016-04-07 11:55:35)
->orWhere(function($query)
{
$query->whereBetween('source_longitude', [51.321519613407, 51.498672386593])
->whereBetween('source_latitude',[35.612195271526,35.756086728473])
->where('status','=','pending')
->where('created_at','<=',2016-04-07 11:55:35)
->where('created_at','>=',2016-04-07 11:50:35);
}
)->get();
But when I remove orWhere function from the query I get expected result
You probably using orWhere a little bit wrong. You need to put where to another where to execute query properly. What You are doing now is something like that where a is 1 and b is 2 or (c is 3 and d is 4), but I believe, You want to do something like that where (a is 1 and b is 2) or (c is 3 and d is 4)
Try this one:
return Order::where(function ($query) {
$query->whereBetween('source_longitude', [$minLon_try_one, $maxLon_try_one])
->whereBetween('source_latitude', [51.365807806703, 51.454384193297])
->where('status', '=', 'pending')
->where('created_at', '<=', '2016-04-07 12:00:35')
->where('created_at', '>=', '2016-04-07 11:55:35');
})->orWhere(function ($query) {
$query->whereBetween('source_longitude', [51.321519613407, 51.498672386593])
->whereBetween('source_latitude', [35.612195271526, 35.756086728473])
->where('status', '=', 'pending')
->where('created_at', '<=', '2016-04-07 11:55:35')
->where('created_at', '>=', '2016-04-07 11:50:35');
})->get();