Given my Models Person and Student. Now, one of my controller is doing this:
$person_id = AppModelsPerson::insertGetId(['name' => $request->name, ...]);
AppModelsStudent::create(['person_id' => $person_id, ...]);
$student = AppModelsStudent::where('person_id', $person_id)->first();
dd($student);
During execution, everything has been save but My problem is why i am receiving null on die and dump?
By the way, my student tables’ primary and foreign key is the person_id column.
Edit
class Student extends Model
{
protected $fillable = ['section', 'current_year'];//there are other attribute here which I didn't show because I am using mobile
public function person(){
return $this->belongsTo('AppModelsPerson', 'person_id', 'id');
}
...
}
Advertisement
Answer
Add person_id in $fillable property:
protected $fillable = ['section', 'current_year', 'person_id'];