What is difference between relation with ->profiles() and ->profiles in Laravel? - 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

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

How to use OrderBy in Laravel using eloquent relationship?

I have two model: Category and PostAd model.
They two have relation with each other.
In laravel view i have passed data through Category Model to display PostAd data.
I want to apply OrderBy condition in query but it doesn't work
Code
$data['category'] = Category::with(['postads'])->where('id',$id)->get();
I want to do this
$data['category'] = Category::with(['postads'])->where('id',$id)->orderBy('adtitle','DESC')->get();
But it doesn't work because it is Category Model.
How can i solve this.
if you want to get PostAd data with an order by then you can do an eager load Order by.
And here you're using get() method so it should be first(); because ->where('id',$id) always get one record.
$data['category'] = Category::with(['postads' => function($query) {
$query->orderBy('adtitle', 'DESC');
}])->where('id',$id)->first();

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 - dd() model with all relationships

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.

Get pivot from two model instance

I have a question. Activity model and User model have many-to-many relationships. I know I can get pivot model like this:
$activities = $user->activities;
foreach($activities as $activity)
{
$pivot = $activity->pivot;
}
But now I already have two model instance:$user and $activity .Get them by id individually. Not access from their relationship. So I want to know is there a method to get their pivot model?
Its not necessary for you to call the relationship chain to access pivot, you can query individual model and access pivot too,
$model = Activity::find(1); //get the activity model by primary key
$model->pivot->activityId;

Resources