2

I have a resource controller. The route for that is like this-

Route::resource('branches/{branch_id}/employees', 'EmployeeController');

The problem is in every method I need to pass the branch variable to the view.

public function index($branch_id){
    $branch = Branch::find($branch_id);
    $employees = Employee::orderBy('created_at', 'desc')->get();
    return view('employees.index', compact('branch', 'employees'));
}

Is there any way that I can pass the branch variable to each view returned through this controller?

@Sapnesh Naik Its not a duplicate as I need to manipulate the branch in each function.

Sunny Kumar
  • 534
  • 12
  • 35
  • 4
    Possible duplicate of [How to pass data to all views in Laravel 5?](https://stackoverflow.com/questions/28608527/how-to-pass-data-to-all-views-in-laravel-5) – Sapnesh Naik Feb 12 '18 at 15:49
  • if you need to pass the variable to specific view then https://laravel.com/docs/5.5/views#view-composers, otherwise use `view::share`. – The Alpha Feb 12 '18 at 15:50
  • @SapneshNaik Actually I'm do not totally agree, he's looking to manipulate the `branch_id` in the `controller` not in the `view` – Sebastien D Feb 12 '18 at 15:51
  • @SapneshNaik It sounds like you only read the title of this question. The one you linked is not a duplicate. – Emile Pels Feb 12 '18 at 15:55
  • @SebastienD and Emile exactly... – Sunny Kumar Feb 12 '18 at 15:59
  • @EmilePels He specifically asked "Is there any way that I can pass the branch variable to each view returned through this controller" That obviously means that he needs to pass a variable to each of the view in a controller. Route model binding and using `find()` yeild the same result. So your answer does not answer the intent he expressed in the title as well as at the end of the question – Sapnesh Naik Feb 12 '18 at 15:59
  • @SapneshNaik Its not a duplicate as I need to manipulate the branch in each function, Its not a constant. – Sunny Kumar Feb 12 '18 at 16:01
  • Do you need to pass the variable to every view or just `employees.index`? – The Alpha Feb 12 '18 at 16:02
  • @TheAlpha Every view that is being returned from this controller. – Sunny Kumar Feb 12 '18 at 16:03
  • @SapneshNaik For just a second, think of what problem he is trying to solve - which is that he does not want to have to explicitly fetch the `Branch` model in every handler. Route model binding solves this problem, and the question you linked does not let him do that in a clean way. – Emile Pels Feb 12 '18 at 16:10
  • @EmilePels In the comments he mentions he wants the variable to be passed to "Every view that is being returned from this controller." vpalade s answer achieves that – Sapnesh Naik Feb 12 '18 at 16:13
  • @SapneshNaik Yes, and he also needs the Branch model to be available in each of the controller's handler. This is obviously his intent, although he may have worded it a bit odd. – Emile Pels Feb 12 '18 at 16:15
  • Thanks @SunnyKumar for fixing the mistake in my answer and for the thumbs up, +1 :-) – The Alpha Feb 12 '18 at 16:26
  • 1
    @TheAlpha and Thank you for solving my problem. – Sunny Kumar Feb 12 '18 at 16:27

2 Answers2

1

Add this to your controller constructor:

    public function __construct()
    {
        view()->share('branch', Route::current()->getParameter('brach_id'););
    }
vpalade
  • 1,427
  • 1
  • 16
  • 20
1

In this case, you may try this:

public function __construct(Request $request)
{
    view()->share(
        'branch', Branch::find($request->route('branch_id'))
    );
}

You may also use request()->route('branch_id') if you don't use method injection by type hinting the Request $request in your __construct method.

The Alpha
  • 143,660
  • 29
  • 287
  • 307