Laravel 8 queries Tips 2


 
$modelBanner = Banner::whereRaw("delete_flag= 0 AND visible= '0'")->get()
Output:
select * from `banners` where delete_flag= 0 AND visible= '0'
$modelBanner = Banner::where(function ($query) {
$query->where(['delete_flag' => 0, 'visible' => 0]);
})->get();
Output:
select * from `banners` where ((`delete_flag` = ? and `visible` = ?))
$modelBanner = Banner::where(function ($query) {
$query->where(['delete_flag' => 0])
->orWhere(['visible' => 0]);
})->get();
Output:
select * from `banners` where ((`delete_flag` = ?) or (`visible` = ?))
$modelBanner = Banner::where(function ($query) {
$query->where(['delete_flag' => 0, 'visible' => 0])
->where(function($sql){
$sql->where('delete_flag' , 0);
$sql->where('visible' , 0);
});
})->get();
Output:
select * from `banners` where ((`delete_flag` = ? and `visible` = ?) and (`delete_flag` = ? and `visible` = ?))

Source:
https://old-oomusou.goodjack.tw/laravel/where-and-or/
https://laravel-news.com/eloquent-tips-tricks
https://fkrihnif.medium.com/mastering-laravel-eloquent-tips-and-tricks-for-effortless-database-interactions-542e64ffbb0

Share on Google Plus

About Ram Pukar

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment

0 comments:

Post a Comment