Trying to get property 'firstname' of non-object Laravel 5.7 - laravel-5.7

I have a question why I'm getting undefined variable in my blade view "Trying to get property 'firstname' of non-object (View: C:\xampp\htdocs\focus\resources\views\assets\asset.blade.php)"
asset.blade.php
<tbody>
#foreach($assets as $asset)
<tr>
<td>{{ $asset->employee->firstname }} {{ $asset->$employee->lastname }}</td>
<!--this where the error came-->
<td>{{ $asset->employee->account->name }}</td>
<td>{{ $asset->employee->position }}</td>
<td>{{ $asset->workstation }}</td>
<td></td>
<td></td>
<td>{{ $asset->remarks }}</td>
<td>
</td>
</tr>
#endforeach
</tbody>
Asset.php
public function employee()
{
return $this->belongsTo('App\Employee');
}
Employee.php
public function asset()
{
return $this->hasMany('App\Asset');
}
I haven't really no idea where this error came from
output of dd($assets)

Try $asset->employee->x instead of $asset->$employee->x.
Notice the $.
When you write $asset->$employee it assumes $employee exists, which does not, hence the error.

Related

Echo out from a Laravel 8 Eloquent collection

I have this collection
How can I echo the relations in Blade? I try this code:
#forelse($articles as $article)
<tr>
<td>{{ $article->title }}</td>
<td>{{ $article->excerpt }}</td>
<td>{{ $article->body }}</td>
<td>{{ $article->colors->name }}</td>
...
But I get: "Trying to get property 'name' of non-object (View: C:\xampp\htdocs\laravel\resources\views\articles\index.blade.php)"
You have a Collection (albeit of only a single Color), so you'll need either a nested #foreach(), or a pluck():
#forelse($articles as $article)
<tr>
<td>{{ $article->title }}</td>
...
#foreach($article->colors as $color)
// Do something with `$color->name`
#endforeach
</tr>
#empty
...
#endforelse
// OR
#forelse($articles as $article)
<tr>
<td>{{ $article->title }}</td>
...
<td>{{ $article->colors->pluck('name')->implode(', ') }}</td> // Purple
</tr>
#empty
...
#endforelse
Basically, you can't echo a single property (like ->name) from a Collection. The code won't know which one you're trying to return. This applies even if the relationship returns a single record. If you only want a single color, then you'd need to modify your relationship to as hasOne(), then you'd be able to do something like:
$article->color->name ?? 'None'

Laravel pagination without style

Below is my code:
<tbody>
#foreach($posts as $post)
<tr>
<th>{{ $post->id }}</th>
<td>{{ $post->title }}</td>
<td>{{ substr($post->body, 0, 50) }} {{ strlen($post->body) > 50 ? "..." : "" }}</td>
<td>{{ date('M j,Y h:i a',strtotime($post->created_at)) }}</td>
<td>
View
Edit
</td>
</tr>
#endforeach
</tbody>
</table>
<div class="text-center">
{{ $posts->links() }}
</div>
And below is the code in controller:
public function index()
{
$posts = Post::orderBy('id', 'desc')->paginate(5);
return view('posts.index')->withPosts($posts);
}
In spite of this, i do not get proper styling!
Bootstrap styles not applied. See the image.
This was the issue with new Bootstrap version 4.1 with Laravel's latest version.
I tried with the earlier version of bootstrap and it is resolved now.

how to separate a row result to grups in TWIG

Form query i have several rows record
Controller:
public function statusTaskAction()
{
$em = $this->getDoctrine()->getManager();
$statusAll = $em->getRepository( 'MyBundle:StatusTask' )
->allStatusQuery();
return [ 'rows' => $statusAll ];
}
statusTask.html.twig
<table class="table ">
<tbody>
{% for entry in row %}
<tr>
<td>{{ entry.endDate is null ? '' : entry.endDate }}</td>
<td>{{ entry.groupID }}</td>
<td>{{ entry.name }}</td>
<td>{{ entry.info }}</td>
</tr>
{% else %}
<tr>
<td colspan="4">No rows</td>
</tr>
{% endfor %}
</tbody>
</table>
groupID displayed status:
g - "Green"
r - "Red"
f - "Future to plan"
p - "Planed"
I'll try to separate result by groupID and for this result add label, decription to each displayed grupID in TWIG and separate it in TWIG.
How can I do it in TWIG?
Since row is an Entity, you don't call the column name, but instead the Entity getters like so:
<table class="table ">
<tbody>
{% for entry in rows %}
<tr>
<td>{{ entry.getDate is null ? '' : entry.getDate }}</td>
<td>{{ entry.getGroupID }}</td>
<td>{{ entry.getName }}</td>
<td>{{ entry.getInfo }}</td>
</tr>
{% else %}
<tr>
<td colspan="4">No rows</td>
</tr>
{% endfor %}
</tbody>
</table>
I presume that would be the naming in your Entity class for the Entity StatuTask. Also, I think you need to use rows as specified in your controller.

Symfony2, doctrine2, can I return an array as a response in the controller?

I've got an embedded controller that takes in a parameter and retrieves from the db.
{% for key, value in boxes %}
{{ render(controller('ABundle:Reports:getBox', { 'boxnum' : value.boxnum })) }}
<tr>
<td>{{ value.boxnum }}</td>
<td>{{ value.boxname }}</td>
<td>( # of total box )</td>
<td>( # of available box)</td>
</tr>
{% endfor %}
Controller:
public function getBox($boxnum) {
$em = $this->getDoctrine()->getRepository('ABundle:MainBoxes');
$getbox = $em->availBoxes($boxnum);
return new Response(array('getbox' => $getbox)); // this doesn't work obviously...
}
I want the results to retrieve db and print it out in a loop. Something like
<tr>
<td>{{ value.boxnum }}</td>
<td>{{ value.boxname }}</td>
<td>{{ getbox.totalbox }}</td>
<td>{{ getbox.availbox }}</td>
</tr>
I'm getting an error for the wrong response return. It's not allowing me to return an array?
An exception has been thrown during the rendering of a template ("The Response
content must be a string or object implementing __toString(), "array" given.")

An attribute is not displayed in twig

I have an attribute immobilier.ref that is not displayed with twig
this is the code
{% for immobilier in listImmobilier %}
<tr>
<td>{{ immobilier.id }}</td>
<td>{{ immobilier.ref }}</td>
<td>{{ immobilier.titre }}</td>
<td><span class="glyphicon glyphicon-eye-open" style="display:block; text-align:center"></span></td>
<td>Edit</td>
<td>Delete</td>
</tr>
{% endfor %}
this is a photo of the table
It(s fine now, it was because I have a function named ref in the entity
/**
* #ORM\PostPersist()
*/
public function ref()
{
$id = $this->getId();
$operation = $this->getOperation()->getRef();
$this->setRef($id.$operation);
}
I have changed her name and it work fine now.

Resources