model - Laravel 5 Eloquent belongsTo() foreign key won't work -


i found out naming of column in schema::create() can affect creating constrains, can't found model in later queries.

schema (simplefied):

schema::create('page_elements', function(blueprint $table) {     $table->increments('id');     $table->integer('page_element_type_id')->unsigned();     $table->foreign('page_element_type_id')->references('id')->on('page_element_types')->onupdate('cascade')->ondelete('restrict');     $table->timestamps(); }); 

and when call

pageelement::find(1)->first()->type()->get() 

i empty collection.

after changing 2 lines in shema (only change page_element_type_id type_id):

$table->integer('type_id')->unsigned(); $table->foreign('type_id')->references('id')->on('page_element_types')->onupdate('cascade')->ondelete('restrict'); 

i got 1 element in collection, should.

anyone knows if there naming issue or rule, regarding columns constrains?

edid: declaration of type() in model, requested:

public function type() {     return $this->belongsto('app\pageelementtype'); } 

can show type function?

i bet did not use second parameter set name of column has foreign key, change to:

return $this->belongsto('app\yourmodel', 'page_element_type_id'); 

Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -