Laravel Foreign key access using eloquence

I have two models called Item and Sale where

class Item extends Model
{   
    public function sale()
    {
        return $this->hasMany('App\Sale', 'sale_id');
    }
}

      

and

class Sale extends Model
{
    public function item()
    {
        return $this->belongsTo('App\Sale', 'item_id');
    }
}

      

These are my migration files for Items

public function up()
    {
        Schema::create('items', function (Blueprint $table) {
            $table->increments('id');
            $table->string('code');
            $table->string('name');
            $table->string('location');
            $table->double('buy_price');
            $table->double('sell_price');
            $table->integer('quantity');
            $table->timestamps();
        });
    }

      

And sales

public function up()
    {
        Schema::create('sales', function (Blueprint $table) {
            $table->increments('id');
            $table->date('date');
            $table->integer('quantity');
            $table->double('sell_price');
            $table->double('subtotal');
            $table->integer('item_id')->unsigned();
            $table->timestamps();
        });

        Schema::table('sales', function (Blueprint $table) {
            $table->foreign('item_id')->references('id')->on('items')
                  ->onDelete('cascade')->onUpdate('cascade');
        });
    }

      

I recorded daily sales data to see what items were sold that day. So I wanted to look at the sales daily. And this is what I did in my controller.

class SaleController extends Controller
{
    public function show($date)
    {
        $sales = Sale::where('date', $date)->get();
        return view('sale.detail', compact('sales'));
    }
}

      

And finally, in my clip, I did this to bring up the element information

@foreach($sales as $sale)
    <tr>
        <td>{{ $sale->item->code }}</td>
        <td>{{ $sale->item->name }}</td>
        <td>{{ $sale->sell_price }}</td>
        <td>{{ $sale->quantity }}</td>
    </tr>
@endforeach

      

In the end I got Attempting to get a property of a non-object Can someone enlighten me what is wrong with my code? I am relatively new to Laravel, but I am trying to try different things to understand what this framework can do.

Thank you so much

+3


source to share


2 answers


I just noticed that your relationship is not defined correctly. Change App\Sale

to App\Item

in your regard item

. Guess the slight slip.



class Sale extends Model
{
    public function item()
    {
        return $this->belongsTo('App\Item', 'item_id');
    }
}

      

+2


source


Since you need to access the object multiple times item

, the best way to do this is to load it like:

public function show($date)
{
    $sales = Sale::with('item')->where('date', $date)->get();
    return view('sale.detail', compact('sales'));
}

      



Then you can leave your click file as it is.

+1


source







All Articles