2
我是laravel的初学者,我想得到一个ManyToMany的关系。这是我的移民文件:将数据导入belongsToMany关系Laravel
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->unique();
$table->string('slug')->unique();
$table->text('description');
$table->decimal('price', 10, 2);
$table->string('image')->unique();
$table->timestamps();
});
Schema::create('product_user', function (Blueprint $table) {
$table->increments('id');
$table->integer('number')->unsigned();
$table->integer('product_id')->unsigned()->index();
$table->integer('user_id')->unsigned()->index();
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
,这是类用户的产品类
class Product extends Model
{
public $fillable = ['name', 'slug', 'description', 'price', 'image'];
public function users()
{
return $this->belongsToMany('App\Models\User');
}
}
,我补充一点:
public function products()
{
return $this->belongsToMany('App\Models\Product');
}
我的问题是如何让外地号码???
@foreach($user->products as $product)
Produit : {{ $product->name }} <br/>
Slug : {{ $product->slug }}<br/>
Number : {{ /* how to get this ??? */ }}<br/>
@endforeach
感谢