if else condition in blade file (laravel 5.3) - php

I want to check if/else condition in my blade file. I want to check the condition $user->status =='waiting' as the code given below. Output returns correctly as I expected. But along with my output I caught curly braces {} printed. I want to remove curly braces in result. Is there anything wrong in my if condition ?
#if($user->status =='waiting')
{
<td><a href="#" class="viewPopLink btn btn-default1" role="button" data-id="{{ $user->travel_id }}" data-toggle="modal" data-target="#myModal">Approve/Reject<a></td>
}
#else{
<td>{{ $user->status }}</td>
}
#endif

No curly braces required you can directly write
#if($user->status =='waiting')
<td><a href="#" class="viewPopLink btn btn-default1" role="button" data-id="{{ $user->travel_id }}" data-toggle="modal" data-target="#myModal">Approve/Reject<a></td>
#else
<td>{{ $user->status }}</td>
#endif

I think you are putting one too many curly brackets. Try this
#if($user->status=='waiting')
<td>Approve/Reject </td>
#else
<td>{!! $user->status !!}</td>
#endif

You don't have to use braces in blade conditions, so your code would be like this:
#if($user->status =='waiting')
<td>
<a href="#" class="viewPopLink btn btn-default1" role="button" data-id="{{ $user->travel_id }}" data-toggle="modal" data-target="#myModal">
Approve/Reject
<a>
</td>
#else
<td>
{{ $user->status }}
</td>
#endif

If wants to assign value on basis of if else inside blade.
#if($shipping_fee >= 1)
#php
$shipping_fee = $any_variable1
#endphp
#else
#php
$shipping_fee = $any_variable2
#endphp
#endif

Related

Laravel Pagination does not change the pages

I wanted to add pagination to my project and followed a tutorial for that. When I click new page the results are not changing.
In my Controller I added this $competitions = Competition::latest()->paginate(1);
Then into app/Providers/AppServiceProvider added this use Illuminate\Pagination\Paginator; and inside the boot function added this: Paginator::useBootstrap();
And the last thing was to add this {{ $competitions->onEachSide(1)->links() }} after I close my table in the blade file.
EDIT:
Controller function:
public function index()
{
$competitions = Competition::latest()->paginate(1);
return view('competition.index', compact('competitions'));
}
Loop in blade:
#foreach ($competitions as $competition)
<tbody >
#switch($competition->status)
#case('active')
<tr>
#break
#case('to_push')
<tr class="bg-secondary">
#break
#case('inactive')
<tr class="bg-warning">
#break
#default
<tr>
#endswitch
<td>{{ $competition->id }}</td>
<td>{{ $competition->name }}</td>
<td>{{ $competition->status }}</td>
<td>{{ $competition->mode }}</td>
<td>{{ $competition->type }}</td>
<td>{{ $competition->frequency }}</td>
<td>{{ $competition->last_push }}</td>
<td>{{ $competition->next_push }}</td>
<td class="text-center"><a class="btn btn-info btn-sm" href="{{ route('events.list_by_competition',['competition' => $competition->id]) }}">Events</a></td>
<td class="text-center"><a class="btn btn-info btn-sm" href="{{ route('competitions.exports',$competition->id) }}">Exports</a></td>
<td class="text-center">
<form action="{{ route('competitions.destroy',$competition->id) }}" method="POST">
<a class="btn btn-primary btn-sm" href="{{ route('competitions.edit',$competition->id) }}">Edit</a>
#csrf
#method('DELETE')
<button type="submit" class="btn btn-danger btn-sm">Delete</button>
</form>
</td>
</tr>
</tbody>
#endforeach
</table>
{{ $competitions->links('pagination::bootstrap-5') }}
dd(Competition::all()) returns the 4 records I have
Can you please tell me what I did wrong? Any and all help gratefully received.
$competitions = Competition::latest()->paginate(1) this command will return you one result and then pagination will take 1 per page so it won't be shown at all.
Try something like this in your controller
$competitions = Competition::paginate(1)
return view(...., ['competitions' => $competitions]);
and in your blade add this under the foreach loop where you display data
{{ $competitions->links() }}
Read more here
HERE IS HOW TO MAKE PAGINATION
BLADE
#foreach($paginate as $p)
{{ $p->name }} <br>
#endforeach
{{ $paginate->links() }}
CONTROLLER
return view('backend.test', [
'paginate' => Product::paginate(5)
]);

laravel 8 syntax error, unexpected '<' while calling component in View

Error showing on F:\xampp\htdocs\ERP\resources\views/building/summary.blade.php:20
following is code
#foreach($records as $record)
<tr>
<td>{{ $record->id }}</td>
<td>{{ $record->building->name }}</td>
<td>{{ $record->name }}</td>
<td>{{ $record->created_at }}</td>
<td>
<x-summary-action :view_path="{{ $view_path }}" :record="{{ $record }}">
</x-summary-action>
</td>
</tr>
#endforeach
And Component code is below
<a href="{{ route( $view_path . '.edit', $record->id) }}" class="btn btn-outline-secondary btn-sm edit" title="Edit">
<i class="fas fa-pencil-alt"></i>
</a>
remove the curly brackets when you pass the variables to the components
<x-summary-action :view_path="$view_path" :record="$record">
</x-summary-action>

Getting ID from the table blade.php to other blade.php error Symfony\Component\Routing\Exception\RouteNotFoundException

I have a table view in which I have a view button and redirect to another page carrying the id of the row that has been clicked.
<tbody>
<tr>
<?php $hold=0; ?>
#if(!empty($users))
#foreach($users as $user)
#if($user->role == 3)
<?php $hold++; ?>
<td class="py-1">{{ $user->id }}</td>
<td>{{ $user->name }} </td>
<td>{{ $user->Zone }} </td>
<td>{{ $user->Purok }}</td>
<td> Wala pa sa database </td>
<td>
<div class="template-demo">
<button type="button" onclick="location.href=' {{ route ('SupAd.View_PL_Accnt/'.$user->id) }}'" class="btn btn-outline-info btn-icon-text">
<i class="ti-search btn-icon-append"></i>View
</button>
</div>
</td>
</tr>
#endif
#endforeach
<?php if($hold == 0){
echo "<p><font color=red size='4pt'>No purok leader can be found</font></p>";
}
?>
#endif
I have here my code on web.php route
Route::group(['prefix'=>'SupAd_Purok_Leader_Account', 'middleware'=>['isSupAd', 'auth', 'PreventBackHistory']], function (){
Route::get('View_PL_Accnt', [SupAdController::class, 'View_PL_Accnt'])->name('SupAd.View_PL_Accnt/{id}'); });
And I got the error:
Symfony\Component\Routing\Exception\RouteNotFoundException
Route [SupAd.View_PL_Accnt/3] not defined.
You are placing the route parameter in the wrong place. Try this:
Route::get('View_PL_Accnt/{id}', [SupAdController::class, 'View_PL_Accnt'])->name('SupAd.View_PL_Accnt'); });
The name is used to call a route using the route helper. You can give the url parameters to the route helper in an array. Inside your blade file use:
{{ route ('SupAd.View_PL_Accnt', [$user->id]) }}
you can change routes like
Route::get('View_PL_Accnt/{id}', [SupAdController::class, 'View_PL_Accnt'])->name('SupAd.View_PL_Accnt'); });
And change the button on click event
<button type="button" onclick="location.href='{!! route('SupAd.View_PL_Accnt', [$user->id]) !!}'" class="btn btn-outline-info btn-icon-text">
<i class="ti-search btn-icon-append"></i>View
</button>

How to put HTML inside a <td> in Laravel Blade?

I want to put Bootstrap Icon inside of my blade..But the problem is i couldn't get the actual output..Means here is the sample code:
#if (($row->is_management) === 1)
<td> <span class="glyphicon glyphicon-ok"></span> </td>
#else
<td><span class="glyphicon glyphicon-remove"></span></td>
#endif
Means if status is 1 then green checked will come and in another condition red cross.
Can anyone suggest me what the actual mistake here?
It will work on bootstrap css with glyphicons fonts. Please make sure that u have bootstrap css called and have glyphicons fonts
#if (($row->is_management) == 1)
<td> <span class="glyphicon glyphicon-ok"></span> </td>
#else
<td><span class="glyphicon glyphicon-remove"></span></td>
#endif
Try this, use == instead of ===
Updated Code:
#if (($row->is_management) == 1)
{{ <td> <span class="glyphicon glyphicon-ok"></span> </td> }}
#else
{{ <td><span class="glyphicon glyphicon-remove"></span></td> }}
#endif

Laravel if/else syntax

How do i set different states of the icon? I want it to change between a liked/not liked icon. Can i do that? I don't completely understand the syntax. The btn-success class is for the liked icon, and btn-danger is for the unliked button.
#if(Auth::check())
#if( !$unblock )
<span data-id="{{ $key->id }}" data-like="Like" data-like-active="Remove Like" title="#if(!empty($likeUser)) Remove Like #else Like #endif" class="btn btn-xs pull-left #if(!empty($likeUser)) btn-success #else btn-danger #endif btn-like likeButton #if(!empty($likeUser)) active #endif">
<i class="Like-icon"></i>
</span>
#else
You're almost right just complete the condition after #else
Here is the place to learn more about blades and templates
It is as simple like regular PHP if..else condition
#if (Auth::check())
Authorized
#else
Nope
#endif
If you want to have additional conditions inside then
#if (Auth::check())
Authorized
//your additional conditions for authorized users
#else
Nope
#endif
#if (count($records) === 1)
I have one record!
#elseif (count($records) > 1)
I have multiple records!
#else
I don't have any records!
#endif

Categories