skip to Main Content

I want add PHP-code as text in laravel blade. (Something like adding code in stackoverflow)

in blade file I have:

{!! nl2br($article->body_ru) !!}

in $article->body_ru I’m add

huijlhgfyuhukik
rfthkgjb
ftygyjh
<code>
<?php

namespace AppServices;

use IlluminateSupportFacadesApp;
use IlluminateSupportCollection;
use stdClass;

class WeatherService
{

}
</code>

And I’m want to see something like this in body:

huijlhgfyuhukik
rfthkgjb
ftygyjh

<?php

namespace AppServices;

use IlluminateSupportFacadesApp;
use IlluminateSupportCollection;
use stdClass;

class WeatherService
{

}

2

Answers


  1. Sorry but I am unable to completely understand what you are trying to achieve here.
    If you could elaborate, that might help people in answering with some relevant work-around.

    If you just want to print this in the output: "huijlhgfyuhukik rfthkgjb ftygyjh"

    Put this text in a compact variable, let’s call it $output.

    function index() {
        $output = "huijlhgfyuhukik rfthkgjb ftygyjh";
        return compact( 'output' );
    }
    

    And use it in the blade file like this:

    {!! $output !!}
    
    Login or Signup to reply.
  2. Blade files get compiled to raw PHP files and they can contain PHP. This would be the same exact thing if you had a regular PHP file and you wanted to output that text. You could use a HTML entity, &lt;, instead of the opening < for the <?php tag so it doesn’t go into PHP mode and will be rendered as < in the HTML by the browser:

    <code>
    &lt;?php
    ...
    </code>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search