skip to Main Content

Hello i am using the Eloquent many to many method to get values from 2 tables and show its values.

This is the Order model code:

<?php

namespace AppModels;

use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;

class Order extends Model
{
    use HasFactory;

    public $timestamps = true;

    protected $fillable = [
        'order_number',
        'client',
        'description',
    ];

    public function clients()
    {
        return $this->belongsToMany(Client::class); 
    }
}

Here is the controller code to open a new window with all the values from table B

 public function index()
    {
        $orders = Order::all();
        return view('orders/index',compact('orders'))
            ->with('i', (request()->input('page', 1) - 1) * 5);
    }

Then this is what i am doin in the view:

@foreach($orders as $order)
{{$order->name}}
@endforeach
 //this works

but if i do

@foreach($orders->clients as $client)
{{$client->name}}
@endforeach
//i get the error

Property [clients] does not exist on this collection instance.
Even though is declared in the model Order

2

Answers


  1. can you try to include the model in the Order class as you have not defined it. Check below

    namespace AppModels;
    
    use IlluminateDatabaseEloquentFactoriesHasFactory;
    use IlluminateDatabaseEloquentModel;
    
    class Order extends Model
    {
        use HasFactory;
    
        public $timestamps = true;
    
        protected $fillable = [
            'order_number',
            'client',
            'description',
        ];
    
        public function clients()
        {
            return $this->belongsToMany('AppModelsClient'); 
        }
    }
    

    Alternatively

    namespace AppModels;
    
    use IlluminateDatabaseEloquentFactoriesHasFactory;
    use IlluminateDatabaseEloquentModel;
    use AppModelsClient;
    
    class Order extends Model
    {
        use HasFactory;
    
        public $timestamps = true;
    
        protected $fillable = [
            'order_number',
            'client',
            'description',
        ];
    
        public function clients()
        {
            return $this->belongsToMany(Client::class); 
        }
    }
    

    both above would then make you able to reference the relationship.

    All the best.

    Login or Signup to reply.
  2. Your error is that you are assuming you are getting clients, that is true, but you are not getting them as a property like $orders->clients, $orders is a Collection, that is what BelongsToMany returns.

    So, your code should be:

    @foreach($orders as $order)
        {{ $order->name }}
    @endforeach
    

    Due to each entry on a Collection is going to be an Order object, that is why $order->name works ($order is literally an Order).

    The only time $orders->clients is going to work for you, is when $order is an Order:

    $order1 = Order::find(1);
    
    // Or
    
    $order2 = new Order;
    
    // Then you have ->clients available
    
    $clients1 = $order1->clients();
    // This returns a Collection with Client if you have a relationship with $order1
    
    $clients2 = $order2->clients();
    // This return an EMPTY Collection as you have no relation at all on the pivot table with this new, non-saved $order2 object
    
    // Now you can iterate $clients1 assuming you got data back
    foreach ($clients1 as $client) {
        // $client is a normal Client object, you have all the properties and methods available
        // Do whatever you need here
    }
    

    So, when you have a Collection in $orders, there is no "property" (related to the relationship, clients in this case), remember that a Collection is an array with steroids, so the Collection does not know anything about what it has inside of itself.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search