I am trying to fire eloquent query in laravel 5.2
Movies_showtimes::with('showdata','movie','cinema')->where(['cinema_id'=>$id,'show_date'=>Carbon::today()])->get();
This query fetches all movie showtimes for today date only. I want to fetch the movie showtimes for >= today but I am unable to fit >= symbol in where() clause in above query.
Help is appreciated. I tried doing like this but it didn't worked for me
Movies_showtimes::with('showdata','movie','cinema')->where(['cinema_id'=>$id,'show_date','>=',Carbon::today()])->get();
Try this query
Movies_showtimes::with('showdata','movie','cinema')->where(
[['cinema_id','=',$id],['show_date','>=',Carbon::today()]]
)->get();
Related
Can any one convert the below given raw query to laravel eloquent query
SELECT `from`,`to`,`from`+`to` as total FROM `chats`;
i have tried select raw , and db raw but it is not working for the sum to from and to as total
i find the way to do it
Chat::select('id','from', 'to', 'message','read','created_at',DB::raw('`from` + `to` as total'))->get();
it's working fine thanx for your support guys.
I am using laravel 8. I want to insert multiple rows by selecting from one table to another. I am using DB::statement() but now want to do it using eloquent. Here is my sample code:
DB::statement("insert into bank_information (user_id, bank_id, account_name,account_number)
select applicant_id,bank_id,account_name,account_number from applicant_bank_information
where erec_applicant_bank_information.applicant_id =".$applicant_id);
How can I do it using single eloquent command ? Also, Is there any other smarter/faster way for it in laravel ??
I do not know of a Eloquent way for this, but after Laravel 5.7.17 we have a new method in the query builder insertUsing(). Unfortunately this is not mentioned in the official documentation.
The query builder code will be something like this:
$select = DB::table('applicant_bank_information as abi')
->select('applicant_id','bank_id','account_name','account_number')
->where('abi.applicant_id', $applicant_id);
$rows_inserted = DB::table('bank_information')
->insertUsing(['user_id','bank_id', 'account_name','account_number'], $select);
Tested and working. Keep in mind that you do not run the select request with ->get().
I'm working on a query where I've to show the records on ascending order base and in database my table field created_at has 2016-11-29 12:18:22 and I want to retrieve date only so I made laravel query and my laravel query is
$countOffer = countOffer::select(DB::raw("DATE(created_at) AS created_at"))->where("offerStatus", "!=", 7)->orderBy("created_at", "ASC")->get()->toArray();
now the problem is when I add order by clause at created_at field it shows 0000-00-00 so I made query on MySQL yog for testing purpose and query is
SELECT DATE(created_at) AS created_at FROM countOffer WHERE offerStatus != 7 ORDER BY created_at ASC
it is also showing the same value 0000-00-00 and search it on different forums but didn't get any solution related to my problem.
Any help will be appreciated.
Thanks in advance
You should update your query like below step
Step 1 :
$countOffer = countOffer::select(DB::raw("DATE(created_at) AS created_at"))
->where("offerStatus", "!=", 7)->orderBy("created_at", "ASC")->get();
Step 2 :
Then you should get the $countOffer array of what you need.
Hope this helps you
You can get created_at only field by using get(['created_at']):
$countOffers = countOffer:where('offerStatus', '!=', 7)->oldest()->get(['created_at']);
In this case, you're using Eloquent without raw queries and it's so much better to use it to keep your app maintainable.
Also, since created_at is a Carbon instance, you can do this in a controller, view or any other place of your app:
#foreach ($countOffers as $countOffer)
{{ countOffer->created_at->toDateString() }}
#endforeach
If you need the data for exporting, use the map() collection method to convert Carbon instance to whatever format you want.
I am using Laravel 5 and its Query Builder to retrieve data from database.
I want to take following output.
I want to take this question thinks using Query Builder
Now I try this code, but not successfully:
$all_members = CommiteeMember::join('members', 'commiteeMembers.memberId', '<>', 'members.id')
->where('commiteeId', $commitee->id)->get();
I don't know what are you trying to achieve.
But from your title looks like what you need is left join.
$all_members = CommiteeMember::leftJoin('members', 'commiteeMembers.memberId', '<>', 'members.id')
->where('commiteeId', $commitee->id)->get();
I am completely new to Laravel Framework. And I am using 4.2 version.
I am trying to run below sql statements through function calling from routes.php.
The page shows no error.
But there is no 'truncate' or 'insert' happend in Database.
Here is the code in Controller funtion:
DB::statement('TRUNCATE TABLE calc2');
DB::statement(
"Insert into calc2
Select groups, members, date(timestamp) as Date, count(id) as Total, sum(order) as Order
from Order
where order > 0 and date(timestamp) >= '$lastday'
group by groups, members, date(timestamp)
");
I checked DB connections. I also run these query directly in MySQL. It is working fine.
I tried DB::select instead of DB::statement and I tried with Eloquent/raw too.
But still Database having 0 records only.
Thanks in advance if anyone can suggest me where I am doing mistake.
Are you commiting your queries?
If not, just use DB::commit();
Doc: http://laravel.com/docs/4.2/database#database-transactions