John Davidson

php - Is it possible to pass blade directive like @if into a string from laravel

0 comments
Message:


Here is the code i have tried in controller


$categories = Category::where('categories.name', 'LIKE', '%' . $category_name . '%')
->where('categories.category_code', 'LIKE', '%' . $category_code . '%')->get();
$category_data = $categories->count();
if ($category_data > 0) {
$i = 1;
foreach ($categories as $category) {
$output .=
'<tr>
<td>' . $i++ . '</td>
<td><img src="' . asset($category->photo) . '" class="img-fluid" style="width: 170px; object-fit: cover;"></td>
<td>' . $category->name . '</td>
<td>' . $category->category_code . '</td>
<td><a href="' . route("category.edit", $category->id) . '" class="btn btn-warning">
<i class="icofont-ui-settings icofont-1x"></i>
</a>
@if($delete == "yes")
<form action="' . route("category.destroy", $category->id) . '" method="POST" class="d-inline-block" onsubmit="return confirm("Are you sure to delete the item?")">
@csrf
@method("DELETE")
<button class="btn btn-outline-danger" type="submit"><i class="icofont-close icofont-1x"></i></button>
</form>
@endif
</td>
</tr>';
}
return Response($output);

Code in blade file, jquery


$('#filtersearch').click(function() {
var category_name = $('#category_name').val();
var category_code = $('#category_code').val();
// alert(category_name)
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
})

$.get('/categorysearch', {
category_name: category_name,
category_code: category_code
},
function(data) {
$('#filter_data').html(data);
})
})

the output looks like thisall of the blade directive are showing as string


Is there a way to develop this, any solution would be very greatful.
Thanks!



My Answer:
No, it is not possible to pass blade directives like @if into a string from Laravel controller. Blade directives are meant to be used in Blade templates for conditional rendering and other logic. If you need to perform conditional logic in your controller, you can use regular PHP if statements.

Rate this post

4 of 5 based on 3144 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us