Answer the question
In order to leave comments, you need to log in
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)
{
//
}
}
<?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);
}
}
}
Answer the question
In order to leave comments, you need to log in
It is not very clear what the conditions depend on, but there are at least two options: one , two .
If you use binding to routing, then this is a good option .
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question