Creating Many-to-Many Relationships Between Dynamo ModelsΒΆ
Step 1: Generate the two models you will be using.php artisan make:dynamo Faq
php artisan make:dynamo Category
Example Faq migration:
Schema::create('faqs', function (Blueprint $table) {
$table->increments('id');
$table->string('question', 255);
$table->mediumText('answer');
$table->timestamps();
});
Example Category migration:
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
Example pivot table migration:
Schema::create('category_faq', function(Blueprint $table) {
$table->integer('faq_id')->unsigned()->nullable();
$table->foreign('faq_id')->references('id')->on('faqs');
$table->integer('category_id')->unsigned()->nullable();
$table->foreign('category_id')->references('id')->on('categories');
});
Run:
php artisan migrate
For the Category model:
public function faqs()
{
return $this->belongsToMany('App\Faq');
}
For the Faq Model:
public function categories()
{
return $this->belongsToMany('App\Category');
}
return Dynamo::make(\App\Employee::class)
->hasMany('categories', ['options' => [$categories]]);
Note
NOTE: You can see a full example of this process in the next section, Dynamo Methods, on the hasManySimple function