laravel eloquent: Get all groups for a member - laravel

I have 3 tables:
user: id,name
group: id,name,parent_id made with this) // this is a nested category
members: id,user_id,group_id,admin
and group_member: group_id,member_id
I want get all groups (include sub groups) for a member. Is it possible do it via laravel?
Group Modal:
public function parent()
{
$parent = $this->belongsTo('App\Group', 'parent_id');
return $parent;
}
public function children()
{
$children = $this->hasMany('App\Group', 'parent_id');
//$children->wherePublish(1);
return $children;
}
public function members()
{
return $this->hasMany(Member::class);
}
public function member()
{
return $this->belongsToMany(Member::class);
}
And Member model:
public function groups()
{
return $this->belongsToMany(Group::class);
}
public function group()
{
return $this->belongsTo(Group::class);
}
And user model:
public function members()
{
return $this->hasMany(Member::class);
}
I want something like this (to return all related groups):
auth()->user()->members()->groups()->get();
I got this error:
"Call to undefined method
Illuminate\Database\Eloquent\Relations\HasMany::groups()"

Please use this below query:
App/User::where('id',auth()->user()->id)->with('members.groups')->get();

Related

Has Many through relationship laravel didnot solve

I Have
Worker -> HasOne-> Document ->morphMany ->FIles
->HasOne-> Medical ->morphMany ->FIles
->HasOne-> Course ->morphMany ->FIles
I want to use hasManyTrough to get files data from worker, I defined
public function medical_detail()
{
return $this->hasOne(MedicalDetail::class);
}
Tried this :
public function document_files()
{
return $this->hasManyThrough(File::class, Document::class);
}
In worker but It expects document_id, how can I use ir for polymirhic relationship?
Also did this
public function document_files()
{
return $this->hasManyThrough(
File::class,
Document::class,
'worker_id',
'filable_id',
'id',
'id'
);
}
THese are my models
File model
protected $fillable = ['file_type', 'file_name', 'fiable_id', 'filable_type'];
public function filable()
{
return $this->morphTo();
}
Worker MOdel
public function course()
{
return $this->hasOne(Course::class);
}
public function document()
{
return $this->hasOne(Document::class);
}
Document model
protected $fillable = ['doc_name', 'worker_id'];
public function worker()
{
return $this->belongsTo(Worker::class);
}
public function files()
{
return $this->morphMany(File::class, 'filable');
}
This is one alternative I added, but gives me No query results for model [App\\Models\\Worker] 1 . SInce files table has $table->morphs('filable'); This defined so I added filable_id. These are my models: docuemnt,file

Access to relation on another model

I have three tables, Owner, Garages, Orders
Owner has "n" garages and each garage belongs to one owner,
Each garage can have many orders,
Now is it possible to only get orders from Owner model with relation instance?
Owner model:
public function garages()
{
return $this->hasMany(Garage::class);
}
Garage model:
public function owner()
{
return $this->belongsTo(Owner::class);
}
public function orders()
{
return $this->hasMany(Order::class);
}
First of all, you should elaborate on how your models are defined. Guessing about how your database schemas are, you must fix your relationships:
Owner.php
public function garages()
{
return $this->hasMany(Garage::class);
}
public function orders()
{
return $this->hasManyThrough(Order::class, Garage::class);
}
Garage.php
public function owner()
{
return $this->belongsTo(Owner::class);
}
public function orders()
{
return $this->hasMany(Order::class);
}
Order.php
public function garage()
{
return $this->belongsTo(Garage::class);
}
Saying that, you can access your Owner's orders by doing
$owner = Owner::findOrFail($owner_id);
$orders = $owner->orders;

How to access a relationship through another relationship

I am working on a survey system and I have a problem accessing one relationship through another.
I have these models and these relationships
Post
id
survey_id
public function survey()
{
return $this->belongsTo(Survey::class);
}
Surveys
id
survey_type_id
public function surveyOptions()
{
return $this->belongsToMany(Survey_options::class);
}
public function surveyType()
{
return $this->belongsTo(Survey_type::class);
}
public function posts()
{
return $this->hasMany(Post::class);
}
Survey_types
id
public function surveyOptions()
{
return $this->belongsToMany(Survey_options::class);
}
Survey_options
id
survey_type_id
public function values()
{
return $this->hasMany(Option_value::class);
}
options_values
survey_options_id
survey_id
public function surveyOptions()
{
return $this->belongsTo(Survey_options::class);
}
Survey_Survey_options (pivot)
survey_id
survey_options_id
I need to do a query with eadger loading to bring me all the posts with survey, surveyOptions, withCount comments y options_values of each surveyOptions. Something like this:
$post->survey->surveyOptions->options_values_count
I have managed to create this query that works for me everything except fetching the option_values count from each SurveyOption. How can I do it?
$posts = Post::with([
'survey' => function ($query) {
$query->withCount('totalSurveys');
},
'survey.surveyOptions',
'image',
'categories'
])
->withCount('comments')
->get();

Laravel check if attributes have products

I have model Attribute, and I have model AttributeValue. I have relation products in AttributeValues belongsToMany. I want check in model Attribute if products exists in AttributeValues. I tried:
public function values()
{
return $this->hasMany(AttributeValue::class);
}
public function getProductsExistsAttribute()
{
return $this->values->each->products()->exists();
}
AttributeValue model:
public function attribute()
{
return $this->belongsTo(Attribute::class);
}
public function products()
{
return $this->belongsToMany(Product::class, 'product_attribute_values');
}
But is not work. I get error: Method Illuminate\Database\Eloquent\Collection::exists does not exist.. How I can resolve this issue?

Call to undefined relationship [cities] on model [App\Models\Municipal_district]

City.php
public function municipal_districts()
{
return $this->hasMany('App\Models\Municipal_district');
}
Municipal_district.php
public function province()
{
return $this->belongsTo('App\Models\City', 'city_id');
}
Where can be wrong?
City Model
public function municipal_districts()
{
return $this->hasMany('App\Models\Municipal_district');
}
Municipal_district Model
public function city()
{
return $this->belongsTo('App\Models\City', 'city_id');
}
And I don't knwo about cities() relationship here!
City has many ditricts and District belongs to one city. So, What is the cities() here?

Resources