Home > Mobile >  Is it possible to pass blade directive like @if into a string from laravel
Is it possible to pass blade directive like @if into a string from laravel

Time:04-10

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) . '"  style="width: 170px; object-fit: cover;"></td>
                <td>' . $category->name . '</td>
                <td>' . $category->category_code . '</td>
                <td><a href="' . route("category.edit", $category->id) . '" >
                                        <i ></i>
                                    </a>
                                    @if($delete == "yes")
                                    <form action="' . route("category.destroy", $category->id) . '" method="POST"  onsubmit="return confirm("Are you sure to delete the item?")">
                                        @csrf
                                        @method("DELETE")
                                        <button  type="submit"><i ></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!

CodePudding user response:

I think the best way to do this is by using render() method you can create your view file and pass your data and render the final HTML code

Example

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

then you can respond with your rendered HTML

return Response($renderedHtml );

I hope it's helpful

CodePudding user response:

You have to use Concatenation in laravel for parsing string:


    foreach ($categories as $category) {
                $output .=
                    '<tr>
    <td>' . $i   . '</td>
    <td><img src="' . asset($category->photo) . '"  style="width: 170px; object-fit: cover;"></td>
    <td>' . $category->name . '</td>
    <td>' . $category->category_code . '</td>
    <td><a href="' . route("category.edit", $category->id) . '" >
        <i ></i>
        </a>';
        if($delete == "yes"){
            $output .= '<form action="' . route("category.destroy", $category->id) . '" method="POST"  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  type="submit"><i ></i></button>
        </form>';
        }
                $output .='</td></tr>';
            }

CodePudding user response:

Starting from laravel 9.0, you can render blade strings on the fly

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) . '"  style="width: 170px; object-fit: cover;"></td>
                <td>' . $category->name . '</td>
                <td>' . $category->category_code . '</td>
                <td><a href="' . route("category.edit", $category->id) . '" >
                                        <i ></i>
                                    </a>
                                    @if($delete == "yes")
                                    <form action="' . route("category.destroy", $category->id) . '" method="POST"  onsubmit="return confirm("Are you sure to delete the item?")">
                                        @csrf
                                        @method("DELETE")
                                        <button  type="submit"><i ></i></button>
                                    </form>
                                    @endif
                                </td>
                                </tr>';
                }

return Blade::render($output, ['delete' => 'yes']);

If you're on laravel 8.x or lower, you can use this package felixdorn/laravel-render-blade-string or the other answers solutions

  • Related