1

I want one route with two controllers. However, I can't implement it. I have ExpenseController and IncomeController and my route looks like this:

Route::get('/api/expense/', 'ExpenseController@index');
Route::post('/api/expense', 'ExpenseController@create');

And I want to add the same route with IncomeController

Route::get('/api/expense', 'IncomeController@index');
Route::post('/api/expense', 'IncomeController@create');
İlker Ergün
  • 68
  • 2
  • 7
latenight
  • 135
  • 3
  • 13
  • 3
    No. One route, one controller, because how will it know which one it will go to? You can probably create a sort of intermediate controller to reroute it depending on what's being submitted. – aynber Aug 29 '19 at 17:30
  • @anyber can u give me any example? – latenight Aug 29 '19 at 17:31
  • HTTP method does not provide the needed context? Think @aynber's suggestion makes sense. `$controller->callAction()`... FWIW: https://laravel.io/forum/05-29-2015-one-route-to-many-controllers – ficuscr Aug 29 '19 at 17:33
  • 3
    Possible duplicate of [(Laravel) How to use 2 controllers in 1 route?](https://stackoverflow.com/questions/26091998/laravel-how-to-use-2-controllers-in-1-route) – ficuscr Aug 29 '19 at 17:35
  • How would you know which controller to call? – IGP Aug 29 '19 at 18:56
  • 4
    Please provide details regarding why you feel that you need more than one controller for a route. You almost certainly need to do something differently as this is not how controllers should be used. – Alex Barker Aug 29 '19 at 19:12
  • @AlexBarker i want to use `Expense` and `Income` in the same page – latenight Aug 30 '19 at 10:32
  • You say 'page' and want a 'create' function, which leads me to believe you actually want normal web routes instead of an API. Can I rephrase your question to "How do I list both Expense and Income records one the same page, and how do I create them with a single form?" – JorisJ1 Aug 30 '19 at 15:31
  • @JorisJ1 yep, that what i want, bro.. – latenight Aug 31 '19 at 14:55

1 Answers1

1

No, It is not possible to directly link one route to two controllers.

However, in the comment section is determined that there is no actual need for one route to link to multiple controllers, but rather a single controller that controls multiple models.

You could create a single controller BudgetController that controls both incomes and expenses. Here is an example for showing a list of both on the same page:

routes/web.php

Route::resource('budget', 'BudgetController');

app/Http/Controllers/BudgetController.php

public function index() 
{
    return view('budget.index', [
        'incomes' => Income::all(),
        'expenses' => Expense::all(),
    ])
}

resources/views/budget/index.php

<table>
    @foreach($incomes as $income)
        <tr><td>{{ $income->amount }}</td></tr>
    @endforeach
</table>

<table>
    @foreach($expenses as $expense)
        <tr><td>{{ $expense->amount }}</td></tr>
    @endforeach
</table>
JorisJ1
  • 922
  • 2
  • 12
  • 22
  • 1
    Looking at your other questions I realise you are probably using Vue, making my answer useless. Is this correct? If so, I will delete this answer. – JorisJ1 Sep 01 '19 at 10:48