Laravel Eloquent: Access relationship of Original - laravel

In Laravel you can use getOriginal() on a model in order to get the original model (before changes since it was queried).
Now I need to access the relationship of that original model... is there a way to do that?
$item = OrderItem::where('id', $id)->with('qualification')->first();
$original_item = $item->getOriginal();
$original_item["qualification"] is not defined. I can access qualification_id though.

getOriginal() method returns array of the model's original attribute values, it is not a model itself, therefore you can not get a relationship.
So you can access the relationship using standard way: $item->qualification, that should not be affected by your changes of the parent model.

Related

The right way to receive related data in Laravel?

Here are some models:
UserModel
SpecializationModel
UserSpecializationModel
I need to recieve authorized user's specialization. I can do that:
$specializations = UserSpecialization::where("user_id", Auth::user()->id)->get();
Also I can do this through the UserModel model using relation hasMany() specializations().
When to use first case and the second?
$specializations = Auth::user()->specializations();
Do I need a model UserSpecializationModel?
In general you don't need UserSpecilizatonModel, in most situations you wont access data directly from that table, you'll either do it through user or specialization model.
Check also https://laravel.com/docs/9.x/eloquent-relationships#retrieving-intermediate-table-columns for accessing data from pivot table.

get laravel eloquent model relationships

is there any way to get the defined relationships in eloquent model. I have a situation where I need to get the model relationships so I can update all other eloquent models that relies on a specific id before delete it
There's no unified method to iterate over all registered relationships of a class. You can, however, access all the currently loaded relationships of a model instance (via the ->relations attribute or the getRelations() method), but that's not what you're up to. I'd suggest you take a look at laravel's documentation on inserting and updating relationships. So far that's the best laravel provides out of the box, the rest is developing approaches.
Try this function:
public function getRelations()
You can use
$model->getRelations()
function to get all relations
Also refer below link for details https://laravel.com/api/5.7/Illuminate/Database/Eloquent/Concerns/HasRelationships.html#method_getRelations

How to get columns of a model instance in Laravel?

Can I extract a specific column for a model instance in Laravel instead of using query methods statically? more clearly, instead of writing the code below:
MyModel::select('columnName')->where('id', $model->id)->first();
can we write?
$model->select('columnName');
Well, when you grab it from a model class, it is a Eloquent Object, and it has all attributes from the database.
But if you are talking about serializing that into an array or JSON everytime, then you need to modify your $visible array ;
if you wanna transform it for a specific case, then you need to do :
MyModel::select('columnName')->where('id', $model->id)->first()->pluck('columnName');

Eloquent model setRelation generating array instead of Collection

If you are doing $instance = $model->with('categories')->find($id); and after var_dump($instance->categories) it going to return Collection of categories.
But on the project I'm working on in some heavy queries, we are not using with and getting data with a combination of GROUP_CONCAT and CONCAT, like this:
\DB::raw('GROUP_CONCAT(DISTINCT CONCAT(categories.id, ",,", categories.name) SEPARATOR ";;") as categories'),
And then we are building relations manually parsing result and creating a relationship using $instance->setRelation($relation, $data) but for some reason, it's returning an array of objects instead of Collection.
There are also option to use setRelations() and this method returning Collection but I found if you have bidirectional relations it's creating recursion and working really slow. For example: if in User model we have set $this->hasMany('Comments') and in Comments model we have set return $this->belongsTo('User'); and after when we running setRelations() to manually build relations it is create nesting models with recursion (User->Comments->User and so on).
The third option is to not use setRelation() or setRelations() and just to manually create Collection, populating it and set to model. But in such case, it will not be set as a model relation.
Any suggestions on how to build manually in the right way (to create relation is same way eloquent creating with with).
Group return collection of collection so you have to remove the keys of first collection and for that you can use values function of collection like this
$instance->setRelation('relation', $data->values()->all());
Details https://laravel.com/docs/5.6/collections#method-values

How do I add an array of pivot table data to a user?

I have a pivot table of user_activities. I already have the relationship defined in the model, for example:
$activities = User::find($id)->activities;
This returns an array of objects. I want to send the user object with the activities array.
I've tried dynamically assigning this activities array to the user object but I only get an empty object as a result ($user->activities = {}) instead of the array full of activity objects. How do I add this array of activity objects to the user object?
You may try following approach (your approach should work, related models will be loaded later on call (dynamically) but this is better, known as eager loading):
$user = User::with('activities')->find($id);
Make sure you have declared the relationship properly and have related models as well.

Resources