I want to have a Chapter model that contains a district column contained multiple District model. But I have no idea how to link it. I just have done with model and migration.
class Chapter extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
];
public function districts()
{
return $this->hasMany(District::class);
}
}
class District extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
];
public function chapter()
{
return $this->belongsTo(Chapter::class);
}
public function blocks()
{
return $this->hasMany(Block::class);
}
}
What should I do after this? Do I need to mention the district in the chapter migration file?
Here is my migration file.
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateChaptersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('chapters', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->indexed();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('chapters');
}
}
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateDistrictsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('districts', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->indexed();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('districts');
}
}
Do I need to link them in the $fillable side?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire