Category : collections

In a controller, I can do this: $myentity = MyEntity::findOrFail($id); $collection = MyEntity::where("field", "=", $myentity->value)->get(); which gives me a collection of MyEntity objects… dd($collection); Collection {#331 ▼ #items: array:11 [▼ 0 => MyEntity {#332 ▶} 1 => MyEntity {#333 ▶} 2 => MyEntity {#334 ▶} 3 => MyEntity {#335 ▶} 4 => MyEntity {#336 ▶} ..

Read more

I’m struggling to grab specific date from an array of items. I want the ‘id’ of the minimum[‘lastUsed’] item. Example $getIdOfLastUsedItem = collect(//arrayOfItems) ->where(‘working’, true) // works great & filters working items ->min(‘lastUsed’) // returns 2 ->pluck(‘id’); // Error // arrayOfItems looks like this array:11 [ 0 => array:3 [ "id" => 6 "working" => ..

Read more

i want to get posts but not that post with specific key value i use below code for get my posts $col = Post::where(‘type’, $type)->whereNotIn(‘restrict’, [‘res1’])->orderBy(‘id’, ‘desc’)->paginate(24); it’s work but it also not return posts with null restrict value how i get posts Except some of theme with restrict key value == ‘res1’ ? Source: ..

Read more

I’ve 3 tables: Users: Schema::create(‘users’, function (Blueprint $table) { $table->unsignedInteger(‘id’)->unique(); $table->string(‘name’, 50)->nullable(); $table->timestamps(); }); Conversations: Schema::create(‘conversations’, function (Blueprint $table) { $table->bigIncrements(‘id’); $table->string(‘token’, 50)->nullable(); $table->timestamps(); }); And User_Conversation: Schema::create(‘conversation_user’, function (Blueprint $table) { $table->unsignedInteger(‘conversation_id’); $table->unsignedInteger(‘user_id’); $table->dateTime(‘read_at’)->nullable(); }); One conversation can have 2 users. I have this class for Users: class User extends Model { public function ..

Read more

I am using Laravel 7. I have Category model. I sent categories as API with laravel resources. But now I want to add "all" value to categories. Controller: ‘categories’ => CategoryResource::collection(Category::all()->push([ ‘id’ => 0, ‘name’ => "All", ‘subcategories’ => [] ])) Resource: public function toArray($request) { return [ ‘id’ => $this->id, ‘name’ => $this->name, ‘subcategories’ ..

Read more

$collection = collect([ [‘product’ => ‘1’, ‘price’ => 1], [‘product’ => ‘2’, ‘price’ => 2], [‘product’ => ‘3’, ‘price’ => 3], ]); print $collection->where(‘price’, ‘&’, 1)->pluck(‘product’)->implode(‘, ‘); It prints 1. But must print 1, 3. https://github.com/laravel/framework/blob/c6cf38139d2524a7c3accb606e3fb1b035c98d6a/src/Illuminate/Support/Traits/EnumeratesValues.php#L890 Why Laravel collections do not support all PHP operators? Sourc..

Read more

Laravel version: 5.7 I have one class Sessions. where I have used appends for the attribute. class Session extends Model { protected $appends = [‘session_type’]; public function getSessionTypeAttribute() { $startTime = $this->getOriginal()[‘start_time’]; return $startTime; } } My query: $sessions = Session::groupBy(DB::raw(‘YEAR(created_at), MONTH(created_at), visibility’)) ->select(DB::raw(‘COUNT(id) as total,MONTH(created_at) as month,MONTHNAME(created_at) as month_name,visibility’)) ->orderBy(‘created_at’, ‘ASC’) ->get(); Here I ..

Read more

For example, is it possible to have a Model that looks like this: class ModelWithoutDBTable extends Model { protected $fillable = [‘field1’, ‘field2’…]; } A collection that looks like this: $collection = collect( new AppModelWithoutDBTable([‘field1’ => ‘value1’, ‘field2’ => ‘value2’]), new AppModelWithoutDBTable([‘field1’ => ‘value3’, ‘field2’ => ‘value4′]), … ); And then query it by it’s ..

Read more

I have multi-dimension collection with categories: => IlluminateSupportCollection {#4379 all: [ AppCategory {#4492 id: 82, name: "Автоэлектроника и противоугонные системы", parent_id: 81, products_count: 0, sub: IlluminateDatabaseEloquentCollection {#4559 all: [ 10 => AppCategory {#4334 id: 83, name: "Видеоскопы", parent_id: 82, products_count: 1, sub: IlluminateDatabaseEloquentCollection {#4497 all: [], }, }, ], }, }, AppCategory {#4343 id: 59, ..

Read more

I’m struggling to get a groupby on a collection to work – I’m not getting the concept just yet. I’m pulling a collection of results from a table for a player the eloquent collection will have data like this: [‘player_id’=>1, ‘opposition_id’=>10, ‘result’=>’won’, ‘points’=>2], [‘player_id’=>1, ‘opposition_id’=>11, ‘result’=>’lost’, ‘points’=>0], [‘player_id’=>1, ‘opposition_id’=>12, ‘result’=>’lost’, ‘points’=>0], [‘player_id’=>1, ‘opposition_id’=>10, ‘result’=>’won’, ‘points’=>2], ..

Read more

I’m trying the following: I’m getting all clinic_tests related to my patients using the following function: public function getPatientsClinicTests(Specialist $id) { $patientClinicTests = $id->patients() ->with(‘PatientClinicTests’, ‘PatientClinicTests.Patient.User’) ->get() ->pluck(‘PatientClinicTests’) ->filter(function ($value) { return !empty($value); }); $result = []; foreach ($patientClinicTests as $array) { $result = array_merge($result, $array->toArray()); } return $result; } First group of code: $patientClinicTests ..

Read more

I doing sortBy function to sort but i can’t use it as sorted by For. $this->journey->journey_item = $this->journey->journey_items->sortBy(‘activation_date’); dump($journey_items); //// sorted list for($i = 0;$i < Count($this->journey->journey_item);$i++){ dump(“#”.$i.” “.$this->journey->journey_item[$i]->activation_date); } When it entered in For, it becomes as unsorted. Where am i doing wrong ? What can i do ? Also, I have created a ..

Read more

my transformer returns this: { “id”: “6”, “first_name”: “Marco” }, { “id”: “4”, “first_name”: “Michel” }, { “id”: “4”, “first_name”: “Marjorie” }, I would like my data like this: { 6: { “first_name”: “Marco” } }, { 4: { “first_name”: “Michel”, “first_name”: “Marjorie” } }, So I need to merge my array together where the ..

Read more

Hi i have problem with merge array, How to merge array from different array From Here Array 1 array:6 [ “patient_name” => “Pasien 4” “employee_no” => “1114” “birth_date” => “1990-05-02” “gender” => “L” “department_code” => “D0004” “section_code” => “S0004” ] Array 2 array:2 [ “kd_layan” => “10000104 ” “nama_layan” => “PAKET MCU ADVANCE (MALE)” ] ..

Read more

Reading Laravel 6 rows like {“filter_options”:[ {“id”:5,”key”:”category_id”,”value”:”2″,”created_at”:”2020-02-10 18:23:48″}, {“id”:4,”key”:”only_with_images”,”value”:”1″,”created_at”:”2020-02-10 18:23:48″} ] } I need to convert data in 1 object, like { category_id : 2, only_with_images :”1″ } I know how that could be implemented with a common circle, but are there some collection mapping methods for this? Thanks! Source: Laravel Sta..

Read more

I need to display a list of category label from a filtered collection seperated by comma in blade views. The code looks like this {{$data->categories->filter(function ($value) {return $value->type_id == 1;})->pluck([‘label’])->toArray()}}; This error is when I use toArray() htmlspecialchars() expects parameter 1 to be string, array given I need the result in array to use it ..

Read more