alessio scionti
alessio scionti

Reputation: 51

can't access model in relations laravel

i have a problem with my models, i am trying to access model properties to recall them in blade. the problem is that i can't log in

this is my model Order:

<?php

namespace App\Models;

use App\Models\Restaurant;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Order extends Model
{
    use HasFactory;

    protected $fillable=[
        'id_user',
        'id_wine',
        'quantita',
        'prezzo',
        'data_ordine',
        'id_restaurant',
        'pagato',
        'evaso',
    ];

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

and this is my model Restaurant:

<?php

namespace App\Models;

use App\Models\User;
use App\Models\Order;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Restaurant extends Model
{
    use HasFactory;

    protected $fillable = [
        'ragione_sociale',
        'indirizzo',
        'citta',
        'provincia',
        'cap',
        'partita_iva',
    ];

    public function users(){
        $this->hasMany(User::class);
    }

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

in blade I need to have access to the restaurant model in order to view the "ragione_sociale", but I get this error:

ErrorException
Trying to get property 'ragione_sociale' of non-object (View: C:\Users\danil\OneDrive\Desktop\progetti_kemedia\Enoteca_Etna2\resources\views\ordini\ordini.blade.php)

this is my blade view:

@foreach ($orders as $order)
        <div>
           <span>Ristorante: {{$order->Restaurant->ragione_sociale}}</span>
        </div>
@endforeach

Upvotes: 1

Views: 756

Answers (2)

Bomberis123
Bomberis123

Reputation: 378

You have to specify foreign key, just check documentation

https://laravel.com/docs/8.x/eloquent-relationships#one-to-many-inverse

Snippet from Laravel documentation.

However, if the foreign key for your relationship does not follow these conventions, you may pass a custom foreign key name as the second argument to the belongsTo method:

public function post()
{
    return $this->belongsTo(Post::class, 'foreign_key');
}

If your parent model does not use id as its primary key, or you wish to find the associated model using a different column, you may pass a third argument to the belongsTo method specifying your parent table's custom key:

/**
 * Get the post that owns the comment.
 */
public function post()
{
    return $this->belongsTo(Post::class, 'foreign_key', 'owner_key');
} 

Upvotes: 1

Alper
Alper

Reputation: 160

change this:

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

to this;

public function Restaurant(){
    return $this->belongsTo(Restaurant::class, 'id_restaurant');
}

and similar,

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

to;

***(hasMany)

public function Order(){
    return $this->hasMany(Order::class, 'id_restaurant');
}

Upvotes: 1

Related Questions