2016-10-19 77 views
0

我有一个仪表板页面,我想要显示多个功能,如显示表单,显示数据库中的文章列表。如何在laravel控制器中同时执行多个方法

但是,由于路由每次只允许一次运行一个方法,我该如何实现呢?

我想要做这样的事情

Route::get('/dashboard','[email protected]'); 
Route::get('/dashboard','[email protected]'); 
Route::get('/dashboard','[email protected]'); 

我知道这是不行的,但什么是另类?因为我想在同一页面上做所有这些。

+2

没有。该路由调用指定的方法。使用该方法触发您想要的功能。例如,使您的Dashboard @ index方法结合索引,文章和用户的功能。这不是路由器的责任。 –

回答

1

你必须在一个方法的所有方法结合这样的,并通过它来查看

public function getIndex() 
{ 
    $users = User::all(); 
    $articles = Articles::all(); 
    return view('page.your_view')->with('users', $users)->with('articles', 'articles'); 
} 
0
Route::get('/dashboard/{?type}','[email protected]'); 

在控制器

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

您可以通过使用实现,

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