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
Related
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();
I have a table called "categories" and there is a 3 table needed the id of categories
the 3 tables needed a categories id is "activies", "stories", "news" i really want to know how to fix this issue the stories and news table is running well im using php tinker to see if the relationship is running and the table activities it give me a null value, inside the activities table i`d input same category_id in the table of activities it gives always a null value given in php tinker
Models
Category.php
public function stories()
{
return $this->hasMany(Story::class);
}
public function news()
{
return $this->hasMany(Newy::class);
}
public function activites()
{
return $this->hasMany(Activity::class);
}
Activity.php
public function category()
{
return $this->belongsToMany(Category::class, 'category_id');
}
If you are using hasMany in your Category Model, the opposite relationship in your Activity model should be belongsTo() and not belongsToMany()
public function category()
{
return $this->belongsTo(Category::class);
}
You can read more in the documentation here
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
Well, these are two simple entities but with many interactions which makes me a bit confused about how to do the relationship setup ..
So, I have two entities .. Category and Article .. It seems simple.. but here are what I need to do:
Categories can be a standalone model with no relationships
Category can contain other categories
Categories can contain Articles
Articles can be standalone (not inside categories)
How do you think I can model these entities and relationships between them?
Most straightforward:
// categories table: category_id fk categories.id nullable
// Category model
public function parent()
{
return $this->belongsTo('Category', 'category_id');
}
public function children()
{
return $this->hasMany('Category', 'category_id');
}
public function articles()
{
return $this->hasMany('Article');
}
// articles table: category_id fk categories.id nullable
// Article model
public function category()
{
return $this->belongsTo('Category');
}
Mind that if you wish to build a tree of those categories and then load whole tree, this will not be easy. If that's the case you should use eg. adjacency list or other model for self-referencing tables.
I have three models with Many to Many relationships: User, Activity, Product.
The tables look like id, name. And in the each model there are functions, for example, in User model:
public function activities()
{
return $this->belongsToMany('Activity');
}
public function products()
{
return $this->belongsToMany('Product');
}
The pivot table User_activity_product is:
id, user_id, activity_id, product_id. The goal is to get data like: User->activity->products.
Is it possible to organize such relations in this way? And how to update this pivot table?
First I suggest you rename the pivot table to activity_product_user so it complies with Eloquent naming convention what makes the life easier (and my example will use that name).
You need to define the relations like this:
// User model
public function activities()
{
return $this->belongsToMany('Activity', 'activity_product_user');
}
public function products()
{
return $this->belongsToMany('Product', 'activity_product_user');
}
Then you can fetch related models:
$user->activities; // collection of Activity models
$user->activities->find($id); // Activity model fetched from the collection
$user->activities()->find($id); // Activity model fetched from the db
$user->activities->find($id)->products; // collection of Product models related to given Activity
// but not necessarily related in any way to the User
$user->activities->find($id)->products()->wherePivot('user_id', $user->id)->get();
// collection of Product models related to both Activity and User
You can simplify working with such relation by setting up custom Pivot model, helper relation for the last line etc.
For attaching the easiest way should be passing the 3rd key as a parameter like this:
$user->activities()->attach($activityIdOrModel, ['product_id' => $productId]);
So it requires some additional code to make it perfect, but it's feasible.
The solution was found with some changes.
In the models relationships look like:
// User model
public function activities()
{
return $this->belongsToMany('Activity', 'user_activity_product', 'user_id', 'activity_id')->withPivot('product_id');
}
public function products()
{
return $this->belongsToMany('Product', 'user_activity_product', 'user_id', 'product_id')->withPivot('activity_id');
}
To update pivot table:
$user->products()->save($product, array('activity_id' => $activity->id));
- where product and activity ids I get from Input.
And, for example, to check if "user -> some activity -> some product is already exists":
if ($company->activities->find($activity_id)->products()->where('product_id', '=', $product_id)->wherePivot('company_id', $company_id)->get()->count() > 0) {
// code...
}
I think it needs improvements but it works for me now.