korni
korni

Reputation: 89

Undefined property error in one to many

I'm trying to make an app where you can administer office materials, invoices etc. So my Order has many Materials and one Invoice.

My Materials table has:

id | name | count_all | count_current | price_unit

My Orders table has:

id | count_order | material_id | invoice_id

My Invoices table has:

id | number | inc_date | scan_name | sum

My Material model looks like:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Material extends Model
{
    public function order(){
        return $this->belongsTo('Order', 'material_id');
    }
    public function expenditure(){
        return $this->belongsTo('Expenditure');
    }
    protected $fillable = [
        'name', 'count_all', 'count_current', 'price_unit',
    ];
}

My Order model looks like:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Order extends Model
{
    public function invoice(){
        return $this->hasOne('App\Invoice');
    }

    public function material(){
        return $this->hasMany('App\Material');
    }

    protected $fillable = [
        'count_order',
    ];
}

My OrderController looks like:

<?php

namespace App\Http\Controllers;

use App\order;
use Illuminate\Http\Request;
use DB;
use App\Http\Controllers\Controller;

class orderController extends Controller
{
    /**
     * Show a list of all of the application's orders.
     *
     * @return Response
     */
    public function show()
    {
        $orders = DB::table('orders')->get();

        return view('order', ['orders' => $orders]);
    }

    public function insert(Request $request){

        $order = new order;

        $order->count_order = $request->count_order;

        $order->save();
        return redirect('/order');
    }

    public function edit(order $order){

        return view('edit_order', compact('order'));   
    }

    public function update(Request $request, order $order){
        if(isset($_POST['delete'])){
            $order->delete();
            return redirect('/order');
        }
        else{
            $order->count_order = $request->count_order;

            $order->save();
            return redirect('/order');
       }
    }
}

So when trying to access DB I will use fuction "show" in my route.

And piece of my blade that "cause problem":

<tbody>
    @foreach($orders as $order)
    <tr>
        <td>{{$order->id}}</td>
        <td>{{$order->material->name}}</td> //this is causing the Undefined property problem when trying to display material name instead of id
        <td>{{$order->invoice_id}}</td> //this should look similar to the one above but since it doesn't work I didn't change it
        <td>{{$order->count_order}}</td>
        <td>    
            <form action="/order/{{$order->id}}">
                <button type="submit" name="edit" class="btn btn-primary">Edycja zamówienia</button>
                {{ csrf_field() }}
            </form>
        </td>
    </tr>
    @endforeach
    </tbody>

So when I'm trying to display Material name in blade (like above) it returns the following error:

Undefined property: stdClass::$materials (View: C:\xampp\htdocs\laravel\petition_app\resources\views\order.blade.php)

Upvotes: 1

Views: 425

Answers (1)

Sohel0415
Sohel0415

Reputation: 9863

You need to use belongsTo() relationship from order to material. Change your relationship to:

public function material(){
    return $this->belongsTo('App\Material','material_id');
}

You are using query builder, you need to use laravel eloquent to access relationship model data. Change your controller logic code like:

$orders = Order::all();

Upvotes: 2

Related Questions