please help me in my query . I am currently getting query error. I am new in laravel and i don't know how to make my query into laravel query. Here is my query :
$date1 = date("Y-m-d", strtotime($request->datepicker));
$date2 = date("Y-m-d", strtotime($request->datepicker1));
$products = DB::table('shipping_table')
->select('products.product_name', 'products.price', DB::raw('Sum(shipping_products.quantity) AS qtysold'), 'shipping_table.sold_date')
->join('shipping_products','shipping_table.shipping_id', '=', 'shipping_products.shipping_id')
->join('products','products.product_id', '=', 'shipping_products.product_id')
->where(['shipping_table.shipping_status', '=' ,1])
->whereBetween(DB::raw("date(shipping_table.sold_date"),[$date1,$date2])
->groupBy('products.product_name')
->get();
please take a look in whereBetween because i am suspecting if it's the one who has wrong syntax.
Error:
Syntax error or access violation: 1064
Got it! You are missing an ) on your between clause:
$date1 = date("Y-m-d", strtotime($request->datepicker));
$date2 = date("Y-m-d", strtotime($request->datepicker1));
$products = DB::table('shipping_table')
->select('products.product_name', 'products.price', DB::raw('Sum(shipping_products.quantity) AS qtysold'), 'shipping_table.sold_date')
->join('shipping_products','shipping_table.shipping_id', '=', 'shipping_products.shipping_id')
->join('products','products.product_id', '=', 'shipping_products.product_id')
->where('shipping_table.shipping_status',1)
->whereBetween(DB::raw("date(shipping_table.sold_date)"),[$date1,$date2])
->groupBy('products.product_name')
->get();
Try now
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
Here my MySQL query (work in phpMyAdmin) :
SELECT waktu_kerusakan, workcenter, COUNT(workcenter) AS jumlah_repair
FROM repair WHERE year(waktu_kerusakan)='2019' GROUP BY workcenter ,
month(waktu_kerusakan) BETWEEN 1 and 6 Order By jumlah_repair Desc
then, i try in Laravel Syntax like this below (not work) :
$sql = Main::groupBy('workcenter')->select('workcenter', \DB::raw('count(*) as frekuensi'))
->whereYear('waktu_kerusakan', 'like', "%".$tahun."%")
->OrderBy('frekuensi', 'Desc')
->groupBy(\DB::raw("MONTH(waktu_kerusakan)"), [1, 6])
->get();
Please anyone help me to convert the MySQL query to Laravel Syntax. Thank you!
Try this.
$repair = DB::table('repair')
->select('waktu_kerusakan','workcenter', DB::raw('COUNT(workcenter) AS jumlah_repair'))
->whereYear('waktu_kerusakan', $tahun)
->groupBy(DB::raw("MONTH(waktu_kerusakan)"), 'workcenter')
->whereIn(DB::raw('MONTH("waktu_kerusakan")'),[1, 6])
->orderBy('frekuensi', 'DESC')
->get()
Try this query :
$sql = Main::select('workcenter', \DB::raw('count(*) as frekuensi'),\DB::raw('MONTH(waktu_kerusakan) as waktu_kerusakan_month'))
->where('waktu_kerusakan',$tahun)
->whereBetween('waktu_kerusakan_month',[1, 6])
->groupBy('workcenter','waktu_kerusakan_month')
->OrderBy('jumlah_repair', 'Desc')
->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();
I need to join two tables in laravel model. Here is my query,
$id="1002";
$resultsInCalls = DB::table('qlog')
->select('user_master.fname','user_master.lname','qlog.data2')
->whereDate('created', '=', date('Y-m-d')) // this day
->join('qlog', 'qlog.agent', '=', 'user_master.sip_id')
->where('agent', '=', $id)
->where(function ($query) {
$query->where('event', '=', 'COMPLETEAGENT')
->orWhere('event', '=', 'COMPLETECALLER');
})
->take(5)
->get();
But I run this query I get following error.
SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'qlog' (SQL: select `user_master`.`fname`, `user_master`.`lname`, `qlog`.`data2` from `qlog` inner join `qlog` on `qlog`.`agent` = `user_master`.`sip_id` where date(`created`) = 2017-11-29 and `agent` = 1002 and (`event` = COMPLETEAGENT or `event` = COMPLETECALLER) limit 5)
I read related questions. But I can't figure it out. How to fix this error.
Here I get it done,
$resultsInCalls = DB::table('qlog')
->join('user_master as user_table', 'qlog.agent', '=', 'user_table.sip_id')
->select('user_table.fname','user_table.lname','qlog.data2')
->whereDate('created', '=', date('Y-m-d')) // this day
->where('agent', '=', $id)
->where(function ($query) {
$query->where('event', '=', 'COMPLETEAGENT')
->orWhere('event', '=', 'COMPLETECALLER');
})
->take(5)
->get();
Hi I have a query in my laravel project which should retrieve dates between ranges. My query is:
$dateFrom = date("Y-m-d", strtotime($request->dateFrom));
$dateTo = date("Y-m-d", strtotime($request->dateTo));
$posts = DB::table('posts')->join('categories','posts.category_id','=','categories.id')->
join('users','posts.user_id','=','users.id')
->select('posts.id','posts.topic','categories.category_name','posts.created_at','users.name')
->whereBetween('posts.created_at',[$dateFrom,$dateTo])
->get();
I'd like to use DATE mysql_function on column posts.created_at but I don't know how could I do it. Below query dosen't work:
$dateFrom = date("Y-m-d", strtotime($request->dateFrom));
$dateTo = date("Y-m-d", strtotime($request->dateTo));
$posts = DB::table('posts')->join('categories','posts.category_id','=','categories.id')->
join('users','posts.user_id','=','users.id')
->select('posts.id','posts.topic','categories.category_name','posts.created_at','users.name')
->whereBetween('DATE(posts.created_at)',[$dateFrom,$dateTo])
->get();
I would be greatful for help. Best regards ;)
change ->whereBetween('DATE(posts.created_at)'
to ->whereBetween(DB::raw('DATE(posts.created_at)'))
if you want to add some raw expression in a query you can use DB::row method as mentioned in laravel documentation
Try using Carbon like this:
use Carbon\Carbon;
...
$dateFrom = Carbon::parse($request->dateFrom);
$dateTo = Carbon::parse($request->dateTo);
$posts = DB::table('posts')
->join('categories','posts.category_id','=','categories.id')
->join('users','posts.user_id','=','users.id')
->select('posts.id','posts.topic','categories.category_name','posts.created_at','users.name')
->whereBetween('posts.created_at',[$dateFrom,$dateTo])
->get();