Except doesn't work with Laravel Collections - laravel

I have the following code:
$object = Object::with("prototypes.fields")->findOrFail($id)->get();
$object_copied = $object->except(['id', 'prefix', 'prototypes']);
dd($object_copied->all());
Last line returns collection with fields that should be except: 'id', 'prefix', 'prototypes'

The first thing:
$object = Object::with("prototypes.fields")->findOrFail($id)->get();
This is probably wrong.
You should either use:
$object = Object::with("prototypes.fields")->findOrFail($id);
or
$object = Object::with("prototypes.fields")->get();
The second thing is what you really want to achieve. except method might not be what you really want to use here if you want to get only some columns. In this case better option would be using select when getting data from database or maybe using map method.
Assuming in $object you have collection of object using except you will remove only some object from method (those with given keys) and keys in those collection will be numeric 0, 1, ... x so you should pass only numerical keys here if you want to not include first model in collection.

Try $object_copied = collect($object )->except('id', 'prefix', 'prototypes');

Related

Laravel Collection Filter breaking serialization format

I have a serialized String like this
$string = '[{"name":"FOO"},{"name":""},{"name":"BAR"}]';
I am trying to process it via Laravel Collection's filter method and eliminate items without a defined "name" property.
$collection = collect(\json_decode($string));
$collection = $collection->filter(function($v){
return !empty($v->name);
});
$string = \json_encode($collection->toArray());
dd($string);
Normally I am expecting something like this:
[{"name":"FOO"},{"name":"BAR"}]
But I'm getting something like this:
{"0":{"name":"FOO"},"2":{"name":"BAR"}}
Funny thing is, if I skip the filtering process or return true every time, I keep getting the string in the desired format. Removing the toArray() call has the same result. I don't want to keep the numeric indices as associative object keys.
Why this anomaly? And what should I do to get the serialized data in desired format?
In PHP arrays the index key must be unique.
In your case you have the key 'name' and collection automatically assigns the index key to all items in the collection.
To overcome that problem just call
$string = \json_encode($collection->values());

Best approcah for getting the object in the foreach loop in laravel using eloquent?

I have some properties and i want to get the object for each property, currently, I am using the eloquent in the foreach loop like this as shown in the image that will describe the best..
but it is really not a good approach because if I have 100 published property I will making 100 calls to the DB... and that is not correct?
I need a suggestion and a proper solution to this query?
Thanks in advance
Before foreach you can get all the RentalProperty items from db like this:
$allRentalProperties = RentalProperty::all();
and in foreach loop you can get those items without connecting database like this:
$propertyObj = $allRenatalProperties -> where('id', $property['id']) -> first();
Also, you can use array shorthand.
$newArray = [];
it's much simple and readable.
You can do array pluck before loop:
$propertyIds = Arr::pluck($published_properties, 'id');
Then you can make a whereIn query to get only those data which are in $published_properties object
$propertyObj = RentalProperty::whereIn('id', $propertyIds);
Then you can access that object with id if you change array key with record id.

Laravel 'where' collection method modifies the collection

Does the Laravel wherecollection method modify the collection?
On the Laravel documentation you can read this:
almost every method returns a new Collection instance, allowing you to preserve the original copy of the collection
And the only methods which have a warning about modifying the collection are transformand forget
But I have this code:
$descriptions = Description::where('description', $request->description);
if ($descriptions->count()) {
$descriptionsWithSameUnit = $descriptions->where('unit_id', $request->unit);
if ($descriptionsWithSameUnit->count()==0) {
$descriptionsWithAnotherUnit = $descriptions->where('unit_id', '!=', $request->unit);
if ($descriptionsWithAnotherUnit->count()) {
...
And the collection IS modified after the first where so $descriptionsWithAnotherUnit is always empty, because on that point the collection only has the records where unit_id == $request->unit. Is this a bug in the framework or in the documentation?
And the question that arises from here: What can I do to keep a copy of the original object without retrieving again from the DB? I tried this:
$descriptions = Description::where('description', $request->description);
if ($descriptions->count()) {
$descriptionsWithSameUnit = $descriptions;
$descriptionsWithSameUnit->where('unit_id', $request->unit);
...
But the $descriptions object is also modified when I apply the where method to the $descriptionsWithSameUnit object
The first thing is that to get collection you need to use get, so if you want to get collection, you should do:
$descriptions = Description::where('description', $request->description)->get();
and not only:
$descriptions = Description::where('description', $request->description);
Second thing is that there's no possibility to use operator using where method on collection, so:
$descriptionsWithAnotherUnit = $descriptions->where('unit_id', '!=', $request->unit);
is completely incorrect. You should use filtermethod here.

Laravel Queries: 'lists' method

The 'lists' method surely does retrieve a list of column's values, but I want the method to retrieve the values but without duplication, how to achieve that?
$types = DB::table('events')->lists('type');
If you need to get just distinct values, you should do:
$types = DB::table('lists')->distinct()->lists('type');

Doctrine toarray does not convert relations

I followed doctrine documnetation to get started. Here is the documentation.
My code is
$User = Doctrine_Core::getTable("User")->find(1);
when I access relations by $User->Phonenumbers, it works. When I convert User object to array by using toArray() method, it does not convert relations to array. It simply display $User data.
Am I missing something?
By using the find method you've only retrieved the User data which is why the return of toArray is limited to that data. You need to specify the additional data to load, and the best place to do this is usually in the original query. From the example you linked to, add the select portion:
$q = Doctrine_Query::create()
->select('u.*, e.*, p.*') // Example only, select what you need, not *
->from('User u')
->leftJoin('u.Email e')
->leftJoin('u.Phonenumbers p')
->where('u.id = ?', 1);
Then when toArray'ing the results from that, you should see the associated email and phonenumber data as well.
I also noticed an anomaly with this where if you call the relationship first then call the ToArray, the relationship somehow gets included. what i mean is that, taking your own eg,
$User = Doctrine_Core::getTable("User")->find(1);
$num= $User->Phonenumbers->office; // assumed a field 'office' in your phone num table
$userArray = $user->toArray(true);
In the above case, $userArray somehow contains the whole relationship. if we remove the $num assignment it doesn't.
am guessing this is due to doctrine only fetching the one record first, and it's only when you try to access foreign key values that it fetches the other related tables

Resources