J
J
jazzus2018-12-22 01:40:31
Laravel
jazzus, 2018-12-22 01:40:31

Why is double return needed when using $this in a Laravel controller?

I'm trying to use $this-> in Laravel controllers. So far, I just understand why they are needed. Like, instead of one long logic in one method, you can divide it into small ones, spawn many methods (and methods in classes and controllers) and then connect the desired method through $this->. Of course, if I imagine everything correctly (I'm a beginner :)). So. I'm redirecting to /home. In the index method, I define the user type and call the client() method:

public function index()
    {
        $user = User::where('id', Auth::id())
                          ->firstOrFail();

        $type = $user->type;

        if ($type == 10) {
           return $this->client();
        }
    }

In client() method redirect
public function client()
    {
           return redirect('/clients');
    }

And the question is why in the index method I need to do return $this->client(); and just $this->client(); doesn't work? After all, the client () method already has a return with a redirect, but for some reason it does not work without a return in the index method. Why do we need a double return? It's strange that the return in the client() method is not fired when the method is called. I just looked through the Laravel files and there they simply access the desired method through this without any returnov

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Eugene Pedya, 2018-12-22
@jazzus

Each function or method is responsible for its own return.
function client() - returns its
own function index() - returns its own

D
Dnebl, 2018-12-22
@Dnebl

return $this->client();
Returns what the client returns, i.e. this:
return redirect('/clients');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question