Will laravel reconnect to the database for any where on returned model collection? - laravel

If we have a find like this:
$returnedModel = Flight::
with("passengers")
->find(1);
Now if I add a where on returnedModel like this:
$returnedModel->passengers()->where("name" , "hamid")->first();
Will Laravel reconnect to the database for any where on returned model collection?
I added DB::getQueryLog() and there is a query with this where! how can I get this without reconnect to database?

You need to access your passengers as a property, not a method, by the way you get a Laravel collection already loaded from the DB (thanks to the with eager-loading).
$returnedModel->passengers->firstWhere('name', 'hamid');

Related

how to change record from db before sending it to response in laravel model

I want to fetch a record(price) from db table and when sending it to api response I want to change it inside my model according to some conditions, I have searched and not found any relevant solution to this.
Try something like this?
$record = ModelName::find($id);
$record->price = $newValueBasedOnCondition;
$record->save();

what is difference between model->name and model() in laravel?

I want to know the difference between laravel eloquent conisgnment->runsheet->name and $consignment->runsheet()->name
i notice that there is performance difference but why? also I notice that when I try to get relationship property like $consignment->runsheet->name it work fine but $consignment->runsheet()->name doesn't work
The first one
$consignment->runsheet
retrieve a collection (so it's already made a query to DB)
while the second
$consignment->runsheet()
is a query builder (hasn't made a database query) you can chain it like any query builder instance
so if you want to get a property from the second one you can do it like:
$consignment->runsheet()->first()->name;
OR
$consignment->runsheet()->value('name');

Laravel pluck with paginate

So, I've a ERM with two database connected in a same laravel app. I'm trying to pull up all the IDs with pluck which is working fine but not being able to paginate. For an instance,
$newsTagSaved = NewsTag::latest()->pluck('post_id');
This works just as fine. But this pulls up way more information than I need. I'm looking for something like:
->pluck('id')->paginate(12);
This way I'm not loading data in ajax that I don't need since it will query unnecessary data on every ajax call.
Thank you!

October CMS overriding list query

In backend I have a list of records. And this list displays all records of this type. It is possible to override this query?
In controller I have Eloquent query and i grab some data from database,
and i set this data in $query_data variable. Howe push this data in list view?
unfortunately, I didn't get exactly your point of telling override query, if you mean that set some condition in fetching data from database in Eloquent you have to use where for example in the example below we have users which their gender is male:
$users=User::where('gender','male')->get();
for set more condition you can do like this:
$users=User::where([['gender','male'],['status',1]])->get();
and for showing the variable in your view you can use compact method:
return view('YOUR BLADE FILE NAME',compact('users'));
hope be helpful bro

Laravel Baum how to get related models on getAncestorsAndSelf() method

Using Laravels Baum package for nested sets I try to fetch the relational model on getAncestorsAndSelf() but does not work
$tree = \App\GroupDeprecator::where('id', $this->team_id)->with('lead')->first();
dd($tree); //this one shows the user object
dd($tree->getAncestorsAndSelf());//this one does not shows the user object
What went wrong in this case?
How you solved it? the code looks fine and should work, unless it has been manually updated in the database, I would do:
GroupDeprecator::rebuild(true)
to update the lft, rgt and depth columns

Resources