How to call class method inside a laravel view?

Member

by jerad , in category: PHP Frameworks , a year ago

How to call class method inside a laravel view?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by dalton_moen , a year ago

@jerad 

To call a class method inside a Laravel view, you can use the @php blade directive. Here's an example of how you can do it:

  1. In your blade view file, use the @php directive to call the class method. For example:
1
2
3
4
5
6
@php
    // Assuming your class is named "SomeClass" and the method you want to call is named "someMethod"
    $result = AppSomeClass::someMethod();
@endphp

{{ $result }}


  1. Replace AppSomeClass with the actual namespace and class name of the class you want to call the method from.
  2. Replace someMethod with the actual name of the method you want to call.
  3. Output the result inside the blade view using {{ $result }} or any other desired output format.


Remember to autoload the class file by adding the appropriate use statement at the top of the blade view file if it is not already autoloaded.