I want to fetch data from database which has booking year is 2022, and the column I have is tanggal_take whose value format is 'Y-m-d', so I want to use SELECT * FROM table_name WHERE YEAR(tanggal_take) = "2022"; How to write it in laravel controller using Eloquent?
here is my code now
$data = Booking::join('users', 'users.id', '=', 'bookings.id_member')
->join('packages', 'packages.kode_paket', '=', 'bookings.kode_paket')
->where( DB::raw('YEAR(bookings.tanggal_take)'), '=', $request->range )
->get(['bookings.*', 'packages.*', 'users.*']);
The result is 0 but in database i have 4 datas with year 2022. I also already change ->where( DB::raw('YEAR(bookings.tanggal_take)'), '=', $request->range ) to ->whereYear('bookings.tanggal_take', '=', $request->range ) but still 0 result. Any hint? I will also looking for documentation by my self rn. Thanks
Related
I have following temp tables I wanna join them In query builder but I am failing to do so. I wanna join these 2 tables so I can do i1.imp/i2.imp
$subQuery1 = MyModel::query()
->from("table as i1")
->select(
\DB::raw('sum(col) as col1'),
\DB::raw('co1')
)->where('stamp', '>=', '2022-03-01 14:25:00')
->where('stamp', '<', '2022-03-07 14:30:00')
->groupBy(
'co1'
);
$subQuery2 = MyModel::query()
->from("table as i2")
->select(
\DB::raw('sum(col) as col1'),
\DB::raw('co1')
)->where('stamp', '>=', '2022-03-01 14:20:00')
->where('stamp', '<', '2022-03-07 14:25:00')
->groupBy(
'co1'
);
You can use from, fromSub or table and pass in a subquery instead of a table.
You can do the same with joinSub for joins.
You need to provide an alias though.
For example, to use $subquery1 as the main table and join it with $subquery2, the resulting query could look like this:
$results = DB::query()
->select(.....)
->fromSub($subquery1, 'i1')
->joinSub($subquery2, 'i2', function ($join) {
$join->on('i1.col', '=', 'i2.col');
// ->orOn(....)
});
->where(....)
->get();
Laravel 9.x API - fromSub
Queries - Subquery Joins
I tried this in laravel, but it's not working. I can't get it to work it's working when I use it straight on PHPMyAdmin
SELECT service,
count(*) as total_count,
sum(if(status ='Successful',1,0)) as successful_count,
sum(if(status ='Failed',1,0)) as failed_count
FROM `wallet_ledgers`
WHERE operator = "-"
AND user_id = 5
group by service
this is the desired output The way the table should look
You can use DB::raw
Link: Laravel-raw-expressions
$result = DB::table('wallet_ledgers')
->select(DB::raw("service, count(*) as total_count, sum(if(status ='Successful',1,0)) as successful_count, sum(if(status ='Failed',1,0)) as failed_count"))
->where([
[ 'operator', '=', '-'],
['user_id', '=', '5'],
])
->groupBy('service')
->get();
How to convert this query in Laravel eloquent way?
SELECT * FROM posts WHERE status=1 and now() BETWEEN start_time and end_time ORDER BY id DESC LIMIT 1
// What so far i have tried
Post::whereStatus(1)->orderBy('id','desc')->get()
"and now() BETWEEN start_time and end_time" .I am unable to convert
this part in eloquent way.
Any suggestion would be highly appreciated.
You can do the following
Post::where('status', 1)
->where('start_time', '>', Carbon::now())
->where('end_time', '<', Carbon::now())
->orderBy('id', 'DESC')
->take(1)
->get();
You can achieve the same semantic by defining that your start_time is lower than the current datetime and your end_time is greater than it:
Post::whereStatus(1)
->where("start_time", "<", Carbon::now())
->where("end_time", ">", Carbon::now())
->orderBy('id','desc')
->first();
I use laravel eloquent outside laravel.
I have a query that is supposed to get only posts that are featured (featured field = 1) and of any of the 3 types (blog, forum and page).
$latestFeaturedPosts = $db->table( 'posts' )
->where( 'featured', '=', 1 )
->orWhere( 'post_type', '=', 'blog' )
->orWhere( 'post_type', '=', 'forum' )
->orWhere( 'post_type', '=', 'page' )
->limit( 15 )
->orderBy( 'created_at', 'desc' )
->get()->toArray();
I expected this query to return what I wanted, but it does return also posts where featuredcolumn is not 1.
Why ? How should I modify this syntax to enforce this ?
I do not now this language, but will give you a starting point to look into.
Your query will be expanded to:
SELECT * FROM posts where featured = 1 OR post_type = 'blog' OR post_type = 'forum' OR post_type = 'page' LIMIT 15 ORDER BY created_at DESC;
In this query, any row that matches any one of the 4 criteria will be returned.
For you to get the results you expect, your query needs to be evaluated to:
SELECT * FROM posts where featured = 1 AND ( post_type = 'blog' OR post_type = 'forum' OR post_type = 'page' ) LIMIT 15 ORDER BY created_at DESC;
In this example, we will always enforce the featured type, and then can select any 3 types that are also featured.
How to do this in your language, I am not sure.
This will work for you:
$db->table('posts')
->where('featured', 1)
->whereIn('post_type', ['blog', 'forum', 'page'])
->limit(15)
->orderBy('created_at', 'desc')
->get()->toArray();
Your query doesn't work as expected because you're using orWhere() without a closure which would group orWhere() clauses.
I'm trying to left join two tables in laravel. after joining got all second table value is empty but data exist in a database .
Can you try this:
$contacts = DB::table('C_Contacts')
->leftJoin('C_Contact_Category_Map', 'C_Contacts.contact_id', '=', 'C_Contact_Category_Map.contact_id')
->get();
Can You try this
$contacts = DB::table('C_Contacts')
->join('C_Contact_Category_Map', 'C_Contacts.contact_id', '=', 'C_Contact_Category_Map.contact_id')
->get();