Laravel - dd() model with all relationships - laravel

Is there a way to die & dump the Model and see all relationships with dd() or var_dump()?
So i can see everything what is connected to it?

If you load model with relation, for example:
$collection = Model::with('relation', 'nested.relation')->get();
When you'll do dd($collection); you'll see all relation models there.

Related

use model belongsTo two Ids in laravel for relationship

i have two table
media and trainingVideo.
training video having two column video_id and second_video_id
videos store in media table.
i want two relationship both video in trainigtable.
//Model
// trainingModel
public function media()
{
return $this->belongsTo(Media::class, ['video_id', 'second_video']);
}
// Media model
public function trainingVideos()
{
return $this->hasMany(TrainingVideo::class, 'id');
}
$a = TrainingVideo::with('media')->get();
i want both video in this relationship.
please help me to solved this.
Answer
Syntax:-
$model = Model::with('relatedModel', 'relatedModelTwo')->get();
So in your case, it can be something like this.
$a = TrainingVideo::with('media','trainingVideos')->get();
When you dd($a), you should see the related models in the relations array attribute.
To access the relations' properties from there, it is simply
$a->media->media_Attribute
$a->trainingVideos->trainingVideos_Attribute
Laravel Relationship Doc :- https://laravel.com/docs/5.1/eloquent-relationships#one-to-one
use model belongsTo two Ids in laravel for relationship
https://stackoverflow.com
Eloquent: Relationships - Laravel - The PHP Framework For Web Artisans
https://laravel.com

What is difference between relation with ->profiles() and ->profiles in Laravel?

What is difference between relation with->profiles() and ->profiles in Laravel?
->profiles returns a collection of the related model(s)
->profiles() returns an instance of the relationship which is handy if you want to update related models
Example:
$posts = $user->posts; // model -> collection
$posts = $user->posts()->get(); // model -> relation -> collection
Read more here: https://laracasts.com/discuss/channels/eloquent/dynamic-property-vs-method-in-eloquent-orm

laravel 5.1: get relation in with

I have a model called user
with relation "travel" model
and travel model with relation "destination"
how can I get user in eloquent with travel and relation.
I know that I can get travel like this :
$row = $this->model
->find($reqId)
->with('travel')
->first();
I want get destination of travels .
If I understood your question correctly, you want to use nested eager loading:
$row = $this->model->with('travel', 'travel.destination')->find($reqId)

Laravel Eloquent Lazy Eager Load Count

I'm ideally looking for a function like
load('relationship')
but which loads a count in the same way
withCount('relationship')
works for eager loading.
I'm thinking it is going to be called loadCount('relationship')
loadCount() is available since Laravel 5.8
$post->loadCount('comments');
$post->comments_count;
Docs
As of Laravel 5.2, this functionality is built-in.
Provided you have a hasMany relationship between Post and Comment, do:
<?php
$posts = App\Post::withCount('comments')->get();
foreach ($posts as $post) {
echo $post->comments_count;
}
You can even eager load relationships count by default by declaring this in your model:
<?php
// Post model
protected $withCount = ['comments'];
This solution works great for me:
Create a new hasOne relationship on the related model and add a raw select to the query for the count. For example, if you want to eager load the number of tasks for a given user, add this to the User Model:
public function taskCount()
{
return $this->hasOne('App\Task')
->selectRaw('user_id, count(*) as count)
->groupBy('user_id');
}
And then eager load the count like this:
$user = User::where('email', $email)->with('taskCount')->first();
And access the count like this:
$taskCount = $user->task_count->count;

Laravel mass update

yesterday i asked a question about Eager Loading and Form Model Binding.
Laravel Eager Loading and Form Model Binding
This is somehow a followup question.
Now i want to update the record in the Database
$user = \User::findOrFail($id);
$user->fill(\Input::all());
$user->push();
But this dose only save the data of the user itself. Not the relations
user = saved
user->profile = not saved
on my user model i have a fillable array with all fillables columns. and on other models like the profile model i just wrote protected $fillable = ['*'];
The Soultion:
call fill() on the relations not just the user model.
$user->fill($input)
$user->profile->fill($input['profile'])
You're not retrieving profile with your user model.
$user = \User::with('profile')->findOrFail($id);
That should give you the profile properties to update.

Resources