<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $table = "products";
public function additives()
{
return $this->belongsToMany(Additive::class);
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Additive extends Model
{
protected $table = "additives";
public function products()
{
return $this->belongsToMany(Product::class);
}
}
$product = Product::find($request->post('id'))->additives()->get();
echo "<pre>";
print_r($product);
echo "</pre>";
<pre>Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
[0] => App\Models\Additive Object
(
[table:protected] => additives
[connection:protected] => mysql
[primaryKey:protected] => id
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
(
)
[withCount:protected] => Array
(
)
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>
[attributes:protected] => Array
(
[id] => 3
[name] => Test1
[image] => Test1
[visibility] => 1
[price] => 2
[created_at] =>
[updated_at] =>
)
[original:protected] => Array
(
[id] => 3
[name] => Test1
[image] => Test1
[visibility] => 1
[price] => 2
[created_at] =>
[updated_at] =>
[pivot_product_id] => 1
[pivot_additive_id] => 3
)
[changes:protected] => Array
(
)
[casts:protected] => Array
(
)
[classCastCache:protected] => Array
(
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[appends:protected] => Array
(
)
[dispatchesEvents:protected] => Array
(
)
[observables:protected] => Array
(
)
[relations:protected] => Array
(
[pivot] => Illuminate\Database\Eloquent\Relations\Pivot Object
(
[incrementing] =>
[guarded:protected] => Array
(
)
[connection:protected] => mysql
[table:protected] => additive_product
[primaryKey:protected] => id
[keyType:protected] => int
[with:protected] => Array
(
)
[withCount:protected] => Array
(
)
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>
[attributes:protected] => Array
(
[product_id] => 1
[additive_id] => 3
)
[original:protected] => Array
(
[product_id] => 1
[additive_id] => 3
)
[changes:protected] => Array
(
)
[casts:protected] => Array
(
)
[classCastCache:protected] => Array
(
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[appends:protected] => Array
(
)
[dispatchesEvents:protected] => Array
(
)
[observables:protected] => Array
(
)
[relations:protected] => Array
(
)
[touches:protected] => Array
(
)
[timestamps] =>
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[fillable:protected] => Array
(
)
[pivotParent] => App\Models\Product Object
(
[table:protected] => products
[connection:protected] => mysql
[primaryKey:protected] => id
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
(
)
[withCount:protected] => Array
(
)
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>