DENNIS KITHINJI
DENNIS KITHINJI

Reputation: 236

How to return multiple variables from a function in a controller

I would like to return two variables from my controller function. The purpose of this is to use them both in my index.blade.php form.

public function index()

    {
        $checkauth=Auth::check();
        $postl= PostModell::orderby('created_at','desc')->paginate(4);

        return view ('posts.index')->with('postl',$postl);
    }

From the above sample code the two variables are $checkauth and $postl

Upvotes: 2

Views: 1333

Answers (1)

Alexey Mezenin
Alexey Mezenin

Reputation: 163748

You can use this syntax:

return view ('posts.index', compact('postl', 'checkauth'));

Or:

return view ('posts.index', ['postl' => $postl, 'checkauth' => $checkauth]);

But actually you don't need to pass $checkauth because you can do the check in the view directly with:

@if (auth()->check())

Or even with the @auth directive:

@auth
    {{-- The user is authenticated --}}
@endauth

Upvotes: 5

Related Questions