Search code examples
phplaravelphp-5.4laravel-6.2

Laravel relationships seems to not working


I have an Item and AdvertItem objects in Laravel. I want to create a 1 to 1 relationship between an item and advert item

The item class looks like this

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Item extends Model
{
    //
    public function Category(){
        return $this->belongsTo(Category::class);
    }

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

    public function AdvertItem(){
        return $this->hasOne(AdvertItems::class);
    }
}

and the AdvertItem class looks like this

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class AdvertItems extends Model
{
    protected $guarded = [];

    //
    public function items(){
        return $this->belongsTo(Item::class);
    }
}

but when I call advertItem I only see item_id = 1 instead of item object.

Item table is created like this

 class CreateItemsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('items', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('description');
            $table->unsignedBigInteger('currency_lookup_id');
            $table->unsignedBigInteger('category_id')->index();
            $table->unsignedBigInteger('price');
            $table->string("image_path");
            $table->string('sale_ind');
            $table->Date('eff_from');
            $table->Date('eff_to');
            $table->timestamps();
        });
    }

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

And advert table is created like this

class CreateAdvertItemsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('advert_items', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('item_id');
            $table->unsignedBigInteger('customer_id');
            $table->Date('eff_from');
            $table->Date('eff_to');
            $table->timestamps();
        });
    }

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

please assist.


Solution

  • The following rules will assist you.

    • Always start relationship names with lower case. Save capitals for Classes not methods.

    • Models should be Singular

    • Pay attention to the plurality of names. Things that there should only be one of should be singular. So, in your 1:1 relationships, both relationship names should be singular.

    AdvertItem class

        public function item(){
            return $this->belongsTo(Item::class);
        }
    

    then, if you have Item and want AdvertItem, you should load it

    $item->load('advertitem');
    

    or the other way around

    $advertItem->load('item');