Passing parameters to views from controllers in Laravel

Data from controllers to views may be passed from different methods in Laravel, here we are using arrays from controller to pass data to views.

Route file (routes.php)

<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/

Route::get('/', 'WelcomeController@index');

// this is the auth controller
Route::resource('auth', 'Authors_Controller@index');

Controller file (Authors_Controller.php)

<?php namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class Authors_Controller extends Controller {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		// here we are defining array
		return view('authors.index', array(
										'name' => 'abc',
										'company' => 'abcCompany',
										'address' => 'xyz'
										  )
		);
	}
}

View (index.php)

<h1>Data display</h1>

<?php 

echo "Name is : " . $name . "<br>";
echo "Company is : " . $company . "<br>";
echo "Address is : " . $address . "<br>";

?>

Output

Data display
Name is : abc
Company is : abcCompany
Address is : xyz

Directory Structure

Untitled

Share

You may also like...