I have MySql Query :
select * from transactions where request_shipping_date = '2020-11-25' and status = 'paid' or 'on_process' or 'finish';
and I got 44 Rows
and I Want Convert to Eloquent Model :
$request_shipping_date = Carbon::now()->addDays(1)->format('Y-m-d');
$data = Order::with('order_details')
->where('request_shipping_date', 'like', $request_shipping_date.'%')
->Where('status', 'paid')
->orWhere('status', 'on_process')
->orWhere('status', 'finish')
->get();
and i got : 24025 Rows
Is there any wrong ?
The no of rows retrieved changes because of the like operator when comparing dates
Try using the below
$request_shipping_date = Carbon::now()->addDays(1)->format('Y-m-d');
$data = Order::with('order_details')
->whereDate('request_shipping_date', $request_shipping_date)
->Where('status', 'paid')
->orWhere('status', 'on_process')
->orWhere('status', 'finish')
->get();
You could also rephrase the eloquent query as
$request_shipping_date = Carbon::now()->addDays(1)->format('Y-m-d');
$data = Order::with('order_details')
->whereDate('request_shipping_date', $request_shipping_date)
->whereIn('status', ['paid', 'on_process', 'finish'])
->get();
Related
i have query in my sql like this
SELECT villages.id FROM gocement.villages LEFT JOIN gocement.districts ON villages.district_id = districts.id Where villages.name = 'kebonagung' && districts.name ='sukodono';
from mysql i got the data village.id = 81
'kebonagung' it is can change using row['kelurahan'] from my code here
$village_name = $row['kelurahan'];
$district_name = $row['kecamatan'];
$query = Village::select('villages.*');
$query->leftJoin('districts', 'villages.district_id', '=', 'districts.id');
$query->where('villages.name', '=', $village_name);
$query->where('districts.name', '=', $district_name);
$hasil = $query->get('villages.id');
dd($hasil);
here what i have try using laravel code.
but when i run my laravel i got like this :
my question is how to fix my queries in my laravel code to get 81
Change it to
$query = Village::select('villages.id');
also change
$hasil = $query->get();
see this
$query = Village::select('villages.id');
$query->leftJoin('districts', 'villages.district_id', '=', 'districts.id');
$query->where('villages.name', '=', $village_name);
$query->where('districts.name', '=', $district_name);
$hasil = $query->pluck('id');
dd($hasil->all()[0]);
it will return exactly 81
I trying to convert MySQL query to Laravel eloquent, but not getting the right output.
MySQL query -
Select contents.con_type, contents.operation, contents.ecu, contents.ecu_sub, COUNT(*)
from contents
group by operation, con_type, ecu, ecu_sub
order by operation asc, con_type asc
MySQL query to Laravel elequent -
$content = Content::query();
if ($request->input('total') == '1') {
$content = $content->select('con_type', 'operation', 'ecu', 'ecu_sub')
->groupBy('operation', 'con_type', 'ecu', 'ecu_sub')
->orderBy('operation', 'asc')
->orderBy('con_type', 'asc')
->get()->count();
}
The output I trying to get:
Thanks in advance.
Test
$content = Content::query();
if ($request->input('total') == '1') {
$content = $content->select(DB::raw('con_type, operation, ecu, ecu_sub, count(*) as `count`')
->groupBy('operation', 'con_type', 'ecu', 'ecu_sub')
->orderBy('operation', 'asc')
->orderBy('con_type', 'asc')
->get();
}
I have the following raw SQL query:
select a.id user_id, a.email_address, a.name_first, a.name_last, count(b.id) number_of_videos, sum(b.vimeo_duration) total_duration, sum(b.count_watched) total_playbacks
from users a,
videos b
where a.id = b.tutor_id
and a.email_address in ('candace_rennie#yahoo.com', 'tjm#hiltoncollege.com', 'matthewjameshenshall#gmail.com', 'nkululeko#syafunda.co.za', 'khulile#syafunda.co.za', 'nzakheni#syafunda.co.za')
group by a.id;
This correctly gets 6 rows from the database. I'm trying to convert this to a Laravel database query like so:
$totals = DB::table('users')
->select(DB::Raw('users.id as user_id'), 'users.email_address', 'users.name_first', 'users.name_last', DB::Raw('count(videos.id) as number_of_videos'), DB::Raw('sum(videos.vimeo_duration) as total_duration'), DB::Raw('sum(videos.count_watched) as total_playbacks'))
->join('videos', 'users.id', '=', 'videos.tutor_id')
->where('users.id', 'videos.tutor_id')
->whereIn('users.email_address', array('candace_rennie#yahoo.com', 'tjm#hiltoncollege.com', 'matthewjameshenshall#gmail.com', 'nkululeko#syafunda.co.za', 'khulile#syafunda.co.za', 'nzakheni#syafunda.co.za'))
->groupBy('users.id')
->get();
This however return 0 rows. Is there anything I'm missing?
It should be smth like below even tho groupBy user id does not help much as id is unique.
$aggregates = [
DB::raw('count(b.id) as number_of_videos'),
DB::raw('sum(b.vimeo_duration) as total_duration'),
DB::raw('sum(b.count_watched) as total_playbacks'),
];
$simpleSelects = ['users.email_address', users.id, 'users.name_first', 'users.name_last'];
$emails = ['candace_rennie#yahoo.com', 'tjm#hiltoncollege.com'....]
$users = Users::select(array_merge($simpleSelects, $aggregates))
->leftJoin('videos as b', function ($join) use ($emails) {
$join->on('b.tutor_id', 'a.id')
->whereIn('users.email_address', $emails);
})
->groupBy('users.id')
->get();
Try to remove this line:
->where('users.id', 'videos.tutor_id')
List item
after sql code convert into laravel
DB::select('posts.id','posts.title','posts.body')
->from('posts')
->where('posts.author_id', '=', 1)
->orderBy('posts.published_at', 'DESC')
->limit(10)
->get();
So I'm not sure whether I asked the question well. I'm new to laravel and right now I'm building an application where I need to fetch data for a user if certain conditions are met. This is what I'm trying to achieve:
Select from database where userid = $user and userstatus = active or inactive. or pending.
So all rows with the user id($user) and status(active, inactive and pending) will be returned.
I tried doing:
$users = DB::table('users')
->where('status', '=', 'active')
->orwhere('status', '=', 'inactive')
->orwhere('status', '=', 'pending')
->get();
This returned all the data in that particular table. What is the best way to go about this.
You have at least two options for how to do this you can use, including both so you have one for use in this case and one for general usage. Using a function in where creates a new block, so works like adding parentheses to your query
$users = DB::table('users')
->where('id', $user)
->where(function($query) {
$query->where('status', 'active')
->orWhere('status', 'inactive')
->orWhere('status', 'pending');
})->get();
or
$users = DB::table('users')->where('id', $user)->whereIn('status', ['active', 'inactive', 'pending'])->get();
You can do it as:
$users = DB::table('users')
->where('userid', $userid)
->whereIn('status',['active', 'inactive','pending'])
->get();
In my first query, I am looping through all records that have a status of open, and channel_id of 23, getting the column field_id_299. The field field_id_299 is the entry_id for another channel.
The second query, then uses this field_id_299 AS entry_id, getting the column field_id_36.
Is it possible to do all of this in one query?
$query = ee()->db->select('cd.field_id_299')
->from('exp_channel_titles AS ct')
->join('exp_channel_data AS cd', 'ct.entry_id = cd.entry_id', 'left')
->where('ct.status', 'open')
->where('ct.channel_id', '23')
->get();
$query = ee()->db->select('field_id_36')
->from('exp_channel_data')
->where('entry_id', $entry_id)
->get();
Try below
$query = ee()->db->select('cd1.field_id_36')
->from('exp_channel_titles AS ct')
->join('exp_channel_data AS cd', 'ct.entry_id = cd.entry_id', 'left')
->join('exp_channel_data AS cd1', 'cd.field_id_299 = cd1.entry_id', 'left')
->where('ct.status', 'open')
->where('ct.channel_id', '23')
->get();