eloquent

Laravel 5 + Eloquent toJson/toArray causes Strange Segmentation Faults

﹥>﹥吖頭↗ 提交于 2021-02-19 02:28:51
问题 I hate to be answering my own question, so maybe you can help me find what fixed this. I have some eloquent models which belongTo each-other, and I set them up via association like this. It's all normal stuff. This process unfortunately causes $device to work erratically. Below you can see individual values are accessible but any form of jsonification destroys the server without error. $device = $truck->device; if(is_null($device) || empty($device)) { $device = new Devices; } $device->truck()

Laravel flatten and pluck from multidimensional collection

我的未来我决定 提交于 2021-02-19 02:00:14
问题 I have to retrieve just an array of id from the given collection, something like [10,54,61,21,etc] . I've tried flatten , pluck , but nothing seems to work apart from a foreach which is something I would like to remove at this step. // Model class Children extends Eloquent { public function directChildrens(){ return $this->hasMany('App\Children','father_id','id')->select('id','father_id'); } public function childrens(){ return $this->directChildrens()->with('childrens'); } } // Controller

Laravel “undefined method Illuminate\Database\Query\Builder::attach()”

筅森魡賤 提交于 2021-02-18 20:50:46
问题 I'm trying to associate related models during database seeding in Laravel 4. According to the documentation here, I can do it like this: $user->roles()->attach(1); So, in my database seed I'm running: $package = Package::create([ 'name' => $faker->word, 'summary' => $faker->sentence, 'base_price' => $faker->randomFloat(2, 200, 10000) ]); // Attach 1-5 randomly selected items to this package foreach(range(1, 5) as $index) { $randomItem = Item::orderBy(DB::raw('RAND()'))->first(); $package-

Case-sensitive where statement in laravel

女生的网名这么多〃 提交于 2021-02-17 15:17:37
问题 How can I do a case-sensitive string match with laravel? SELECT * FROM `invites` WHERE `token`='OGwie2e2985tOEGewgu23hUFs' Can be done as Invite::where('token',$token)->first() If I want a case-sensitive match I need to use a statement like this (or similar, as far as I know): SELECT * FROM `invites` WHERE BINARY `token`='OGwie2e2985tOEGewgu23hUFs' My best guess would be: Invite::whereRaw("BINARY `token`='{$token}'")->first() but then my input is not going through a prepared statement, right?

How to get data from parent and child table on the basis of status where foreign key have different status for every row

此生再无相见时 提交于 2021-02-17 06:52:18
问题 I have 2 tables with foreign key relationship. Situation is I have a case and a case have many revision s and every revision have its own status . I want to get parent table data and child data if only specific row of foreign key table status is changed Table Case id case_no patient_name age 1 12564 abc 78 2 1256 lkj 63 3 125 bdhf 23 Table Case_Revisons id case_id revison status 1 1 0 assesemnt 2 1 1 assesment 3 1 2 treatment 4 2 2 assesment 5 3 1 assesment What I want is all data all data

How to append or attach a pivot relationship to a model?

穿精又带淫゛_ 提交于 2021-02-11 15:37:33
问题 I am trying to get the practice area record of lawyers from practice_areas table along side each time I get a lawyer's record from lawyer_profiles_table. This two tables (practice_areas and lawyer_profiles) are related with a pivot table named (lawyer_practice_area) This code below are from the LawyerProfile Model public function practice_areas() { return $this->belongsToMany(PracticeArea::class, "lawyer_practice_area", "practice_area_id", "lawyer_id"); } I tried getting it through an

How to use order by query both with current table column and relationship table column in laravel?

笑着哭i 提交于 2021-02-11 14:45:24
问题 i am trying to fetch record with order by query but situation is this that i have to use order by both on current table column and relationship table column in laravel . I tried this $consignments = Consignment::where('delivery_run_id', $id)->whereIn('status', [ 'In Warehouse', 'With On Forwarder', 'In Transit (Warehouse->Delivery)', 'Awaiting Pickup' ])->with(['consignment_run_sheet' => function ($query) { $query->orderBy('run_sheet_id'); }])->orderBy('delivery_date', 'DESC')->get();

Unable to get and display grandchild relationship for dynamic menu in laravel 5.8

被刻印的时光 ゝ 提交于 2021-02-11 13:41:48
问题 I am creating dynamic multilevel menu in laravel, I am able to get parent and child relationship and display the menu but unable to get and display if relationship goes one more level down. Please assist. My Model: public function parent() { return $this->belongsTo('App\Menu', 'parent_id'); } public function children() { return $this->hasMany('App\Menu', 'parent_id'); } public function gchildren() { return $this->hasMany('App\Menu', 'parent_id'); } My Nav View: <ul class="navbar-nav">

Laravel | Polymorphic Relationship unknown column

早过忘川 提交于 2021-02-11 13:41:44
问题 This is my first time using a polymorphic relationship. I am creating a LMS where a Assignment can be allocated to a individual user or a team so reading the Laravel docs it seems that the Polymorphic Relationship will be a good way to go about it. I have created 4 tables. Users: | id | username | password | created_at | updated_at | Teams: | id | friendly_name | slug | Team User: | id | user_id | team_id | Assignment Allocation: | id | assignment_id | assignmentable_type | assignmentable_id

Is it possible to order by the total count of multiple tables?

牧云@^-^@ 提交于 2021-02-11 12:29:49
问题 I can't get this query to work! return Post::selectRaw('likes_count + comments_count as total_count') ->withCount(['likes', 'comments']) ->groupBy('posts.id') ->orderByDesc('total_count') ->paginate(); It works this way below but I want all the counts and something performant! return Post::leftJoin('likes', function ($join) { $join->on('posts.id', 'likes.likable_id') ->where('likes.likable_type', (new Post)->getMorphClass()); })->leftJoin('comments', function ($join) { $join->on('posts.id',