kazulla
kazulla

Reputation: 65

How to use hasOne relationship correctly?

I am learning Laravel and I'm trying to create simple online store. I created tables Items and Amounts. Now I want to display all Items with their amount in stock but for some reason unknown to me, amount of item is not fetched into items.

These are my schemas for tables:
Items:


    Schema::create('items', function (Blueprint $table) {
                $table->increments('id');
                $table->integer('category_id')->unsigned();
                $table->string('name', 120)->nullable(false);
                $table->float('price',8,2)->unsigned()->nullable(false);
                $table->longText('short_specification');
                $table->longText('specification');
                $table->longText('description');
                $table->string('photo', 100);
                $table->engine = 'InnoDB';

                $table->foreign('category_id')->references('id')->on('categories');
            });

Amounts:


    Schema::create('amounts', function (Blueprint $table) {
                $table->integer('item_id')->unsigned();
                $table->integer('amount')->unsigned()->nullable(false);
                $table->engine = 'InnoDB';
            });

            Schema::table('amounts',function($table){
                $table->foreign('item_id')->references('id')->on('items');
                $table->primary('item_id');
            });

These are my models:
Item:


    class Item extends Model
    {
        public $timestamps = false;


        function amount()
        {
            return $this->hasOne('App\Amount','item_id','id');
        }
    }

Amount:


    class Amount extends Model
    {

        function item()
        {
                 //$this->belongsTo('App\Item');
            return $this->belongsTo('App\Item','item_id','id');
        }
    }

When I do:

$items = DB::table('items')->get();
dd($items);
return view('home')->with('items',$items);

Items are displayed correctly, but amount of item isn't there. When I do:


    @foreach($items as $item)

            {{ $item->id }}
            {{ $item->amount }}

    @endforeach

I get:

Undefined property: stdClass::$amount (View: D:\2. PROGRAMY\xampp\htdocs\silicon_store\resources\views\home.blade.php) error.


From what I've seen on the web (I've been trying to fix this for over 3 hours now so I must be doing something totally wrong) it should work properly but it isn't.

Upvotes: 1

Views: 85

Answers (2)

HEL Mab
HEL Mab

Reputation: 35

or you can use kind of this query

$items = App\Item::whereHas('amount')->get()

Here link to understanding whereHas

Upvotes: 0

Don't Panic
Don't Panic

Reputation: 41810

With $items = DB::table('items')->get();, you're using the query builder. It won't have the value of the relationship unless you join the amounts table in the query.

$items = DB::table('items')
        ->leftJoin('amounts', 'items.id', '=', 'amounts.item_id')
        ->get();

I think you could also use an Eloquent query. In that case each $item would be an instance of the Item model rather than a StdClass object.

$items = App\Item::with('amount')->get();

Upvotes: 1

Related Questions