select last visit date in Laravel (Mysql) - php

I'm trying to select last visit date in php (Laravel). First I order by descending and then select the maximum date. Can any body help me?
Here is my unsuccessful code:
$getlast = DB::table('tr_visit')
->join('tm_child','tm_child.Child_ID','=','tr_visit.Child_ID')
->where('tm_child.Child_Name', 'LIKE', '%'.$input.'%')
->where('tm_child.Child_ID','=','CH001')
->orderBy('tr_visit.Visit_Date', 'desc')
->select(DB::raw('max(tr_visit.Visit_Date),Visit_Date'))
->get();

Try this
$getlast = DB::table('tr_visit')
->join('tm_child','tm_child.Child_ID','=','tr_visit.Child_ID')
->where('tm_child.Child_Name', 'LIKE', '%'.$input.'%')
->where('tm_child.Child_ID','=','CH001')
->orderBy('tr_visit.Visit_Date', 'desc')
->select(DB::raw('max(tr_visit.Visit_Date),Visit_Date'))
->first();

If you are already ordering by the date descending, there is no need to select the maximum. The first row will be the maximum date.
->orderBy('tr_visit.Visit_Date','desc')
->first();
Should be all you need.

If you want to order by date in descending order,then you have to use DATE like this
->orderBy("DATE('tr_visit.Visit_Date')",'desc')
->first();

Related

I want to find duplicate data from the database and show sum of duplicate data in laravel 8

I want to get all the data from SALES and PURCHASES table but I want to show product name for the single time and also want to show the sum of its quanitity..
How can i do this in laravel 8 ?
What will be the query ??
$stock = DB::table('products')
->join('purchases','purchases.product_id','products.id')
->join('sales','sales.product_id','products.id')
->groupBy('product_id')
->get();
I dont have your whole query but use havingRaw to find the duplicates.
$dups = DB::table('tableName')
->select('col1_name','colX_name', DB::raw('COUNT(*) as `count`'))
->join('purchases','purchases.product_id','products.id')
->join('sales','sales.product_id','products.id')
->groupBy('col1_name', 'ColX_name')
->havingRaw('COUNT(*) > 1')
->get();
Maybe something like that
$stock = DB::table('products')
->select('sales.*','purchases.*','products.name', DB::raw('products.name as `NbProduct`'))
->join('purchases','purchases.product_id','products.id')
->join('sales','sales.product_id','products.id')
->groupBy('sales.id', purchases.id)
->get();

How to use the Larave DB Facade to get the first record with a date close to a particular date

I am trying to get the first transaction that was recorded close to a particular date using the Laravel 8 DB Facade.
I have tried to use it in the following manner
$lastest_transaction = DB::table($targetTable)
->where('date', '<=', date('F'))
->where('id', '=', $client->id)
->where('branch', '=', session('branch'))
->get();
but it's not giving me the correct transaction.
In order to get the record closest to the date, you can sort by the date in descending order, which would make the first record the "largest" date that's less than or equal to what you are searching for:
$lastest_transaction = DB::table($targetTable)
->where('date', '<=', date('F'))
->where('id', '=', $client->id)
->where('branch', '=', session('branch'))
->orderBy('date', 'DESC') // Order by the date in descending order
->first(); // Get the first record

Ordering records using dates in Laravel eloquent

I have a column move_out that stores date in this format - m/Y e.g 02/2020. I want to order the records in that table by the move_out column in descending order but my solution don't seem to be working. It does not arrange the records appropriately. This is what I am doing.
$data = User::orderBy('move_out', 'DESC')->get();
How do I solve this?
The datatype for the move_out column is string.
try STR_TO_DATE
$data = User::orderBy(DB::raw("STR_TO_DATE(CONCAT('01-', move_out),'%d-%m/%Y')"), 'DESC')->get();
Try to use DATE_FORMAT:
$items = DB::table("users")
->orderBy(DB::raw("DATE_FORMAT(move_out,'%M/%Y')"), 'DESC')
->get();

Laravel: select students who do not have payment made in the month

I want to make a list of students who did not make payment in the current month, or never made a payment.
it is possible to do with a single query builder, or eloquent function?
With the code below I can do exactly the opposite of what I want :/
$indebted = DB::table('students')->where('students.active',1)
->leftJoin('payments', 'students.id', '=', 'payments.user_id')
->whereMonth('payments.created_at','=', $today->month)
->get();
I don't know what value you are passing here.
$today->month
Instead of this $today->month you have to pass exact month here
Please try this
$indebted = DB::table('students')->where('students.active',1)
->leftJoin('payments', 'students.id', '=', 'payments.user_id')
->whereMonth('payments.created_at','=', 06)
->get();
Here is the students ids who made payments in Current month and year
$payments= DB::table('payments')
->whereMonth('created_at',date('m'))
->whereYear('created_at', date('Y'))
->pluck('user_id')->all();
use whereNotIn('id',[students ids who made payments])
$students = DB::table('students')
->whereNotIn('id',$payments)
->get();
You can use Raw SQL in this kind of situation
I am assuming you are using mysql database
$sql=SELECT * from students s LEFT JOIN payments p ON s.id=p.user_id
WHERE s.active=1 AND (p.created_at is NULL or MONTH(MAX(p.created_at) < MONTH(CURRENT_DATE())) )
$students = DB::connection('mysql')->select( DB::raw(" $sql"));
foreach($students as $aStudent){
...
....
}
you can get student who do not have payments made in this month
$students = App\student::whereDoesntHave('payments', function (Builder $query) {
$query->whereMonth('payments.created_at',$today->month);
})->where('active',1)->get();

(laravel) Two ORDER BY clauses and a LIMIT

This is some sorting:
I want to list 20 best posts based on "views".
then, sort it based on "created_at".
How to do it?
Heres my current code (which work, but doesnt sort by "created_at"):
$this->data['today_post'] =
Posts::orderBy('views', 'desc')
->orderBy('created_at', 'desc')
->whereRaw('created_at >= NOW() - INTERVAL 1 DAY')
->limit(20)->get();
You can write this. Hopefully this will solve your problem
$this->data['today_post'] =
Posts::orderBy('views', 'desc')
->orderBy('created_at', 'desc')
->whereDate('created_at', Carbon::today()->toDateString())
->limit(20)
->get();
Also add use Carbon\Carbon; at the beginning of your controller.
Get a collection from database first
$x = Posts::whereRaw('created_at >= NOW() - INTERVAL 1 DAY')->orderBy('views', 'desc')->limit(20)->get();
Then sort the collection
Descending:
$this->data['today_post'] = $x->sortByDesc('created_at');
Ascending:
$this->data['today_post'] = $x->sortBy('created_at');
You can simply sort the result collection. Keeping your code, add the following:
$this->data['today_post'] = $this->data['today_post']->sortBy('created_at');

Categories