eloquent

filtering a paginated eloquent collection

旧巷老猫 提交于 2020-06-14 09:38:35
问题 I am trying to filter a paginated eloquent collection, but whenever I use any of the collection methods, I lose the pagination. $models = User::orderBy('first_name','asc')->paginate(20); $models = $models->each(function($model) use ($filters) { if(!is_null($filters['type'])) { if($model->type == $filters['type']) return $model; } if(!is_null($filters['state_id'])) { if($model->profile->state_id == $filters['state_id']) return $model; } if(!is_null($filters['city_id'])) { if($model->profile-

Eloquent query scope on relationship

我怕爱的太早我们不能终老 提交于 2020-06-13 05:58:29
问题 I have two models called Page and User and I've set up the Eloquent relation as follows: public function user() { return $this->belongsTo('User') } When I dd(Page::with('user')->get()) I get the right result. But now I want to have a search filter on this result. I've used a scopeSearch but I'm not sure how to search on the resultset. At the moment I have a scope like this: public function scopeSearch($query, $search) { $query->where('username', 'LIKE', '%'.$search.'%') ->orWhere('name',

Custom sorting on a laravel relationship collection

心不动则不痛 提交于 2020-06-10 03:44:30
问题 I'm a little stuck on something that usually is quite straight forward. I need to sort records from a hasMany relationship into a custom order based on a certain value and an 'sort order' array. My code below doesn't work because I'm passing uSort() a eloquent collection and I'm not sure how to get around it. $go = $this->hasMany('Product')->orderBy('colour','DESC'); $order = array('RED', 'GREEN', 'BLUE', 'YELLOW'); usort($go, function ($a, $b) use ($order) { $pos_a = array_search($a->colour,

laravel 5.2 - Model::all() order by

倾然丶 夕夏残阳落幕 提交于 2020-06-09 16:47:29
问题 I get the full collection of a Model with the following: $posts = Post::all(); However I want this is reverse chronological order. What is the best way to get this collection in the desired order? 回答1: $posts = Post::orderBy('created_at', 'desc')->get(); You can use the orderBy method. Replace the column name with the one you want. 回答2: You can now use sortBy or sortByDesc : $posts = Post::all()->sortBy('created_at'); 回答3: As many may be moving to newer versions of Laravel, you can use :

laravel 5.2 - Model::all() order by

冷暖自知 提交于 2020-06-09 16:45:09
问题 I get the full collection of a Model with the following: $posts = Post::all(); However I want this is reverse chronological order. What is the best way to get this collection in the desired order? 回答1: $posts = Post::orderBy('created_at', 'desc')->get(); You can use the orderBy method. Replace the column name with the one you want. 回答2: You can now use sortBy or sortByDesc : $posts = Post::all()->sortBy('created_at'); 回答3: As many may be moving to newer versions of Laravel, you can use :

How to delete all the rows in a table using Eloquent?

纵饮孤独 提交于 2020-06-09 07:55:44
问题 My guess was to use the following syntax: MyModel::all()->delete(); But that did not work. I'm sure it's super simple, but I've searched for documentation on the subject and can't find it! 回答1: The reason MyModel::all()->delete() doesn't work is because all() actually fires off the query and returns a collection of Eloquent objects. You can make use of the truncate method, this works for Laravel 4 and 5: MyModel::truncate(); That drops all rows from the table without logging individual row

laravel eloquent model cache

可紊 提交于 2020-06-09 04:11:04
问题 I am learning laravel eloquent in Laravel 5.6 . I have already used database query builder & cache. Retrieve & Store for Database Query Builder $value = Cache::remember('users', $minutes, function () { return DB::table('users')->get(); }); But i don't know how to cache for eloquent. Retrieve & Store for Eloquent $value = Cache::remember('users', $minutes, function () { return App\User::all(); }); Is it correct for eloquent or any other method existing? 回答1: There is a built-in method for this

Counting a many to many relationship?

独自空忆成欢 提交于 2020-06-01 08:28:50
问题 I have a many to many relationship between users and images. User Model public function image() { return $this->belongsToMany('\App\Image'); } Image Model public function user() { return $this->belongsToMany('\App\User'); } Tables users id | name images id | url image_user id | image_id | user_id When a user 'favourites' an image, it's stored in the pivot table. id | image_id | user_id 1 1 1 2 2 1 3 1 2 I need a count of each images favourites. I try something like: Image::with('user')->find

Laravel/Lumen/Eloquent - Update coretable::with($OneDimArrayOfTablenames) with multidimensional array

梦想的初衷 提交于 2020-05-31 06:11:53
问题 I want to write a function where a multidimensional array with the following structure: { "id_coretable": 1, "Internal_key": "UPDATED1", "extensiontable_itc": { "description_itc": "UPDATED1" }, "extensiontable_sysops": { "description_sysops": "UPDATED1" } } updates a multidimensional model/collection (i can create either of them, whatever the actual solution to this problem might require) with basically the same structure: { "id_coretable": 1, "Internal_key": "NOTYETUPDATED1", "extensiontable

Laravel/Lumen/Eloquent - Update coretable::with($OneDimArrayOfTablenames) with multidimensional array

冷暖自知 提交于 2020-05-31 06:11:08
问题 I want to write a function where a multidimensional array with the following structure: { "id_coretable": 1, "Internal_key": "UPDATED1", "extensiontable_itc": { "description_itc": "UPDATED1" }, "extensiontable_sysops": { "description_sysops": "UPDATED1" } } updates a multidimensional model/collection (i can create either of them, whatever the actual solution to this problem might require) with basically the same structure: { "id_coretable": 1, "Internal_key": "NOTYETUPDATED1", "extensiontable