How to call a controller function inside a view in Laravel 5?


The controller method you want to use can be added inside the view as shown below –

<?php echo App\Http\Controllers\StudentController::test(); ?>

Here App\Http\Controllers\StudentController is the controller and test() is the method we want to call.

Example 1

The view is student.blade.php.Here we are calling the controller studentController and the method test(). The method test() is defined inside the studentController as shown below −

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Student; use Illuminate\Support\Facades\DB; use Illuminate\Support\Collection; use URL; use Illuminate\Support\Facades\App; class StudentController extends Controller { public function index() { return view('student'); } public function validateform(Request $request) { } public function test() { echo "Testing controller function in view"; } }

HTML File

<html> <head> <title>Student Form </head> <body> @if (count($errors) > 0) <div class = "alert alert-danger"> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <?php echo Form::open(array('url'=>'/student')); echo App\Http\Controllers\StudentController::test(); ?> <table border = '1'> <tr> <td align = 'center' colspan = '2'>Student Registration</td> </tr> <tr> <td>Name</td> <td><?php echo Form::text('name'); ?></td> </tr> <tr> <td>Email</td> <td><?php echo Form::text('email'); ?></td> </tr> <tr> <td>Age</td> <td><?php echo Form::text('age'); ?></td> </tr> <tr> <td>Address <td><?php echo Form::text('address'); ?></td> </tr> <tr> <td align = 'center' colspan = '2'><?php echo Form::submit('submit'); ?></td> </tr> </table> <?php echo Form::close(); ?> </body> </html>

Output

The output of the above code is −

In the above output, we can see the method test() is called and the message we echoed is displayed to the user.

Another way to use the controller inside view is as follows −

Example 2

You can make use of the following code to make use of the controller inside your view.

<?php use App\Http\Controllers\StudentController; StudentController::test(); ?>

After adding the above code, the view is as follows −

<html> <head> <title>Student Form</title> </head> <body> @if (count($errors) > 0) <div class = "alert alert-danger"> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <?php echo Form::open(array('url'=>'/student')); ?> <?php use App\Http\Controllers\StudentController; StudentController::test(); ?> <table border = '1'> <tr> <td align = 'center' colspan = '2'>Student Registration</td> </tr> <tr> <td>Name</td> <td><?php echo Form::text('name'); ?></td> </tr> <tr> <td>Email</td> <td><?php echo Form::text('email'); ?></td> </tr> <tr> <td>Age</td> <td><?php echo Form::text('age'); ?></td> </tr> <tr> <td>Address</td> <td><?php echo Form::text('address'); ?></td> </tr> <tr> <td align = 'center' colspan = '2'><?php echo Form::submit('submit'); ?></td> </tr> </table> <?php echo Form::close(); ?> </body> </html>

Output

The output of the above code is as follows

Example 3

You can also make use of the controller by wrapping it inside the curly braces in the blade template as shown below −

{{ App\Http\Controllers\StudentController::test() }}

Inside view, it will be as follows −

<html> <head> <title>Student Form</title> </head> <body> @if (count($errors) > 0) <div class = "alert alert-danger"> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <?php echo Form::open(array('url'=>'/student')); ?> {{ App\Http\Controllers\StudentController::test() }} <table border = '1'> <tr> <td align = 'center' colspan = '2'>Student Registration</td> </tr> <tr> <td>Name</td> <td><?php echo Form::text('name'); ?></td> </tr> <tr> <td>Email</td> <td><?php echo Form::text('email'); ?></td> </tr> <tr> <td>Age</td> <td><?php echo Form::text('age'); ?></td> </tr> <tr> <td>Address <td><?php echo Form::text('address'); ?></td> </tr> <tr> <td align = 'center' colspan = '2'><?php echo Form::submit('submit'); ?></td> </tr> </table> <?php echo Form::close(); ?> </body> </html>

The controller is as follows −

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Student; //use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\DB; use Illuminate\Support\Collection; use URL; use Illuminate\Support\Facades\App; class StudentController extends Controller{ public function index() { return view('student'); } public function validateform(Request $request) { $data = file_get_contents('php://input'); print_r($data); } public function test() { echo "Testing controller function in view"; } }

Output

The output of the above code is

Updated on: 30-Aug-2022

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements