I have a loop foreach in a blade template where i print data from a specific model, the problem is I am not able of getting the value of "$pedido->proveedor()->first()->name" in the code giveing me this error "ErrorException (E_UNKNOWN) Trying to get property of non-object (View: C:..":
#foreach($pedidos as $pedido)
<tr>
<td>
{{ $pedido->id }}
</td>
<td>
{{ $pedido->proveedor()->first()->name }}
</td>
<td>
{{ date('d/m/Y', $pedido->fecha) }}
</td>
<td>
<a onclick="return confirm('deseas borar este registro?')" class="btn btn-danger btn-xs fullButton">Borrar</a>
</td>
</tr>
#endforeach
The weird thing here is when I code this "$pedido->proveedor()->first()" in side the loop of the template I get an object like this:
{"name":"nombre","domicilio":"domicilio","cp":"46006","poblacion":"poblacion","ciudad":"ciudad","pais":"pais"}
but coding this "$pedido->proveedor()->first()->name" I get the error:
the data is sent from a controller:
public function listPedidos()
{
$pedidos = Pedido::all();
// this next pice of code shows me i can get the name as spected but only from php
// foreach($pedidos as $pedido){
// ddd($pedido->proveedor()->first()->name);exit;
// }
return View::make('pedidos/pedidos-list')->with('pedidos', $pedidos);
}
Another weirder thing is that I have the same code with different model and it is working.
Thanks in advance for any help. ;)
You should use:
{{ $pedido->proveedor->name }}
Problem solved:
the previos answer was ok. I can use:
{{ $pedido->proveedor()->first()->name }}
or
{{ $pedido->proveedor->name }}
but because in one of the way of the loop there was not content to refer to i.e($pedido->proveedor didn't exixt) I use this:
{{ isset($pedido->Proveedor->name )?$pedido->Proveedor->name :''; }}
Related
I try to ech the object of a laravel many-to-many relation in blade:
When I try to find the created_at with $product->id=47 and echo it out like so:
{{ $inquiry->products->find($product->id)->created_at }}
I get the error: "Trying to get property 'created_at' of non-object..."
When I remove the $product->id and change it to "47" everything works find:
{{ $inquiry->products->find(47)->created_at }}
But I need to query with the variable $product->id.
Any ideas what's wrong here?
Here is a longer part of my code:
#foreach ($upgrades as $upgrade)
{{ $upgrade->id }} //Echos exactly as 47
//List all upgrades
<select>
#if($inquiry->products->where('id', $upgrade->id)->count() == 1)
<option> {{ $inquiry->products->find($upgrade->id)->pivot->duration }}
#endif
</select>
#endforeach
When I try to use $student->links() I see this error :
Facade\Ignition\Exceptions\ViewException
Call to undefined method App\Student::links()
I checked the controller, model etc but all of them seem OK... How can I fix this?
(I tried this code both on my Macbook and VPS -CentOS7- but same problem occurs)
That part of my view looks like this:
</tr>
#endforeach
</tbody>
</table>
{{ $student->links() }}
</div>
#endsection
Change
{{ $student->links() }}
to
{{ $students->links() }}
(use plural form).
You need to paginate in your backend code. $students = App\Student::paginate(15);
And then you can access the links()
<div class="container">
#foreach ($students as $student)
{{ $student->name }}
#endforeach
</div>
{{ $students->links() }}
I am fetching records with pagination. The first page is working fine but when I click on page number 2 then it return following error
Undefined offset: 0
I am trying following script in controller
$Jobs = Jobs::orderBy('job_id', 'asc')->paginate(5);
return view('veteran.job-posting.index',['Jobs'=>$Jobs]);
In the blade following code I am trying
#if($Jobs)
#foreach($Jobs as $Job)
{{ ucfirst(trans($Job->job_title)) }}
#endforeach
#endif
For pagination I am using following script
<div class="pagination-test">
{{ $Jobs->links() }}
</div>
Please guide me why it does returning error when I click on page number 2. I would like to appreciate
After Request For More Detail from #pseudoanime
For more detail about error
2/2) ErrorException
Undefined offset: 0 (View: E:\xampp\htdocs\project\project-sub-root\resources\views\index.blade.php)
Try adding this outside of the loop
{{ $Jobs->links() }}
Can you try to add Pagination like this.
#if(count($Jobs) > 0)
#foreach($Jobs as $Job)
{{ ucfirst(trans($Job->job_title)) }}
#endforeach
{{ $Jobs->links() }}
#endif
With your CSS class
#if(count($Jobs) > 0)
#foreach($Jobs as $Job)
{{ ucfirst(trans($Job->job_title)) }}
#endforeach
<div class="pagination-test">
{{ $Jobs->links() }}
</div>
#endif
Edited Update
Try With With in controller
return view('veteran.job-posting.index')->with('Jobs',$Jobs);
Update your controller's function return and use compact function for to pass jobs to view.
$jobs = Job::orderBy("id", "asc")->paginate(5);
return view("jobs", compact("jobs"));
You need to add this for pagination in blade.
<ul class="pagination" >
{!! $Jobs->render() !!}
</ul>
I have a table what populates from database:
#extends('layouts.master')
#section('main')
#parent
<table border=1 align=center;>
<tr><td rowspan=10><img src="media/productimg/question.jpg" width=250px></td>
<th>Márka</th><td colspan=3>{{ $productdetails->brand }}</td></tr>
<tr><th>Beszállító</th><td colspan=3>{{ $productdetails->supplier }}</td></tr>
<tr><th>{{ $productdetails->type }}</th>
<th colspan=3>{{ $productdetails->name }}</th></tr>
<tr><th>Nettó beszerzési ár</th><td>{{ $productdetails->wholeprice }} Ft</td>
<th rowspan=2>Ár</th><td rowspan=2>{{ $productdetails->price }} Ft</td></tr>
<tr><th>Bruttó beszerzési ár</th><td>{{ $productdetails->wholeprice }} Ft</td>
<tr><th>Vonalkód</th><td>{{ $productdetails->barcode }}</td><th>Raktáron</th><td>{{ $productdetails->count }} {{ $productdetails->dimension }}</td></tr>
<tr><th>Elhelyezkedés</th><td>{{ $productdetails->whereis }} {{ $productdetails->whereis2 }}</td><th>Küszöb</th><td>{{ $productdetails->threshold }} {{ $productdetails->dimension }}</td></tr>
<tr><th>Utolsó rendelés</th><td> NA </td>
<th>Utolsó vásárlás</th><td> NA </td></tr>
<tr><td colspan=5><button class="button">Kép szerkesztése</button>
<button class="button">Termék szerkesztése</button>
<button class="button">Mennyiség szabályozása</button></td></tr>
</table>
#endsection
And I got this error:
ErrorException in 20d205ed160f36c734b8252e44ac81bfa0977988.php line 6:
Trying to get property of non-object
If I replace the table with
<?php print_r($productdetails);?>
I got the the array with the good values.
What going wrong?
You are saying that the array will show as it should be.
You could try for example:
{{ $productdetails['type'] }} instead of {{ $productdetails->type }}
But please post the result of the print_r($productdetails) function so that we can see whats wrong with the code.
The solution was: {{ $productdetails[0]->type }}
Thanks for #rahul_m for his answer:
Once check the structure of array it is having 0th index first,
You should get it like,
{{$product[0]->brand}}
OR
$product = DB::table("inventory")->where("barcode", $id)->first(); //
and get data as echo $product->brand;
Give it a try, it should work.
I am facing a problem while fetching the data from database
view.php
<p>attend</p>
#foreach($attendings as $attending)
#if ($attending->acceptance==1)
{{ $attending->membername }}
#endif
#endforeach
<p>not attend</p>
#foreach($attendings as $attending)
#if ($attending->acceptance==0)
{{ $attending->membername }}
#endif
#endforeach
<br>
<br>
<p>final decision </p>
{{ $attendings->editor_com }}
controller
public function attendx()
{
$attendings = DB::table('attendance')->get();
return view('My_Work.report', ['attendings' => $attendings]);
}
routes
get('/editor/report','DatabaseController#attendx');
But what I'm facing is Undefined variable: attendings
What am I doing wrong?
Note: I am writing the codes in laravel 5.1
You are returning attendings to this view file My_Work.report.blade.php
So you have to place all your code in report.blade.php file .
Remove this line
{{ $attendings->editor_com }}
because this will throw error Trying to get property of non-object