eloquent

Laravel chunk returns null

泪湿孤枕 提交于 2019-12-24 17:07:18
问题 I'm needing to chunk my query as it's making PHP run out of memory, however the below code just dumps null : $chunked = $query->chunk(25, function ($events) { //dd($events); }); dd($chunked); However, when I do the below, it dumps the first chunk of 25: $chunked = $query->chunk(25, function ($events) { dd($events); }); //dd($chunked); No combination of changing dd($events); to the likes of return $events , return true , iterating over each item in each chunk and returning that - works. Am I

Retrieving records from database using eloquent with optional query parameters

一世执手 提交于 2019-12-24 16:15:25
问题 i have the following block of code in my Resource Controller: $travel_company_id = Input::get('travel_company_id'); $transport_type = Input::get('transport_type'); $route_type = Input::get('route_type'); $travelRoutes = TravelRoute::where('travel_company_id', $travel_company_id) ->where('transport_type', $transport_type) ->where('route_type', $route_type) ->get(); Now what this does is it gets travelRoutes based on the parameters supplied. What i want is for it to do is perform a search based

Laravel / Eloquent eager loading

烂漫一生 提交于 2019-12-24 15:49:28
问题 This will be an easy question for some, but I can't seem to find the answer online or in the documentation (only variants of it which I don't want). Lets say we have a Question class Each Question object can optionally have multiple Tag s I have set the classes up and things behave as expected. Currently I use: $questions = Question::all(); This works as expected, however it does not eager load. To be clear: $question->tags gives the array I am expecting. There is not a relationship problem.

Is it bad practice for a model to call itself within itself?

怎甘沉沦 提交于 2019-12-24 14:38:07
问题 Here's an example, using Eloquent in Laravel. Let's say I'm working on a CMS. the controller takes the route and looks up the page via the route. the model provides a static function that uses the route to figure out the id of the row it's looking for the model then uses itself to perform the database query and returns the result Example Controller Code: Route::get('(.*)', function($route) { $page = Page::load_by_route($route); }); Example Model Code: class Page extends Eloquent { public

Operation inside the view in the laravel

懵懂的女人 提交于 2019-12-24 14:20:17
问题 Here is my controller i am taking the blog details from the Blog model and passing it through BlogData Here is the controller $BlogData = Blog::where('BlogUrl', $data)->get(); return View::make('blogview')->with('BlogData', $BlogData); In the view @foreach ($Blogs as $Blog) {{ $Blog->BlogTitle }} {{ $Blog->BlogTag }} @endforeach The Question is i have the BlogTag coloumn as 1,3,4 which means it includes First, Second and Third Tag How can i fetch the Title of the Tag from the Tag Model Note :

Using polymorphic relationships in Eloquent to extend model

此生再无相见时 提交于 2019-12-24 14:07:52
问题 I am quite new to Slim, still trying to learn it and decided to redo an old app I'd made. I am trying to use Eloquent but have quickly gotten lost doing what I wouldn't think is very complicated. The app I had was even too complicated to learn on, so I backtracked to this tutorial here, as this is more or less what I am trying to do, use models extending one other class, to see if I can even get this working: http://www.richardbagshaw.co.uk/laravel-user-types-and-polymorphic-relationships/ It

How to use the array intersect method to match two values within 2 sets of arrays

拟墨画扇 提交于 2019-12-24 13:42:25
问题 I have this query: $subcommon= MyModel::selectRaw('subjectlist_id, count(subjectlist_id) AS aggregate') ->whereIn('user_id', [Auth::id(), $user->id]) ->groupBy('subjectlist_id') ->having('aggregate','>',1) ->get(); which gives this query result: {"subjectlist_id":1,"aggregate":2} {"subjectlist_id":3,"aggregate":2} I also have this query: $values = Mysecondmodel::where('user_id', Auth::id()) ->first(); which gives this result: {"id":1,"subjectlist_id":1, "article_id":4} Because im finding it

Force eloquent to use mutators during serialization

守給你的承諾、 提交于 2019-12-24 13:33:39
问题 Is there a way to force eloquent to use mutators when serializing data? I am currently converting my app to use vue.js and several fields are computed within the model, which I need to be included, in the serialized data. Eloquent\Model - make toArray() utilize mutators I understand why Taylor does not honour mutators but is there a way to override this behaviour? 回答1: Before returning the model, you can do this $model->setAppends(['mutator_1', 'mutator_2']); return $model->toArray(); if you

Laravel 5.3 eloquent 3 table join using models

泄露秘密 提交于 2019-12-24 12:20:05
问题 I read this post which helped, but need more guidance please. I need a set of Results for a particular $batteryid (on Result) and a particular $age (on Test model) and a particular $gender (from Athlete). Each Test belongs to one athlete. Each test has many results. Model Battery: class Battery extends Model{ public function results(){ return $this->hasMany('App\Result'); } } Model Result: class Result extends Model{ public function test(){ return $this->belongsTo('App\Test', 'test_id'); }

Laravel 5 - Elequent GROUP BY is failing

那年仲夏 提交于 2019-12-24 11:43:01
问题 I am trying to do the following: I have two tables: 1) Content id, section_id parent_id, sequence, 2) Sections id, title, description, date_entered Each Content has to have a section, which is defined by a foreign key, the content can have a sub section, where if the content have the same parent_id - then this is classed as a sub section.. So for example: 1. My first section 1.1. My first sub section 2. My second section 3. My third section 3.1 My third sub section I am using Eloquent and