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 App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
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