dimanche 17 juin 2018

laravel hasmany relationship view in blade laravel 5.6

i have a hasmany relationship in my code that i want to show it in my view but i get null error here is my code this my model that i define relations

 public function products()
{
    return $this->hasMany('App\Product', 'id', 'product_id');
}

and here is my controller

$products = Invoice::with('products')->get();
    return view('admin.invoices.show', compact('invoice', $invoice),compact('clients',$clients))->with(compact('products',$products));
}

and finally here the view file

 @foreach($products as $product)
                            <td></td>
                        @endforeach

and when i dd the $products i get this result

#relations: array:1 [▼
    "products" => Collection {#321 ▼
      #items: array:1 [▼
        0 => Product {#318 ▼
          #fillable: array:5 [▶]
          #connection: "mysql"
          #table: null
          #primaryKey: "id"
          #keyType: "int"
          +incrementing: true
          #with: []
          #withCount: []
          #perPage: 15
          +exists: true
          +wasRecentlyCreated: false
          #attributes: array:8 [▼
            "id" => 1
            "name" => "asd"

with this code i get the null error "no products" thanks for help



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2t3UiCL
via IFTTT

Aucun commentaire:

Enregistrer un commentaire