eloquent

Laravel filter a value in all columns

佐手、 提交于 2021-02-08 07:45:23
问题 public function getBooks($input) { $books= Book::where('book_name', 'LIKE', '%' . $input . '%')->get(); return Response::json($books); } I know how to filter a column by a given value. But how do I filter ALL columns by a given value. For example, I have a column called 'category' where user should be able to use the same search bar to filter the category. Something like: $books = Book::where('all_columns', 'LIKE', '%' . $input . '%')->get(); Thanks! 回答1: Most databases do not support

Laravel Eloquent Pluck produced incorrect data

余生颓废 提交于 2021-02-08 04:10:55
问题 Trying to get created_at date using pluck. $campaigns_dates = CampaignHistory::where('status', "Complete")->orderBy( 'created_at', 'ASC' )->pluck('created_at'); $campaigns_dates = json_decode( $campaigns_dates ); return $campaigns_dates; //Getting This ["2020-06-29T14:19:03.000000Z","2020-06-29T14:19:42.000000Z","2020-07-29T14:23:54.000000Z","2020-08-28T14:55:53.000000Z","2020-08-29T14:17:40.000000Z","2020-09-29T14:18:27.000000Z","2020-09-29T14:21:13.000000Z"] //But Want This Example: ["2020

Laravel sync Relation with optional parameters

那年仲夏 提交于 2021-02-08 03:39:26
问题 I use the sync function for syncing a belongsToMany Relation: $model->products()->sync($productIds); In the $productIds array there is flat array with some Id's - something like this: $productIds = [1,3,5,6]; What I want: The pivot table has also additional columns like "created_by" and "updated_by". But how can I add these fields to my array WITHOUT doing a foreach loop? Is there a shorter way to do this? I need an array like this: $productIds = [1 => [ 'created_by' => 1, 'updated_by' => 1 ]

Laravel how do I get the row number of an object using Eloquent?

梦想的初衷 提交于 2021-02-07 20:49:38
问题 I'd like to know the position of a user based on its creation date. How do I do that using Eloquent? I'd like to be able to do something like this: User::getRowNumber($user_obj); 回答1: I suppose you want MySQL solution, so you can do this: DB::statement(DB::raw('set @row:=0')); User::selectRaw('*, @row:=@row+1 as row')->get(); // returns all users with ordinal 'row' So you could implement something like this: public function scopeWithRowNumber($query, $column = 'created_at', $order = 'asc') {

Laravel how do I get the row number of an object using Eloquent?

烂漫一生 提交于 2021-02-07 20:47:30
问题 I'd like to know the position of a user based on its creation date. How do I do that using Eloquent? I'd like to be able to do something like this: User::getRowNumber($user_obj); 回答1: I suppose you want MySQL solution, so you can do this: DB::statement(DB::raw('set @row:=0')); User::selectRaw('*, @row:=@row+1 as row')->get(); // returns all users with ordinal 'row' So you could implement something like this: public function scopeWithRowNumber($query, $column = 'created_at', $order = 'asc') {

Cant retrieve column value from Laravel's Eloquent when primary key is varchar

对着背影说爱祢 提交于 2021-02-07 12:24:18
问题 I encountered an issue where my Laravel's Eloquent Model does not give me the value of the column called 'id' it just turn to be an integer (0) instead of a string. I though the column was somehow protected but in other Models where the 'id' is an intenger it return the value just fine. Question: Can't I use VARCHAR for primary keys? Note: I have to create more tables with respective models where the primary key need to be a string; I'm kinda new Laravel Migration: Schema::create('country',

Laravel 'like' query with MongoDB connection

ぃ、小莉子 提交于 2021-02-05 11:19:31
问题 I am facing an issue in laravel 'like' query. I have a MIS on laravel with databases on MongoDb. Now my DB has a table named kw with urlencoded keywords like cars%20in%20London , Now my query gives accurate results for cars or cars%20in%20London but when I search cars%20in I get 0 results! This is how laravel 'like' is used in query but Mongo uses /. m ./ form, How can I make this working. Here is my Model function public static function selectKeywordIncomplete($keyword) { $search_volume

Laravel order results by column on polymorphic table

邮差的信 提交于 2021-02-04 21:40:14
问题 Short question Using Laravel's Eloquent models; how do I order a query by a column on a has morphMany polymorphic relationship? Longer question Say I have the given table structure: // table: images + -- + ------------ + -------------- + ----- + ----------- + | id | imageable_id | imageable_type | views | upload_date | + -- + ------------ + -------------- + ----- + ----------- + | 1 | 1 | User | 100 | 2016-16-06 | | 2 | 3 | User | 200 | 2016-16-06 | + -- + ------------ + -------------- + ----

Eager load relationships in laravel with conditions on the relation

点点圈 提交于 2021-02-04 13:31:09
问题 I have categories related to each other in a tree. Each category hasMany children. Each end category hasMany products. The products also belongsToMany different types. I want to eager load the categories with their children and with the products but I also want to put a condition that the products are of a certain type. This is how my categories Model looks like public function children() { return $this->hasMany('Category', 'parent_id', 'id'); } public function products() { return $this-

Query relationship Eloquent

左心房为你撑大大i 提交于 2021-02-04 10:37:30
问题 I have News model, and News has many comments, so I did this in News model: public function comments(){ $this->hasMany('Comment', 'news_id'); } But I also have field trashed in comments table, and I only want to select comments that are not trashed. So trashed <> 1 . So I wonder is there a way to do something like this: $news = News::find(123); $news->comments->where('trashed', '<>', 1); //some sort of pseudo-code Is there a way to use above method or should I just write something like this: