Laravel: Weird Unexpected 'endif' - php

I'm trying to load some data from the DB into my View and I have coded this:
#forelse($wallets as $wallet)
<td>{{ $wallet->title }}</td>
<td>{{ $wallet->name }}</td>
<td>
#if(($wallet->is_active)==1)
Active
#else
Deactive
#endif
</td>
<td>
#if(($wallet->is_cachable)==1)
Cachable
#else
Un Cachable
#endif
</td>
#endforelse
But I get this:
syntax error, unexpected 'endif' (T_ENDIF)
However, as you can see I have closed the ifs properly by saying: #endif
So what is going wrong here?

Your syntax for #forelse is wrong.
You need to place an #empty somewhere in-between which will be rendered when $wallets is empty:
#forelse($wallets as $wallet)
<td>{{ $wallet->title }}</td>
<td>{{ $wallet->name }}</td>
<td>
#if(($wallet->is_active)==1)
Active
#else
Deactive
#endif
</td>
<td>
#if(($wallet->is_cachable)==1)
Cachable
#else
Un Cachable
#endif
</td>
#empty
<p>No wallet data available</p>
#endforelse
Looping in blade.

Related

Give role acess in php?

Is it right approach to write Laravel PHP code to give a role to hr to view certain column? Because after hosting the website stops automatically and start automatically. Code is working fine..
I am new to PHP, I have no idea if i am going wrong in this code can anyone help me out yrr...
<table class="table table-striped mb-0 dataTable">
<thead>
<tr>
#role('company')
<th>{{__('Employee Name')}}</th>
#endrole
#role('hr')
<th>{{__('Employee Name')}}</th>
#endrole
<th>{{__('Designation')}}</th>
<th>{{__('Promotion Title')}}</th>
<th>{{__('Promotion Date')}}</th>
<th>{{__('Description')}}</th>
#if(Gate::check('Edit Promotion') || Gate::check('Delete Promotion'))
<th width="200px">{{__('Action')}}</th>
#endif
</tr>
</thead>
<tbody class="font-style">
#foreach ($promotions as $promotion)
<tr>
#role('company')
<td>{{ !empty($promotion->employee())?$promotion->employee()->name:'' }}</td>
#endrole
#role('hr')
<td>{{ !empty($promotion->employee())?$promotion->employee()->name:'' }}</td>
#endrole
<td>{{ !empty($promotion->designation())?$promotion->designation()->name:'' }}</td>
<td>{{ $promotion->promotion_title }}</td>
<td>{{ \Auth::user()->dateFormat($promotion->promotion_date) }}</td>
<td>{{ $promotion->description }}</td>
#if(Gate::check('Edit Promotion') || Gate::check('Delete Promotion'))
<td>
#can('Edit Promotion')
<i class="fas fa-pencil-alt"></i>
#endcan
#can('Delete Promotion')
<i class="fas fa-trash"></i>
{!! Form::open(['method' => 'DELETE', 'route' => ['promotion.destroy', $promotion->id],'id'=>'delete-form-'.$promotion->id]) !!}
{!! Form::close() !!}
#endif
</td>
#endif
</tr>
#endforeach
</tbody>
</table>
You can find what you are asking in the documentation here (version select on the left):
https://spatie.be/docs/laravel-permission/v5/basic-usage/blade-directives
#role('hr')
I am from hr!
#else
I am not from hr...
#endrole
It further reads #hasrole('hr') is an alias of #role('hr'), so no worries there.
Similarly, the same goes for permissions:
#can('edit articles')
//
#endcan
For more information, see link mentioned above.

laravel id value changed after adding a nested loop

I used the following code to display the role of the user
however, when I want to edit or delete it leads me to the deleted ids number 2/3 it return the same thing even when I change the for else with foreach Attempt to read property "id" on null (View: C:\laravel\gauto\resources\views\dashboard\user\edit.blade.php)!
when I delete the nested foreach loop everything back to normal
thank you in advance
#forelse($users as $item)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $item->name }}</td>
<td>{{ $item->email }}</td>
<td>
#if($item->email_verified_at)
{{ "verified" }}
#else
{{ "not verified" }}
#endif
</td>
<td>
#foreach($item->roles as $item)
{{ $item['name'] }}
#endforeach
</td>
<td>
Edit
<form action="{{ route('user.destroy', $item->id) }}" method="POST">
#csrf
#method('delete')
<button onclick="return confirm('Are you sure ??')" type="submit">Delete</button>
</form>
</td>
</tr>
#empty
<tr>
blank data
</tr>
#endforelse
Your users are also saved in $item and in the loop you are allocating $item a different value every time.
Try changing the code to
<td>
#foreach($item->roles as $role)
{{ $role['name'] }}
#endforeach
</td>

Why would the VSC IDE Not ignore the commented out code?

Why would the VSC IDE Not ignore the commented-out code?
As I understand it, an object property of an object is accessed like this:
$parentobj->$daughterobj.
So, this should be OK. I have an index page that builds a table.
#foreach($events as $event)
<tr>
<td>{{ $event->id }}</td>
<td>{{ $event->eventTitle }}</td>
<td>{{ substr($event->eventDescription, 0, 24)}}</td>
{{-- the IDE trips on the next line, Undefined variable: memory. -->
<td>{{ $event->$memory->id}}</td>
<td>{{ $event->$memory->memTitle}}</td>
<td>{{ substr($event->$memory->memBody,0,24)}}</td>```
**{{-- if I comment them with {{-- --}} it still complains about the Undefined variable: memory**
```<td>{{ date('Y-m-d', strtotime($event->created_at)) }}</td>
<td>
Show
Edit
<form action="/postmem/{{$event->id}}" method="post" class="d-inline">
{{ csrf_field() }}
#method('DELETE')
<button class="btn btn-danger" type="submit">Delete</button>
</form>
</td>
</tr>
#endforeach
Can someone tell me why the IDE is reading commented out lines?
Because I was using the wrong commenting characters. Should be <!-- -->, not {!-- --}. Bad me.

ErrorException (E_ERROR) rawurlencode() expects parameter 1 to be string, object given '

ErrorException (E_ERROR) rawurlencode() expects parameter 1 to be string, object given '
I am trying to display data from two tables and am getting the above error message, can somebody help translate this error message?
Here is the Controller
public function index()
{
$maintenances = DB::table('tenants')->select('tenants.lastname','tenants.firstname','maintenances.m_status','tenants.designation', 'maintenances.description','maintenances.building_section','maintenances.category','maintenances.reported_date')
->join('maintenances','maintenances.tenants_id','=','tenants.id')
->get();
//dd($maintenances);
return view('agent/maintenance_list', compact('maintenances', 'assetTenants', 'tenants'));
}
And view
#foreach($maintenances as $maintenance)
<tr>
<td class="text-center">
<div class="checkbox-custom">
<input id="product-01" type="checkbox" value="01">
<label for="product-01" class="pl-0"> </label>
</div>
</td>
<td>{{ $maintenance->designation }} {{ $maintenance->firstname }} {{ $maintenance->lastname }}</td>
<td>{{ $maintenance->category }}</td>
<td>{{ $maintenance->building_section }}</td>
<td>{{ $maintenance->description }}</td>
<td>{{ $maintenance->reported_date }}</td>
<td>{{ $maintenance->m_status }}</td>
<td class="text-center">
<div role="group" aria-label="Basic example" class="btn-group btn-group-sm">
<i class="ti-pencil"></i>
</div>
</td>
</tr>
#endforeach
With the route
Route::get('maintenance_list', 'MaintenanceController#index')->name('/maintenance_list');
This error message indicate that something is route with your url, in this case from the view. To solve this problem, this code
<i class="ti-pencil"></i>
as to change to
<i class="ti-pencil"></i>

get fields of an object with twig

I have a problem on a personal project and need your help.
I have an entity bien, users can list all bien entities from the database in a table with all the fields of the entity. I'd like to make an administration page where people with proper access can choose the fields to display on this listing.
I created a system with a JSON file ("field" => <boolean>). I can get the admin choice and create the appropriate JSON, however there is a problem when I try to display the list with only the fields that are set to true. I wanted to display it with something like this: (you can see the former way to display the table which is now commented)
{% for bien in biens %}
<tr>
{% for categorie, affichage in donnee %}
{% if affichage %}
<td>{{ bien.{{ categorie }}}}</td>
{% endif %}
{% endfor %}
{# former way #}
<td>{{ bien.id }}</td>
<td>{{ bien.titre }}</td>
<td>{{ bien.description }}</td>
<td>{{ bien.type }}</td>
<td>{{ bien.surfaceHabitable }}</td>
<td>{{ bien.nombrePiece }}</td>
<td>{{ bien.nombreEtage }}</td>
<td>{% if bien.sousSol %}Yes{% else %}No{% endif %}</td>
<td>{{ bien.prix }}</td>
<td>{{ bien.honoraire }}</td>
<td>{{ bien.charge }}</td>
<td>{{ bien.adresse }}</td>
<td>{{ bien.codePostal }}</td>
<td>{{ bien.ville }}</td>#}
<td>
<ul>
<li>
Voir
</li>
<li>
Editer
</li>
</ul>
</td>
{# end former way #}
</tr>
{% endfor %}
But I have an error on the line
<td>{{ bien.{{ categorie }}}}</td>
Expected name or number.
I think I am not making it the right way (I also tried {{ bien.categorie }}without goodthe proper result).
Thank you for your help ;)

Categories