laravel-query-builder

can't save new data, and Array to string conversion?

感情迁移 提交于 2019-12-11 04:27:12
问题 Model in laravel "Array to string conversion (SQL: update spent_times set updated_at = 2018-10-18 06:02:29, spent_time = 12, percentage = 60.00, task_category = testing where id = 7) ◀" public static function findOrCreate($plan_id, $data) { $fromDate = Carbon::now()->subDay()->startOfWeek()->toDateString(); $nowDate = Carbon::now()->today()->toDateString(); $spent_time = static::where('plan_id', $plan_id)->first(); if (is_null($spent_time)) { return static::create($data); }else{ $new_spent

Laravel Query Builder - Use sum method on a generated attribute

萝らか妹 提交于 2019-12-10 00:10:20
问题 Let's say I have these 2 models: Order Model: id state (incomplete, complete) Item Model: id order_id type is_worthy. . /** * Returns the item's price according to its worthy */ public function getPriceAttribute() { return $this->is_worthy ? 100 : 10; // $ } So far so good. Now I want to summarize the price of the complete orders. So I'm doing this: App\Item::whereHas('order', function ($query) { $query->where('state', 'complete'); })->sum('price') But the thing is, that I don't have in my

Laravel 4 query builder - with complicated left joins

霸气de小男生 提交于 2019-12-09 16:28:48
问题 I'm new to Laravel 4. I have this query: SELECT a.id, active, name, email, img_location, IFNULL(b.Total, 0) AS LeadTotal, IFNULL(c.Total, 0) AS InventoryTotal FROM users AS a LEFT JOIN ( SELECT user_id, count(*) as Total FROM lead_user GROUP BY user_id ) AS b ON a.id = b.user_id LEFT JOIN ( SELECT user_id, count(*) as Total FROM user_inventory GROUP BY user_id ) AS c ON a.id = c.user_id WHERE a.is_deleted = 0 How can I convert it to Laravel query builder? I'm confused on how to use the

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

梦想与她 提交于 2019-12-07 11:32:28
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',$task_category) ->sum('daily_spent_time', $new_spent_time->daily_spent_time , $fromDate); $new_spent

How to use inner joins with subqueries in Laravel Eloquent

こ雲淡風輕ζ 提交于 2019-12-06 08:20:43
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 advice seems extra-hacky.. advice? context this will be used as a scope inside a model, so this works for

Laravel 4 query builder - with complicated left joins

我的梦境 提交于 2019-12-04 03:29:58
I'm new to Laravel 4. I have this query: SELECT a.id, active, name, email, img_location, IFNULL(b.Total, 0) AS LeadTotal, IFNULL(c.Total, 0) AS InventoryTotal FROM users AS a LEFT JOIN ( SELECT user_id, count(*) as Total FROM lead_user GROUP BY user_id ) AS b ON a.id = b.user_id LEFT JOIN ( SELECT user_id, count(*) as Total FROM user_inventory GROUP BY user_id ) AS c ON a.id = c.user_id WHERE a.is_deleted = 0 How can I convert it to Laravel query builder? I'm confused on how to use the Laravel join query builder with this type of query. Answer!! Will all the help of petkostas on laravel forum.

Laravel: change a raw query in a “query-builder” or “eloquent” one

僤鯓⒐⒋嵵緔 提交于 2019-12-03 15:21:07
I have this Laravel Query Builder snippet that is working fine: $records = DB::table('users') ->select( DB::raw('users.*, activations.id AS activation, (SELECT roles.name FROM roles INNER JOIN role_users ON roles.id = role_users.role_id WHERE users.id = role_users.user_id LIMIT 1) AS role') ) ->leftJoin('activations', 'users.id', '=', 'activations.user_id') ->where('users.id', '<>', 1) ->orderBy('last_name') ->orderBy('first_name') ->paginate(10); Is there a way to avoid use of raw queries and get the same result? In other words, how can I write this in a more "query-builder" style? Can I also

SQL/Laravel - Why does my query returns an empty collection?

匆匆过客 提交于 2019-12-02 16:36:47
问题 My Laravel Query Builder returns an empty collection, while the SQL string itself returns the correct records when executed inside phpmyadmin. This is my code: $times = Time::join(\DB::raw('(SELECT `ath_id`, `stroke_id`, MIN(time) AS time FROM times GROUP BY ath_id, stroke_id) b'), function($join) { $join->on('times.ath_id', '=', 'b.ath_id') ->where('times.stroke_id', '=', 'b.stroke_id') ->where('times.time', '=', 'b.time'); }) ->where('times.ath_id', '=', $id) ->orderBy('times.stroke_id',

SQL/Laravel - Why does my query returns an empty collection?

柔情痞子 提交于 2019-12-02 11:17:24
My Laravel Query Builder returns an empty collection, while the SQL string itself returns the correct records when executed inside phpmyadmin. This is my code: $times = Time::join(\DB::raw('(SELECT `ath_id`, `stroke_id`, MIN(time) AS time FROM times GROUP BY ath_id, stroke_id) b'), function($join) { $join->on('times.ath_id', '=', 'b.ath_id') ->where('times.stroke_id', '=', 'b.stroke_id') ->where('times.time', '=', 'b.time'); }) ->where('times.ath_id', '=', $id) ->orderBy('times.stroke_id', 'ASC') ->orderBy('times.date', 'DESC'); dd($times->get()); Below is the sql that works inside phpmyadmin,

Cannot save calculation data by category when create new data in laravel

家住魔仙堡 提交于 2019-12-01 14:40:35
Model SpentTime I am unable save calculation data by category when create new data in laravel public static function findOrCreate($plan_id, $data) { $fromDate = Carbon::now()->subDay()->startOfWeek()->toDateString(); $nowDate = Carbon::now()->today()->toDateString(); $spent_time = static::where('plan_id', $plan_id)->first(); if (is_null($spent_time)) { return static::create($data); }else{ $task_category = SpentTime::where('task_category', $spent_time->task_category)->get(); $create_spent_times = SpentTime::where('task_category', $spent_time->task_category)->sum('daily_spent_time', $fromDate);