laravellaravel-blade

Laravel 12: how to send html data from controller to lay-out with yield


I want to use the blade system of Laravel 12 by extending a lay-out.

@extends('layouts.newslayout')

@foreach($data as $item)
@section('title', $item->title)
@section('newsimage', $item->image)
@section('newscontent', $item->long)
@endforeach

On the lay-out page:

 <p><blockquote>@yield('newscontent')</p>

When I use the code above: the $item-long shows HTML code on the webpage in the browser. I know there is a possibility to use something like:
{! $item->long !} But this code returns the following error: syntax error, unexpected token "{", expecting ")"

Any suggestions?

Tried already to change the syntax of the @section parameters in different ways.


Solution

  • Anything passed as a second parameter into the @section parameter is subjected to the encoding function, so anything you pass can't have HTML. Instead, you need to place it inside the @section directive like this:

    @section('newscontent')
    {!! $item->long !!} 
    @endsection