Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Laravel Model relation error how to fix it?

I have two model : Barang and Ruangan

at migration barang :

    <?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateBarangTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('barang', function (Blueprint $table) {
            $table->id();
            $table->string('nama');
            $table->string('spesifikasi');
            $table->foreignId('id_ruangan');
            $table->string('kondisi');
            $table->integer('jumlah');
            $table->string('sumber_dana');
            $table->string('jenis');
            $table->string('keterangan');
            $table->timestamps();
        });
        
        Schema::table('barang', function (Blueprint $table) {
            $table->foreign('id_ruangan')->references('id')->on('ruangan');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('barang');
    }
}

at migration ruangan

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    <?php use Illuminate\Database\Migrations\Migration; 
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateRuangansTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('ruangan', function (Blueprint $table) {
            $table->id();
            $table->string('nama_ruangan');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('ruangans');
    }
}

Here My model Barang

 <?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Barang extends Model
{
    use HasFactory;
    
    protected $table = "barang" ;

    public function ruangan()
    {
        return $this->belongsTo(Ruangan::class);
    }
}

when i call this on tinker

$bar = new Barang
$bar = Barang::first()
$bar->ruangan->nama_ruangan 

its return null ,

even though in each table on database I have added some dummy data

i have spend 3hours to solve it and not find the answer , i have rollback the migration , i create it twice and still not solved please help me

>Solution :

Did you put the relation on Ruangan model ?

Example:

  public function barang()
    {
        return $this->hasOne(Barang::class);
    }

And by the way first returns the first item inside a collection

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading