laravel-collection

Laravel 5.5 Collection where like

好久不见. 提交于 2021-02-05 11:13:24
问题 i am filtering data using collections. But i need to use like method. I had tried to write like this : ('name', 'LIKE', '%value%') but it did not work. Here is my method : protected function filterData(Collection $collection, $transformer) { foreach (request()->query() as $query => $value) { $attribute = $transformer::originalAttribute($query); if (isset($attribute, $value)) { $collection = $collection->where($attribute, $value); } } return $collection; } 回答1: The 1st question is whether you

How to sort a collection of UTF-8 strings containing non-Latin chars in Laravel 5.3?

假如想象 提交于 2020-07-08 13:04:42
问题 Folks, I want to sort following nested collection by string alphabeticaly : $collection = collect([ ["name"=>"maroon"], ["name"=>"zoo"], ["name"=>"ábel"], ["name"=>"élof"] ])->sortBy("name"); I would expect : 1=> "ábel" 2=> "élof" 3=> "maroon" 4=> "zoo" I got instead : 1=> "maroon" 2=> "zoo" 3=> "ábel" 4=> "élof" I seen some PHP threads for this, but I am curious if there is any Laravel workaround for this. Thanks. 回答1: Here's a Solid way to do it: $blank = array(); $collection = collect([ [

Laravel grouped collection returns object instead of array

*爱你&永不变心* 提交于 2020-06-17 09:20:47
问题 I have the following query $outings = Outing::all()->groupBy(function ($item) { return Carbon::parse($item['start'])->format('m/d/Y'); }); return response()->json([ 'outings' => $outings ], 200); The response is returning an object and I need it to return an array How can I get outings to be an array instead of an object. If I don't group the collection and just do Outing::all(); It will return an array rather than an object. The Group by is doing something weird. If I DD($outings) it does in

Laravel grouped collection returns object instead of array

落爺英雄遲暮 提交于 2020-06-17 09:20:06
问题 I have the following query $outings = Outing::all()->groupBy(function ($item) { return Carbon::parse($item['start'])->format('m/d/Y'); }); return response()->json([ 'outings' => $outings ], 200); The response is returning an object and I need it to return an array How can I get outings to be an array instead of an object. If I don't group the collection and just do Outing::all(); It will return an array rather than an object. The Group by is doing something weird. If I DD($outings) it does in

Laravel: Count integer value based on week of the month

a 夏天 提交于 2020-03-25 16:05:34
问题 I'm trying to count integer value from JSON: https://pomber.github.io/covid19/timeseries.json and got what I expected. { "date": [ "22 Jan", "23 Jan", "24 Jan", "25 Jan", .... ], "total_confirmed": [ 555, 653, 941, 1434, .... ], "total_deaths": [ 17, 18, 26, 42, .... ], "total_recovered": [ 28, 30, 36, 39, .... ], "max_value_of_total_confirmed": 272166 } Here's my controller: $client = new Client();$request = $client->get('https://pomber.github.io/covid19/timeseries.json'); $response =

Laravel using Sum and Groupby

♀尐吖头ヾ 提交于 2019-12-31 03:01:42
问题 I would like to fetch sum of quantity in each month, so that I can display on bar chart quantities against month This is what I thought but didn't workout $data1 = Borrow::groupBy(function($d) { return Carbon::parse($d->created_at)->format('m')->sum('quantity'); })->get(); My table structure Schema::create('borrows', function (Blueprint $table) { $table->increments('id'); $table->integer('member_id'); $table->integer('book_id'); $table->integer('quantity'); $table->integer('status')->default

Laravel using Sum and Groupby

本小妞迷上赌 提交于 2019-12-31 03:01:26
问题 I would like to fetch sum of quantity in each month, so that I can display on bar chart quantities against month This is what I thought but didn't workout $data1 = Borrow::groupBy(function($d) { return Carbon::parse($d->created_at)->format('m')->sum('quantity'); })->get(); My table structure Schema::create('borrows', function (Blueprint $table) { $table->increments('id'); $table->integer('member_id'); $table->integer('book_id'); $table->integer('quantity'); $table->integer('status')->default

How to check if something is countable?

こ雲淡風輕ζ 提交于 2019-12-23 07:53:51
问题 I have a var: $a . I don't know what it is. I want to check if I can count it. Usually, with only array, I can do this: if (is_array($a)) { echo count($a); } But some other things are countable. Let's say a Illuminate\Support\Collection is countable with Laravel: if ($a instanceof \Illuminate\Support\Collection) { echo count($a); } But is there something to do both thing in one (and maybe work with some other countable instances). Something like: if (is_countable($a)) { echo count($a); } Does

How to check if something is countable?

三世轮回 提交于 2019-12-23 07:53:16
问题 I have a var: $a . I don't know what it is. I want to check if I can count it. Usually, with only array, I can do this: if (is_array($a)) { echo count($a); } But some other things are countable. Let's say a Illuminate\Support\Collection is countable with Laravel: if ($a instanceof \Illuminate\Support\Collection) { echo count($a); } But is there something to do both thing in one (and maybe work with some other countable instances). Something like: if (is_countable($a)) { echo count($a); } Does

Laravel pluck fields from relations

為{幸葍}努か 提交于 2019-12-17 10:59:07
问题 I have a Seller object which has a related User. I need to fill a select from LaravelCollective so I need to make something like this: {!! Form::selectGroup('seller_id', 'Seller', Seller::with('user')->pluck('user.first_name', 'id')->toArray(), null) !!} The problem is that I cannot take fields from relationships (user.first_name). How can I do it? UPDATE I want to avoid doing this... <?php $sellers = []; Seller::with('user')->get()->each(function ($seller) use (&$sellers) { $sellers[$seller-