Home > Software engineering >  Laravel Model relation error how to fix it?
Laravel Model relation error how to fix it?

Time:12-09

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

    <?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

CodePudding user response:

as you are using a foreign key outside the naming convention of laravel, you have to pass the custom foreign key name as the second argument of the relationship definition.

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

and then you can access $bar->ruangan->nama_ruangan; to get nama_ruangan from Ruangan model.

CodePudding user response:

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

  • Related