php - Laravel migration references between tables -
i've read laravel eloquent relationship & database migration docs, can't understand 1 thing. can create relationship between 2 tables through creating migration file like:
public function up() { schema::create('keywords', function (blueprint $table) { $table->increments('id'); $table->string('name'); $table->integer('keyword_group_id')->unsigned(); $table->foreign('keyword_group_id')->references('id')->on('keyword_groups')->ondelete('cascade'); }); }
and then, after php artisan migrate command got 2 connected one many relation tables. on other side docs describes process of creating relations between 2 eloquents. in case looks like:
class keyword extends model { public $timestamps = false; protected $fillable = array('name'); public function keywordgroup() { return $this->belongsto('app\keywordgroup'); } } class keywordgroup extends model { public $timestamps = false; protected $fillable = array('name'); public function keywords() { return $this->hasmany('app\keyword'); } }
but if comment string of code, make relation between tables in migration file, 2 connection functions in model classes doesn't creat relationship. can please explain me need write methods inside model classes? keywordgroup & keywords
the migrations create database structure, if create foreign key here relation added in database itself.
the relations create in model have no effect on database itself, used make easy load data different tables in code. these work wether or not have created foreign keys in database.
Comments
Post a Comment