Jaskaran Singh Puri
Jaskaran Singh Puri

Reputation: 739

How can I execute multiple methods at once in a laravel controller

I have a dashboard page where I want to display multiple functionalities like displaying a form, showing a list of articles from database.

But, since routes can allow only one method to run at a time for each route, how can I achieve it?

I want to do something like this

Route::get('/dashboard','Dashboard@index');
Route::get('/dashboard','Dashboard@showArticles');
Route::get('/dashboard','Dashboard@showUsersList');

I know this doesn't work, but what is the alternative? Since I want to do all this on the same page.

Upvotes: 0

Views: 1355

Answers (3)

suguna
suguna

Reputation: 811

You can achieve by using,

public function index()
{
  $users = User::all();
  $articles = Articles::all();
  return view('page.your_view', compact(['users' => $users, 'articles' => 'articles']); 
}

Upvotes: 1

Ronak Dattani
Ronak Dattani

Reputation: 466

Route::get('/dashboard/{?type}','Dashboard@index');

In controller

public function getIndex($type)
{
    if(isset($type) && !empty($type) && $type=='article'){ 
           return $this->article();
    }
  return view('page.index'); 
}
public function article(){
       ...YOUR CODE
}

Upvotes: 0

Komal
Komal

Reputation: 2736

You have to combine all methods in single method like this and pass it to view

public function getIndex()
{
  $users = User::all();
  $articles = Articles::all();
  return view('page.your_view')->with('users', $users)->with('articles', 'articles'); 
}

Upvotes: 1

Related Questions