How to return all content from three variables in one function in Laravel?

Viewed 131

I have three variables articles, users and matches inside one function. I want to be able when I call that function it lists all content from those three variables. Currently when I dd() any variable I get content but I want when function is called to get content from all three. Any help is appreciated. Here is my code.

web.php

Route::get('/dashboard', 'DashboardController@index')->name('dashboard.index');

DashboardController.php

public function getAll()
{
    $articles = Article::get();
    $users = User::get();
    $matches = Match::get();
}
1 Answers

Return an array containing all of the values.

public function getAll()
{
    return [
        'articles' => Article::get(),
        'users' => User::get(),
        'matches' => Match::get()
    ];
}

Then you can select each one by type of value,

$values = getAll();
$articles = $values['articles'];
///etc...
Related