May 7, 2018 02:50
my pleasure
May 7, 2018 02:50
Awesome! Glad to hear it
May 7, 2018 02:46
of course, i'll wait and make sure
May 7, 2018 02:44
I've updated my answer to reflect this conversation
May 7, 2018 02:43
your pivot table will contain a relationship to both a product, and a relative
May 7, 2018 02:43
yup
May 7, 2018 02:40
So within that model, you'd add two methods...something like this
public function product()
{
return $this->belongsTo(Product::class,'product_id');
}
public function relative()
{
return $this->belongsTo(Product::class,'relatives_id');
}
May 7, 2018 02:40
From within the pivot table, they are technically "BelongsTo" if you think about it
May 7, 2018 02:39
if you re-create the model, don't add a BelongsToMany inside it
May 7, 2018 02:39
Yeah, i was anticipating this. Okay, so you can re-create that model if you want...or you can just do a raw DB query
May 7, 2018 02:36
you might need to add a couple more parameters to make your columns line up...but i think that'll work
May 7, 2018 02:36
public function relatives()
{
return $this->belongsToMany(Product::class, 'product_relatives', 'product_id', 'relatives_id');
}
May 7, 2018 02:35
product_relatives is the pivot table containing the values
May 7, 2018 02:34
So the Product belongsToMany Product
May 7, 2018 02:34
$this->belongsToMany(Product::class, 'product_relatives', ...)
May 7, 2018 02:34
you just need a pivot table and a BelongsToMany
May 7, 2018 02:33
I'm just trying to think if it'd be easiest to simply edit my answer. I don't think you need a ProductRelative model at all.
May 7, 2018 02:33
Ohhhh. Now I understand what you are trying to do :)
May 7, 2018 02:31
Why did you call your model ProductRelative?
May 7, 2018 02:31
But that doesn't require two models and a separate pivot table
May 7, 2018 02:31
So if elsewhere you are using sync in your code, then you might actually be trying to do a BelongsToMany.
May 7, 2018 02:30
Hi :). Stack started squaking at me
May 7, 2018 02:30
So sync is a method that only exists on a BelongsToMany Relationship
May 7, 2018 02:29
In that case your Relatives model would contain the product_id. So it can only "belongTo" a single product. Several Relatives can "belongTo" the same product however, so from the Products perspective it is "HasMany". Does this sound like what your are looking for? Otherwise, if you are wanting Relatives to HaveMany products, and Products to HaveMany Relatives, then you wanted a BelongsToMany, which I've described in my answer.
May 7, 2018 02:29
At this point I'm lacking a bit of context to make suggestions to database design. Normally to create a relationship between two models though you would use a HasMany relationship. The confusing part to me is your $fillable fields. Those look like you are trying to create a pivot table.
May 7, 2018 02:29
Then you shouldn't be doing a belongsToMany relationship. It implies using a pivot table between two models.
 
Aug 16, 2017 21:50
@Pioneer2017 Yeah, the code I added won't work as is. I'll post an answer on your original question to get you started. Give me a bit.
Aug 16, 2017 18:20
I think you'd benefit from taking a couple hours and watching the following series (at least up until you understand eloquent relationships) laracasts.com/series/laravel-from-scratch-2017. What you're attempting to do seems wildly over complicated compared to the "Laravel Way". Why even use Laravel if you're not going to embrace it? I suspect you should be able to accomplish what your attempting with little more than $products = Product::with(['categories', 'items'])->orderBy('price', $sort)->get(); in your controller. Why build up your own arrays if you don't have to?