Ich möchte eine "One To Many" -Beziehung seed.Seeding/Migration hasMany Beziehung
Produktmodell
class Product extends Model
{
protected $table = 'products';
}
Auftragsmodell
class Order extends Model
{
protected $table = 'orders';
/**
* Products by order.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function comments()
{
return $this->hasMany('Product');
}
}
Produktmigration
class CreateProductsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->unique();
$table->integer('stock');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('products');
}
}
Bestellen Migration
class CreateOrdersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('orders', function (Blueprint $table) {
$table->increments('id');
// refers to a user table
$table->integer('user_id')->unsigned();
$table->foreign('user_id')
->references('id')->on('users');
// "One To Many" relation???
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('orders');
}
}
Produkt Seeder
class ProductsTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
DB::table('products')->insert([
'name' => 'EEE PC',
'stock' => 20
]);
}
}
Bestellen Seeder
class OrdersTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
DB::table('orders')->insert([
'user_id' => 1
// "One To Many" relation???
]);
}
}
Muss ich eine Tabelle wie "order_product" oder etwas verbinden schaffen? Ich bin verwirrt, weil in der Reihenfolge Modell, das hasMany
zu Artikel bezieht sich
Eine Bestellung Produkte haben, aber jedes Produkt kann in in unterschiedlicher Reihenfolge verwendet werden!