0

I have the below content in my DB-

<p>This is dummy content for testing</p>
{{LandingPageController::getTest()}}

I want to render that into my view. But when I'm rendering this in Laravel view, this {{LandingPageController::getTest()}} is getting displayed as it is stored in DB. I want to call the LandingPageController getTest method in my view.

Please suggest me a quick fix for this.

Landing Page Controller

public function getTest(){
    return "Hello World!!!";
}
daemon
  • 113
  • 3
  • 14
  • check this https://stackoverflow.com/questions/35332784/how-to-call-a-controller-function-inside-a-view-in-laravel-5 – Amol Rokade May 14 '18 at 05:42
  • You need to use the helpers in that case. Check below answer https://stackoverflow.com/questions/35332784/how-to-call-a-controller-function-inside-a-view-in-laravel-5 – Amol Rokade May 14 '18 at 05:44
  • That is working if I'm using this. But in my case I'm loading this {{LandingPageController::getTest()}} from my db. Please suggest me a solution for that – daemon May 14 '18 at 05:44

5 Answers5

1

just make the function static

public static function getTest(){
    return "Hello World!!!";
}

that's the only way you can call it like this {{LandingPageController::getTest()}} but I do advice not to do that in your blade file this not a good code design. you should do $test = LandingPageController::getTest() in the controller that you return the blade view and pass it like this return view('blade_file_name',compact('test')) and in your blade file just do {{$test}}

PS - if you doing it your controller use the class like this use Path\To\Controller\LandingPageController

Chamara Abeysekara
  • 1,272
  • 1
  • 10
  • 28
0

Use namespace for that controller in your blade file. example

namespace App\Http\Controllers\LandingPageController;
Md. Miraj Khan
  • 377
  • 3
  • 15
  • That I had already done. This {{LandingPageController::getTest()}} is stored in my db. And I want to call the getTest method from my view after loading the content from db. – daemon May 14 '18 at 05:29
  • The content is getting displayed as it is stored in database. I want the getTest method to call from my view. – daemon May 14 '18 at 06:06
0

You can evaluate a string as a php code using the eval() function

eval — Evaluate a string as PHP code

But it is highly discouraged.

The eval() language construct is very dangerous because it allows execution of arbitrary PHP code. Its use thus is discouraged. If you have carefully verified that there is no other option than to use this construct, pay special attention not to pass any user provided data into it without properly validating it beforehand.

You can use a generic string, {test} for example, when saving the content in the storage.

<p>This is dummy content for testing</p>
{test}

Then whenever you need to display the actual content, you can simply replace the generic string with the real value. You'll have this line in your blade file:

{{ str_replace('{str}', "Hello World", $content) }}
plmrlnsnts
  • 1,644
  • 12
  • 10
0

Take a look at Helper. You can call helper function in view to render your text or html

jack
  • 589
  • 8
  • 22
0

Got the solution, achieve the functionality with "laravel-shortcodes".

Found a very good tutorial on laravel-shortcodes like wordpress

daemon
  • 113
  • 3
  • 14