Laravel nested `hasOne` relationship with `where` clause - laravel

I have Shop model which has multiple Content entries:
public function contents(): HasMany
{
return $this->hasMany(Content::class);
}
And I had hasOne relationship with one specific type of content like this:
public function widgetContent(): HasOne
{
return $this->hasOne(Content::class)->where('type', Content::WIDGET);
}
It was working fine but I moved Content's type into a new model called ContentType, and removed the type field from Content model, but instead added another hasOne relationship via content_type_id field.
Now, I'm trying to achieve the same widgetContent relationship but I can't. Here's my code:
public function widgetContent(): HasOne
{
return $this
->hasOne(Content::class)
->whereHas('contentType', fn ($q) => $q->where('type', ContentType::WIDGET));
}
What should I do to achieve this? Also, if this is not the best practice (that's how I feel), what is the best practice?

You could create a closure and use that in stead. So you would still have the relationship like this:
public function content(): HasOne
{
return $this
->hasOne(Content::class);
}
But then you add a closure in your model:
public function getWidgetContent()
{
return $this->content()->whereHas('contentType', function(Builder $q){
$q->where('type', ContentType::WIDGET)
})->get();
}
And then you just call the closure function when you need the results.

Related

access relation inside hasMany relation

I have a model request.php has this relation :
public function requestItems(): HasMany
{
return $this->hasMany(RequestItemd::class, 'request_id', 'id');
}
inside this relation there is another relation
public function item()
{
return $this->belongsTo(Item::class, 'item_id', 'id');
}
how to reach to belongsTo relation and retrieve data through the original hasMany relation?
You will need to loop over your items or get the individual item, something like this:
$requestItemsQuery->each(function($reqestItmId) {
$item = $reqestItmId->item;
});
there are a few other ways of doing this though, so if you share how you are planning to use the reference i might be able to give a better answer

How to disable loading of relationships when not needed in Laravel

is it possible to disable the loading of relationships, but only in some cases?
Here are my models:
class League extends Model
{
...
public function country()
{
return $this->belongsTo(Country::class)->with('translations');
}
}
class Country extends Model
{
...
public function translations()
{
return $this->hasMany(CountryTranslation::class, 'country_id');
}
}
class CountryTranslation extends Model
{
...
}
In many places, I need to load the translations relationship for Country, but on some pages, I want to display information about the League and its Country only. There I don't want to show the CountryTranslation collection.
Here is the code for that page:
$country = $league->country;
Is it possible only for this line to disable the relations?
So, you're currently finding out one of the reasons for not defining the eager loading inside of the relationship. The first suggestion would be to remove the with() from the relationship definition, and add it in where needed. If desired, you can create another relationship that has the eager loading enabled, and it can use the base relationship to keep it DRY:
public function country()
{
return $this->belongsTo(Country::class);
}
public function countryWithTranslations()
{
return $this->country()->with('translations');
}
If this code change is not feasible, you will need to change how you're accessing the country relationship. When you access the relationship attribute, it lazy loads the relationship, and you don't have the ability to modify the relationship query. So, instead of accessing the relationship attribute, you'd need to call the relationship query so you can modify it.
Therefore, you won't be able to do $country = $league->country;, but you can do:
$country = $league->country()->without('translations')->first();
he with() simply eager loads the translations to avoid additional queries, but you should be able to load the translations with and without it, without with( adds additional queries. https://laravel.com/docs/9.x/eloquent-relationships#eager-loading
You will want to change:
public function country()
{
return $this->belongsTo(Country::class)->with('translations');
}
to
public function country()
{
return $this->belongsTo(Country::class);
}
If you want to load translations, you can do it in the controllers
// if you want translations at some point do this:
$league = League::with('country.translations')
$country = $league->country->translations
// if you do not want translations
$league = League::with('country')
$country = $league->country;
If you do not want to touch:
public function country()
{
return $this->belongsTo(Country::class)->with('translations');
}
you can create another method
public function countryClean()
{
return $this->belongsTo(Country::class);
}
$country = $league->countryClean;

Laravel how to get only relation data

this is my User.php relation code
public function activities()
{
return $this->hasMany(Activities::class, 'builder');
}
and this is my query to get only relation data
return User::whereHas('activities', fn($query) => $query->where('user_id', 1))
->paginate();
but it returns only user data without appling any relation, and its pagination not get to use pluck
i have also tried this
User::where('username', request()->username)->has('activities')->paginate();
but i need only get relation data not user with relation, and i prefer do it with whereHas
You need to create reverse relation for Activities model:
class Activities extends Model
{
// ...
public function user(): BelongsTo
{
return $this->belongsTo(User::class, 'user_id');
}
}
And get all the activities using Activities builder:
$paginated = Acitivities::query()
->whereHas('user', static function(Builder $userQuery) {
$userQuery->where('name', request()->username); // Think about security!
})
->paginate();
Note: All the models must be named in singular (e.g. Activity, User, Product and etc.).

How to add a condition to a eloquent relation

This is the case I have
$user = User_picture::where('is_main','=',1)->where('user_id','=',Auth::user()->id);
This means a user has many pictures but there is only one picture that has is_main value 1.
I tried this.
public function active_picture()
{
return $this->hasMany('App\User_picture')->find($this->is_main);
}
This doesn't return anything but an error that says it should be an object. Can anyone help me out with the case?
HasMany is meant to return a collection since it is a one to many relationship method.
If you only want to return one result, you should use HasOne with conditions. To define conditions, you use where() not find(). Find() is only used to match the model's primary key.
public function active_picture()
{
return $this->hasOne('App\User_picture')
->where('is_main', 1);
}
You could declare an accessor like:
In User model:
// declare relationship:
public function pictures()
{
return $this->hasMany(Picture::class);
}
// declare accessor:
public function getMainPictureAttribute()
{
return $this->pictures()->where('is_main', 1)->first();
}
So you can do this:
auth()->user()->main_picture ...

Laravel query multiple tables using eloquent

hi sorry bit of a newbie here but I am have three tables users, profiles, friends. they all have the user_id fields within them and I want fetch all of the fields in one statement using Eloquent and not DB::statement and doing the table joins.
How can I achieve this?
Try this
use the User class and the with method that laravel has to query model relationships
$user = User::with(['profile', 'friend'])->get();
Ensure your models has the correct relationships as follows:
app/models/User.php
public function friend () {
return $this->hasMany('Friend');
}
public function profile () {
return $this->hasOne('Profile');
}
app/models/Profile.php
public function user() {
return $this->belongsTo('User');
}
app/models/Friend.php
public function user() {
return $this->belongsTo('User');
}
use some thing like this:
You should define relations in your models with hasOne, hasMany.
class Review extends Eloquent {
public function relatedGallery()
{
$this->hasOne('Gallery', 'foreign_id', 'local_id');
}
}
class Gallery extends Eloquent {
public function relatedReviews()
{
$this->hasMany('Review', 'foreign_id', 'local_id');
}
}
$gallery = Gallery::with('relatedReviews')->find($id);
Will bring the object Gallery with
$gallery->id
gallery->name
...
$gallery->relatedReviews // array containing the related Review Objects

Resources