laravel-query-builder

How to load Laravel with nested models and limit the nested models?

纵饮孤独 提交于 2020-01-21 10:18:08
问题 Hello guys I am using Laravel 5.6 I have three tables in the database posts,comments and replies and three models Post , Comment , Reply the relations are as follow one post has many comments and one comment has many replies. I created a route that when hit will return some data ;however, I want this data to be in a specific way read this example: Lets say I have 6 posts in my database and each post has 6 comments also each comment has 6 replies I want to return only the first 3 posts along

Cast in query builder laravel

拜拜、爱过 提交于 2020-01-13 13:48:11
问题 I want to cast orderBy in query builder laravel because my price is varchar type.. so when it's sorting...the result is far from I want... my script like this DB::table('test')->where(...)->orderBy('price')->get(); i already try something like this $query = CAST(price AS DECIMAL(10,2)) DESC; DB::table('test')->where(...)->orderBy($query)->get(); how can I cast OrderBy query builder so I can sort price desc 回答1: Try it with orderByRaw() : $query = "CAST(price AS DECIMAL(10,2)) DESC"; DB::table

Cast in query builder laravel

笑着哭i 提交于 2020-01-13 13:48:08
问题 I want to cast orderBy in query builder laravel because my price is varchar type.. so when it's sorting...the result is far from I want... my script like this DB::table('test')->where(...)->orderBy('price')->get(); i already try something like this $query = CAST(price AS DECIMAL(10,2)) DESC; DB::table('test')->where(...)->orderBy($query)->get(); how can I cast OrderBy query builder so I can sort price desc 回答1: Try it with orderByRaw() : $query = "CAST(price AS DECIMAL(10,2)) DESC"; DB::table

can save when create new data, but can't calculated data by category

旧时模样 提交于 2020-01-03 02:37:25
问题 Model can save when create new data, but can't calculated data by category public static function findOrCreate($plan_id, $data) { $fromDate = Carbon::now()->subDay()->startOfWeek(); $nowDate = Carbon::now()->today(); $spent_time = static::where('plan_id', $plan_id)->first(); if (is_null($spent_time)) { return static::create($data); }else{ $new_spent_time = SpentTime::first(); $task_category = $new_spent_time->task_category; $new_spent_time->spent_time = $new_spent_time::where('task_category',

change default date format laravel sql query

帅比萌擦擦* 提交于 2019-12-30 17:11:35
问题 I want to change date out put from following SELECT query to DD/MM/YYY format . pls advice. this is what I tried so far ->select('time_sheets.progress',DATE_FORMAT('time_sheets.date', "%d/%l/%Y") ,'role_users.role_id','roles.role_name') 回答1: Instead of using ->select you can use selectRaw to allow raw SQL query so you can use: ->selectRaw('time_sheets.progress, DATE_FORMAT(time_sheets.date, "%d/%l/%Y"), role_users.role_id, roles.role_name'); 回答2: ->select('time_sheets.progress',\DB::raw('DATE

change default date format laravel sql query

牧云@^-^@ 提交于 2019-12-30 17:10:14
问题 I want to change date out put from following SELECT query to DD/MM/YYY format . pls advice. this is what I tried so far ->select('time_sheets.progress',DATE_FORMAT('time_sheets.date', "%d/%l/%Y") ,'role_users.role_id','roles.role_name') 回答1: Instead of using ->select you can use selectRaw to allow raw SQL query so you can use: ->selectRaw('time_sheets.progress, DATE_FORMAT(time_sheets.date, "%d/%l/%Y"), role_users.role_id, roles.role_name'); 回答2: ->select('time_sheets.progress',\DB::raw('DATE

Laravel 5.0 Query Builder - Where with multiple parameters

无人久伴 提交于 2019-12-25 09:01:23
问题 I've a Laravel 5.0 project, that I need to implement the following logic $var1= 'A'; $var2= 'B'; $var3= 50; $data = DataModel::select('attr1','attr2')->where(function($q){ $q->where('attr3','like','%'.$var1.'%'); $q->where('attr4','like',$var2.'%'); $q->where('attr5','=',$var3); })->get(); The problem is for the "Where" function $var1, var2 and $var3 are undefined variables. My Question is, How can I pass multiple parameters to the where function? 回答1: Here, closure function passed in where

How to use inner joins with subqueries in Laravel Eloquent

穿精又带淫゛_ 提交于 2019-12-22 18:11:05
问题 Note: this is laravel 5.3 Basically I'm running a query when a user selects arabic translation.. the full sql looks like this select s.ref, t.text as ref_ar FROM stores AS s INNER JOIN (SELECT item, text FROM translator_translations WHERE locale ='ar' AND namespace ='*' AND item like 'store.ref%' ) AS t ON substring(s.ref_translation from 14 for 26) = t.item; don't see much documentation on subqueries on the official Laravel docs (there are inner join stuff but not good enough) and the SO

I've assigned Laravel Query Builder to a variable. It changes when being used

﹥>﹥吖頭↗ 提交于 2019-12-19 10:27:43
问题 it's a WHY-question, not How-to one:) I have assigned a Query Bulder to a variable $query: $query = table::where(['id'=>1, 'this_version'=> 1]); $versions['slug1'] = $query->select('tourist_id', 'tourist_version')->get()->toArray(); print_r($versions); outputs array with 2(!) sub-arrays: Array ( [slug1] => Array ( [0] => Array ( [tourist_id] => 1 [tourist_version] => 1 ) [1] => Array ( [tourist_id] => 2 [tourist_version] => 1 ) ) ) But if I add another line using $query between my $query

A.* isn't in GROUP BY with left join on laravel query builder

心不动则不痛 提交于 2019-12-17 21:33:09
问题 $search_alls= DB::table('a16s as A') ->select('A.id') // ->select('A.*') ->addSelect(DB::raw('SUM(CASE WHEN B.approve = 1 ELSE 0 END) as Yshow')) ->leftjoin('a16s_likes as B', function($join) { $join->on('A.id', '=', 'B.p_id'); }) ->groupBy('A.id') ->get(); when I use above select('A.id') is work well. But when I use select('A.*') to select all A cloumn I got the error SQLSTATE[42000]: Syntax error or access violation: 1055 'employee.A.name' isn't in GROUP BY PS:employee is my DB name The