Laravel MongoDB hasMany relationship jenssegers/laravel-mongodb - laravel

I have two collections (films and actors) in films collection I have column actor_ids which contains actors ids like this
actors: ["62ced094c68f1d2da30de05d", "62cedf86c68f1d2da30de061"] and I want to create relationship between films and actors.
I define hasMany in films model
public function producer(): HasMany
{
return $this->hasMany(Actor::class);
}
And BelongsTo relation in actor model
public function films() : BelongsTo
{
return $this->belongsTo(Film::class);
}
But as result of $film->actors I get empty array.
Please help how to get data.

Related

How to get the all array in relation model in Larave?

How to get all the array in relations model. It supposed to be 3 records in relation service, I can fetch all the services of doctor if per one record, but if I will display all the doctor with services, the services per doctor displaying only one.
In my controller
$doctors = User::with('reviews', 'services')->role('Doctor')->where('clinic_id', $id)->get();
User Model
public function services()
{
return $this->belongsTo(Service::class, 'id', 'doctor_id');
}
This is the structure of service table
I've tried hasMany but relation service is empty
You have to change your relation in User model to hasMany as Doctor has many services
per your table structure.
public function services()
{
return $this->hasMany(Service::class, 'doctor_id', 'id');
}
and in Service model use belongsTo

How to get data from three models that are related to each other in Laravel 5.8

So I have this model Order which has one to many relation with ProductOrder model. And also Product model which has the same one to many relation with ProductOrder model. I can get the PorductOrder data with Product or with Order. Like this:
Order::with('product_orders')->get();
Now this is returning my orders with the associated ProductOrder data. How can I include the Products of each ProductOrder data in this collection?
Make changes of function name, field name, model name as per you requirements
Step 1:
In you ProductOrder Model use this function:
public function products()
{
return $this->hasOne('App\Product', 'id', 'product_id');
}
Step 2:
After this in you query you can use as:
Order::with('product_orders.products')->get();
It sounds like ProductOrder is just a joining table/model, is that right? If that is the case, you should change to a ManyToMany relationship to of the Products model to
public function orders()
{
return $this->belongsToMany('App\Orders', 'product_order');
}
and the Orders to
public function products()
{
return $this->belongsToMany('App\Products', 'product_order');
}
Then you can access like
Order::find(1)->products()->get();

Laravel belongs to Many relations

I can't speak English well. I'm Sorry.
There are 3 tables : adverts, advert_categories, categories.
I want to pull data, I get an error
Adverts::with(['categories'])->where("user_id", Auth::user()->id)->get();
public function categories(){
return $this->belongsToMany(AdvertCategories::class, 'advert_categories', 'advert_id', 'category_id');
}
advert_categories :
categories:
The first parameter of belongsToMany method should be your target table not the pivot table in your case is Category,
I suppose that your models are: Advert belongsToMany Category, and your pivot table is: advert_categories the you relationship definition should look like this:
public function categories(){
return $this->belongsToMany(Category::class, 'advert_categories', 'advert_id', 'category_id');
}
Laravel docs link

Laravel relationships with three tables

I am busy building a online ordering system, but now I am stuck with this one table relationship.
I want the User to be able to create many orders, and the order will have many products.
I have looked at many-to-many relationships, however it does not include a third table.
The basic many-to-many relationship releases by pivot table and declares in laravel like belongsToMany
In your situation you can use hasManyThgough relationship to access all products which are connected with user through orders
User.php
public function orders() {
return $this->hasMany('App\Order');
}
public function products() {
return $this->hasManyThrough('App\Product', 'App\Order');
}
Order.php
public function products() {
return $this->hasMany('App\Product');
}
Link to laravel relationship hasManyThrough documentation

Laravel eloquent not calling the relationship (returning string)

I have this relationship:
A store may have many products and each products can have many effects
stores and products -> many to many relationship
products and effects -> one to many (one product can have multiple effects)
Here are my relationships:
Store model
public function products()
{
return $this->belongsToMany('App\Models\Product');
}
Product model
public function stores()
{
return $this->belongsToMany('App\Models\Store');
}
public function effects()
{
return $this->hasMany('App\Models\Effect');
}
Effect model
public function products()
{
return $this->belongsTo('App\Models\Product');
}
Now, I can access the relationship with products ok, but when I access the effects relationships:
$store = Store::where('slug', 'cantina')->first();
dd($store->products->first()->effects);
The dd returns a string with the name of the relationship:
"effects"
I never seen this "error" before, anyone knows why the relationship are returning a string instead of the eloquent?
[Edit]
If I digit anything in the relationship call, they output what I digited:
dd($store->products->first()->effects);
//Output:
"effects"
Another example:
dd($store->products->first()->foobar);
//Output:
"foobar"

Resources