是否可以将像 @if 这样的 blade 指令传递到来自 laravel 的字符串中

Is it possible to pass blade directive like @if into a string from 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);

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

输出看起来像这样all of the blade directive are showing as string

有没有办法开发这个,任何解决方案都会非常有用。 谢谢!

您必须在 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 开始,您可以即时渲染 blade 个字符串

use Illuminate\Support\Facades\Blade;

//...

$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 或其他答案解决方案

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

示例

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

然后您可以使用渲染后的内容进行响应 HTML

return Response($renderedHtml );

希望对您有所帮助