Search
 
SCRIPT & CODE EXAMPLE
 

PHP

laravel order by before group by

//query below will fetch the latest entry saved in users table with a company
//in this case, there are a lot of users associated to company id = 1
//and we need to fetch only the latest user saved for this company
$latest_added_user = DB::table('company')
                ->select('company.*', 'users.*')
                ->leftJoin('users', function($query) {
                   $query->on('company.id','=','users.company_id')
                   ->whereRaw('users.id IN (select MAX(a2.id) from users as a2 join company as u2 on u2.id = a2.company_id group by u2.id)');
                })
                ->whereIn('company.id', 1)
                ->get();
Comment

PREVIOUS NEXT
Code Example
Php :: php iterate through a loop 
Php :: php replace string 
Php :: laravel function 
Php :: getimagesize php 
Php :: apache use public folder as root 
Php :: return json in php 
Php :: laravel 9 excel 
Php :: php warning: php startup: unable to load dynamic library 
Php :: woocommerce_variation_option_name on frontend 
Php :: signup api in laravel 
Php :: laravel request input default value 
Php :: php localhost 
Php :: laravel eloquent get all where in 
Php :: php docker offical apache 
Php :: laravel generate unique string 
Php :: Artisan namespace 
Php :: php ksort 
Php :: api resource create in laravel 
Php :: php get duplicate keys in array without using inbuilt function 
Php :: clear cache without using composer in laravel 8 
Php :: make controller and model laravel 
Php :: get diff array php 
Php :: php xml parser 
Php :: laravel one command for model table and controller 
Php :: license_verify 
Php :: wordpress rest_no_route custom post type 
Php :: php random number 
Php :: PHP sha1 — Calculate the sha1 hash of a string 
Php :: uft8 json php 
Php :: filament make resource 
ADD CONTENT
Topic
Content
Source link
Name
8+2 =