Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save daniellima/9b9a608cfb227a676822 to your computer and use it in GitHub Desktop.

Select an option

Save daniellima/9b9a608cfb227a676822 to your computer and use it in GitHub Desktop.
A controller that allows you to pass data to views using the controller instance variables
A controller that allows you to pass data to views using the controller instance variables
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
public $data;
public function __set($name, $value){
$this->data[$name] = $value;
}
public function callAction($method, $parameters)
{
$response = parent::callAction($method, $parameters);
if(get_class($response) === \Illuminate\View\View::class){
$response->with($this->data);
}
return $response;
}
}
<?php
class SomeController {
public function index(){
$users = User::all();
return view('index')->with('users', $users);
}
// When you use this controller as your Base, you can do the same with
public function index(){
$this->users = User::all();
return view('index');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment