是否可以将类似@if的blade指令从laravel传递到字符串中



这是我在控制器中尝试的代码

$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);

刀片文件中的代码,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);
})
})

输出看起来像这样所有的刀片指令都显示为字符串

如果有办法发展这一点,任何解决方案都会非常棒。谢谢

我认为最好的方法是使用render()方法您可以创建视图文件,传递数据并呈现最终的HTML代码

示例

$renderedHtml = view('your_view_file',compact('some_data'))->render();

然后你可以用你渲染的HTML 进行响应

return Response($renderedHtml );

我希望这对有帮助

您必须在laravel中使用连接来解析字符串:


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"){
$output .= '<form action="' . route("category.destroy", $category->id) . '" method="POST" class="d-inline-block" onsubmit="return confirm("Are you sure to delete the item?")">';
$output .= '<input type="hidden" name="_token" value="'.csrf_token().'">';
$output .= '<input type="hidden" name="_method" value="DELETE"><button class="btn btn-outline-danger" type="submit"><i class="icofont-close icofont-1x"></i></button>
</form>';
}
$output .='</td></tr>';
}

从laravel 9.0开始,您可以在飞行中渲染刀片字符串

use IlluminateSupportFacadesBlade;
//...
$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 Blade::render($output, ['delete' => 'yes']);

如果你在laravel 8.x或更低版本上,你可以使用这个包felixdorn/laravel render blade string或其他答案解决方案

最新更新