php - One to Many Relationship Laravel cannot display the related data
Get the solution ↓↓↓I have two models calledDataKelurahan
andRegistrasiPasien
and have a one-to-many relationship, but I can't access the relationship.
I have made a form for adding patient and save it to theregistrasi_pasiens
table and it works well. but when I try to display the relation data, it doesn't work properly.
In theregistrasi_pasiens
table, I have 1 record withkelurahan_id = 3
. Then, I try to access it viaphp artisan tinker
with these command:
$kelurahan = App\Domain\DataKelurahan\Models\DataKelurahan::find(3)
works fine and data is exist.$pasien = App\Domain\RegistrasiPasien\Models\RegistrasiPasien::find(2007000001)
works fine and the data is exist withkelurahan_id = 3
$kelurahan->pasiens
the result isnull
. Shouldn't it show the pasien data that has kelurahan_id = 3?$kelurahan->pasiens->nama
and the result is like thisPHP Notice: Trying to get property 'nama' of non-object in D:/PROFESSIONAL/PROJECT/WEB DEVSeval()'d code on line 1 => null
I don't have any idea what's wrong with my codes. Much appreciate for your help guys.
Below are the models that I have made:
DataKelurahan.php
<?php
namespace App\Domain\DataKelurahan\Models;
use Illuminate\Database\Eloquent\Model;
use App\Domain\RegistrasiPasien\Models\RegistrasiPasien;
class DataKelurahan extends Model
{
protected $fillable = ['nama_kelurahan', 'nama_kecamatan','nama_kota'];
public function pasiens(){
return $this->hasMany('RegistrasiPasien');
}
}
RegistrasiPasien.php
<?php
namespace App\Domain\RegistrasiPasien\Models;
use Illuminate\Database\Eloquent\Model;
use App\Domain\DataKelurahan\Models\DataKelurahan;
class RegistrasiPasien extends Model
{
protected $fillable = [
'nama',
'alamat',
'telepon',
'rt',
'rw',
'tgl_lahir',
'jenis_kelamin'
];
public function kelurahan(){
return $this->belongsTo('DataKelurahan');
}
}
And below are my database tables:
data_kelurahans
Schema::create('data_kelurahans', function (Blueprint $table) {
$table->increments('id');
$table->string('nama_kelurahan');
$table->string('nama_kecamatan');
$table->string('nama_kota');
$table->timestamps();
});
registrasi_pasiens
Schema::create('registrasi_pasiens', function (Blueprint $table) {
$table->increments('id');
$table->integer('kelurahan_id')->unsigned();
$table->string('nama');
$table->string('alamat');
$table->char('telepon', 15);
$table->integer('rt');
$table->integer('rw');
$table->date('tgl_lahir');
$table->string('jenis_kelamin');
$table->timestamps();
});
Schema::table('registrasi_pasiens', function (Blueprint $table){
$table->foreign('kelurahan_id')->references('id')->on('data_kelurahans')->onDelete('cascade');
});
Answer
Solution:
From Docs:
Eloquent will automatically determine the proper foreign key column on the model. By convention, Eloquent will take the "snake case" name of the owning model and suffix it with
_id
.
So, Eloquent probably got your foreign key name wrong so you must override the foreign key by passing additional arguments to thehasMany
/belongsTo
method:
public function pasiens(){
return $this->hasMany('RegistrasiPasien','kelurahan_id');
}
public function kelurahan(){
return $this->belongsTo('DataKelurahan','kelurahan_id');
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: php undefined array key
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.