Reputation: 2000
hello i want to know what i am doing wrong that i am getting this error here is my controller and view
$invoice_specs = invoice::find($id);
$view=view('admin.invoices.pdf',compact('invoice_specs',$invoice_specs));
and here is where i have my view
<table class="table ">
<thead>
<tr>
<th>id</th>
<th>name</th>
<th>description</th>
</tr>
</thead>
<tbody>
@foreach($invoice_specs as $invoice_spec)
<tr>
<td>{{$invoice_spec->id}}</td>
<td>{{$invoice_spec->title}}</td>
<td>{{$invoice_spec->description}}</td>
</tr>
@endforeach
</tbody>
</table>
and finally here is when i dd $invoice_specs
#attributes: array:5 [▼
"id" => 3
"title" => "سومین فاکتور"
"description" => "سومین فاکتور"
"created_at" => "2018-06-12 21:31:57"
"updated_at" => "2018-06-12 21:31:57"]
#original: array:5 [▼
"id" => 3
"title" => "سومین فاکتور"
"description" => "سومین فاکتور"
"created_at" => "2018-06-12 21:31:57"
"updated_at" => "2018-06-12 21:31:57"
]
Upvotes: 1
Views: 2417
Reputation: 5608
Do not use foreach
loop there, Its not a collection.
Simply use $invoice_spec->id
straight away. That's it.
We use foreach
or forelse
for a collection.
Upvotes: 1
Reputation: 1559
You should not here iterate over the result. You should do this way in your HTML
<table class="table ">
<thead>
<tr>
<th>id</th>
<th>name</th>
<th>description</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{$invoice_specs->id}}</td>
<td>{{$invoice_specs->title}}</td>
<td>{{$invoice_specs->description}}</td>
</tr>
</tbody>
</table>
Upvotes: 1
Reputation: 191
Your invoice_specs
is a single Model, you may not want to iterate over it using foreach
you may simply try accessing id
property directly on invoice_specs
.
But, it seems like you want to find a number of invoices, but you are using find()
to find by primary key
in which case you will get only one or no result.
Please provide more clarity to your question.
Upvotes: 2
Reputation: 513
The problem is that the static find method you call to retrieve data from database, only returns a single model not a collection. So when in your view you try to use foreach with the variable $invoice_specs
it in fact doesn't loop through collection of models.
In Laravel's docs on Eloquent and retrieving single models you can see that the find
and first
methods only return a single model, which you cannot use into a foreach loop.
This section of Laravel's docs explains how to retrieve models and how to work with them. I suggest you to read that first :)
So currently what you can do if you want to display a single model is get rid of the foreach
loop in your view and in your controller you should rename variable invoice_specs
to invoice_spec
.
Upvotes: 2