laravel-eloquent

Laravel Orderby not working inside Eloquent whereHas relationship?

岁酱吖の 提交于 2019-12-08 02:15:02
问题 Please have look on below code. $serviceNew = CompletedService::selectRaw('gallons_collected as total,account_id,id,service_provider_id,service_id,service_date') ->where("service_id", '1')->where('service_provider_id', $service_privider_id)->whereMonth('service_date', $month)->whereYear('service_date', $year) ->with('account') ->whereHas('account', function($qs) { $qs->orderBy('restaurant_name', 'DESC'); }) ->get(); I have multiple records in "CompletedService" and there is one parent id

laravel Collections's function only on Elequent ORM result always return empty collection

时光怂恿深爱的人放手 提交于 2019-12-07 16:57:39
问题 I want only return the id , name and pic_thumbnail column of the Eloquent ORM collections result. I use only() on the result of get() . But I always get the empty collection. public function models() { if(!isset($_SESSION["uid"])) return Response::json(['error' => 'please login.']); $models = Model::where('user_id', $_SESSION["uid"])->get(); Log::info($models); Log::info($models->only(['id', 'name'])); Log::info($models->only(['id', 'name'])->all()); foreach($models as $model){ $pic = $model-

MySQL error when creating foreign key with Laravel migration

早过忘川 提交于 2019-12-07 05:42:06
问题 I have a Laravel application set up and I'm using Sentry 2 for user authentication. I have a model called Post along with the default sentry User table. I would like to have a user to have many posts, and a post to belong to a user. To reflect this in my database schema, I need to create a foreign key constraint between posts and users . The migration that I've written is the following: public function up() { Schema::table('posts', function(Blueprint $table) { $table->integer('user_id')-

laravel Collections's function only on Elequent ORM result always return empty collection

感情迁移 提交于 2019-12-05 23:16:48
I want only return the id , name and pic_thumbnail column of the Eloquent ORM collections result. I use only() on the result of get() . But I always get the empty collection. public function models() { if(!isset($_SESSION["uid"])) return Response::json(['error' => 'please login.']); $models = Model::where('user_id', $_SESSION["uid"])->get(); Log::info($models); Log::info($models->only(['id', 'name'])); Log::info($models->only(['id', 'name'])->all()); foreach($models as $model){ $pic = $model->pics()->orderBy('displayorder')->first(); if($pic == null) $model->pic_thumbnail = 'static/thumbnail

Laravel Eloquent Many-to-Many query whereIn

风格不统一 提交于 2019-12-04 18:21:41
问题 In my application I have updated a relationship from one-to-many to many-to-many and I'm trying to figure out a way to persist associated functionality. Let's say I have two related tables, e.g. dogs and owners. If I have an array of owners and I'm trying to get a list of dogs id's for those owners, how should I do it eloquently? Similar question was asked here: https://laracasts.com/discuss/channels/laravel/getting-many-to-many-related-data-for-an-array-of-elements So, How would I get the

Laravel hasMany Many to Many To One Eloquent

你说的曾经没有我的故事 提交于 2019-12-03 17:11:34
问题 I haven't had much luck sorting this out the Laravel way. So I pose two questions. Given that I have a Car and that Car can have many Features, but that Features are also separated by Feature Type, how can I return all Features, separated the Feature Type, for said Car? I have four tables, with listings_features being the pivot table: listings (id) listings_features (listing_id, feature_id) listings_features_types (id, type) listings_features_values (id, listing_feature_type_id, value) I have

Laravel hasMany Many to Many To One Eloquent

[亡魂溺海] 提交于 2019-12-03 05:28:14
I haven't had much luck sorting this out the Laravel way. So I pose two questions. Given that I have a Car and that Car can have many Features, but that Features are also separated by Feature Type, how can I return all Features, separated the Feature Type, for said Car? I have four tables, with listings_features being the pivot table: listings (id) listings_features (listing_id, feature_id) listings_features_types (id, type) listings_features_values (id, listing_feature_type_id, value) I have the following code, which produces what I need, but when I use it, I get a Laravel error ... "Call to

Why wheredate not working in laravel 5.3?

南笙酒味 提交于 2019-12-02 15:57:47
问题 My laravel eloquent is like this : public function search(Request $request) { $search = ''; $searchdate = '2017-04-27'; $reports = Report::whereHas('user', function($query) use ($search) { $query->where(function ($q) use($search) { $q->where('name' ,'LIKE' ,'%'.$search.'%'); }); })->orWhereHas('store', function($query) use ($search){ $query->where(function ($q) use($search) { $q->where('name', 'LIKE', '%'.$search.'%'); }); })->orWhereHas('category', function($query) use ($search) { $query-

Laravel insert millions of database rows from models

旧城冷巷雨未停 提交于 2019-12-02 10:28:13
I have a text file that contains comma delineated values representing data set with each row within the string. There are about 2 million of them and I want to parse the string, create Laravel models from them and store each as a row in my database. At this time, I have a class that parses the file line by line and creates a model for each as follows: class LargeFileParser{ // File Reference protected $file; // Check if file exists and create File Object public function __construct($filename, $mode="r"){ if(!file_exists($filename)){ throw new Exception("File not found"); } $this->file = new

How can I make sub query in laravel eloquent?

时光怂恿深爱的人放手 提交于 2019-12-02 09:33:14
When I use db raw, it works My query is using db raw like this : $products = DB::select(DB::raw('SELECT * FROM ( SELECT a.*, b.name AS store_name, b.address FROM products a JOIN stores b ON b.id = a.store_id WHERE a.category_id = '.$category_id.' ORDER BY a.total_sold DESC, a.updated_at DESC LIMIT '.$num.' ) AS product GROUP BY store_id')); It works. But I want to change it use laravel eloquent I try like this : $products = Product::where('category_id', '=', $category_id) ->with('store') ->groupBy('store_id') ->orderBy('total_sold','desc') ->orderBy('updated_at', 'desc') ->take($num) ->get();