Fetch Auth::user()->name from Database

Published 6 months ago by Kaustubh

Hi I have query table

id  |       query       |           output
1   |   What is my name |   your name is {!! auth::user()->name !!}

when i fetch data in blade i get this output your name is {!! auth::user()->name !!}

but i want your name is "USER NAME"

Best Answer (As Selected By Kaustubh)
Dry7

@Kaustubh you need to replace the name in the controller like this

$item = YourModel::find(1);
$item->output = str_replace('{!! auth::user()->name !!}', Auth::user()->name, $item->output);
return $item->output;

{!! auth::user()->name !!} can be replaced by something like #NAME#

bobbybouwmann

Can you show your full blade file? You probably some syntax mistake!

Also if you want to output a variable you should use {{ $variable }}. {!! $variable !!} is used for outputting html and things that don't need escapes.

Kaustubh

actuall i am appending the response on ajax success

$("#chat-section ul").append('<li><div class="left-chat"><img src="https://images.g2crowd.com/uploads/product/image/large_detail/large_detail_1508920769/chatbotsbuilder.png"><p>'+data+'</p></div></li>');

The 'data' contain this output 'your name is {!! auth::user()->name !!}'
Devmaurice

You are displaying data in javascript so blade part will not be translated/compiled by blade templating engine. You must use it directly in a blade file.

Dry7
Dry7
6 months ago (152,030 XP)

@Kaustubh you need to replace the name in the controller like this

$item = YourModel::find(1);
$item->output = str_replace('{!! auth::user()->name !!}', Auth::user()->name, $item->output);
return $item->output;

{!! auth::user()->name !!} can be replaced by something like #NAME#

Please sign in or create an account to participate in this conversation.