Laravel eloquent builder to raw - php

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.

Related

Insert from select in laravel using eloquent

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().

Laravel How to sum DB query with substring?

I want to sum with substring in laravel like this sql.
(My model name is Fgbarcode) Thanks for help me T^T
SELECT SUM(substr(fg_code, 22,14)) AS fg_code
FROM fg_barcode group BY substr(fg_code,2,3);
Assuming you have an eloquent model you are able to utilize DB raw to achieve these custom mysql queries.
FgBarcode::
select(DB::raw('SUM(substr(fg_code, 22,14)) AS fg_code'))
->groupBy(DB::raw('substr(fg_code,2,3)'));

order by not working with mysql date function laravel

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.

how to write >= eloquent database query in laravel 5.2

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();

Mysql query into Eloquent

I'm converting roject from pure PHP to Laravel 4, and I have a problem with converting this query
SELECT * FROM verses,thoughts WHERE verses.id != thoughts.verses_id
I have two tables verse and thought. one verse has one thought. when creating a thought i want to display all the verse whose thoughts has not been created . Im using Laravel 4.2 display the verses it is not listed in thoughts
Did you read the docs?
DB::table('thoughts')
->join('verses', 'verses.id', '!=', 'thoughts.verses_id')
->get();

Categories