Home > Back-end >  [PHP][Laravel] Cant show/get column from another table using Laravel Eloquent ORM
[PHP][Laravel] Cant show/get column from another table using Laravel Eloquent ORM

Time:10-06

im trying to show or get column from another table using eloquent laravel, but it seems i have miss something or typo. Im using Laravel 9, and i've followed all the instructions video and documentation and all of them seems to be not working.

Error message says : Attempt to read property "deskripsi" on null

Migrations :

  • table bisnis :

public function up()
    {
        Schema::create('t_bisnis', function (Blueprint $table) {
            $table->id();
            $table->string('deskripsi', 255);
            $table->string('pemilik', 255);
            $table->timestamps();
        });
    }

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

  • table group :

public function up()
    {
        Schema::create('t_grup_layanan', function (Blueprint $table) {
            $table->id();
            $table->foreignId('bisnis_id')->nullable()->index('fk_bisnis_to_grup');
            $table->string('deskripsi', 255);
            $table->timestamps();
        });

    }

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

Models :

  • bisnis :

class Business extends Model
{
    use HasFactory;

    protected $table = 't_bisnis';
    protected $guarded = [];
    protected $primary_key = 'id';
    protected $with = ['group'];
    protected $fillable = 
    [
        'deskripsi',
        'pemilik'
    ];

    public function group(){
        return $this->hasOne(GroupService::class);
    }

}

  • group :

class GroupService extends Model
{
    use HasFactory;
    
    protected $table = 't_grup_layanan';
    protected $guarded = [];
    protected $primary_key = '';
    protected $fillable = 
    [
        'bisnis_id',
        'deskripsi'
    ];
    protected $with = [];

    public function service(){
        return $this->hasMany(Service::class);
    }    

    public function business()
    {
        return $this->belongsTo(Business::class);
    }
    
}

My blade :

@foreach ($list as $item)    
            <tr>
              <td>{{ $loop->iteration }}</td>
              <td>{{ $item->id }}</td>
              <td>{{ $item->business->deskripsi }}</td>
              <td>{{ $item->deskripsi }}</td>
              <td>{{ $item->updated_at->diffForHumans() }}</td>
              <td>
                {{-- <a href="#" ><span data-feather="eye"></span></a> --}}
                <a href="{{ route('gruplayanan.edit', $item->id) }}" ><span data-feather="edit"></span></a>
                <form action="{{ route('gruplayanan.delete', $item->id) }}" method="post" >
                @method('delete')
                @csrf
                <button  onclick="return confirm('Are you sure?')"><span data-feather="x-circle"></span></button>
                </form>
              </td>
            </tr>
@endforeach

My controller :

class GroupServiceController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $list = GroupService::all();
        return view('dashboard.gruplayanan.main', compact('list'));
    }

CodePudding user response:

When you create a belongsTo relation in a model it automatically assumes that the relation key is {the name of the function}_id.

In your case it looks for a business_id field in the t_grup_layanan table, because your function name is business.

You can override this behavior by giving a second parametor to the belongs to function which is the foreign key name.

public function business()
{
    return $this->belongsTo(Business::class, 'bisnis_id');
}

This should do the trick.

You can find information about this in the official documentation if you scroll down just a little here

  • Related