C
C
Camaro672018-07-03 19:41:34
Laravel
Camaro67, 2018-07-03 19:41:34

Is it possible to do dependency injection in a controller with a condition?

Good day, toaster! Is it possible somehow in the controller to perform dependency injection with a condition? Those. it's about this:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;

class UserController extends Controller
{
    public function update(Request $request, User $user)
    {
        //
    }
}

I'm interested in the option with dependency injection, and not this one:
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;

class UserController extends Controller
{
    public function update(Request $request, $user)
    {
        if(!$user = User::where('age', '>', 20)->find($user))
        {
            abort(404);
        }
    }
}

Thank you in advance!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
JhaoDa, 2018-07-03
@Camaro67

It is not very clear what the conditions depend on, but there are at least two options: one , two .

F
fomenko_alexandr, 2018-07-03
@fomenko_alexandr

If you use binding to routing, then this is a good option .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question