我想在另一个函数中传递一个变量,我使用 __construct 函数,但我的浏览器返回“无法解析的依赖关系解析 [参数 #0 [ $startingAt ]]”。感谢您的帮助!
我的 Controller :
protected $startingAt;
public function __construct($startingAt)
{
$this->startingAt = $startingAt;
}
public function index()
{
return view('index');
}
public function startingAt()
{
$this->startingAt = Carbon::now();
return $this->startingAt;
}
我的 Blade .pĥp:
<form action="{{ route('starting.race') }}" method="POST">
{{ csrf_field() }}
@method('post')
<button class="bg-indigo-600 text-white font-bold px-3 rounded-full" type="submit" name="submit">Start</button>
</form>
我的 web.php :
Route::get('/', [\App\Http\Controllers\CarbonController::class, 'index'])->name('index.race');
Route::post('/start', [\App\Http\Controllers\CarbonController::class, 'startingAt'])->name('starting.race');
最佳答案
只需调用您为填充构造函数中的变量而创建的函数,并从构造函数方法中删除参数 $startingAt
即可。
public function __construct()
{
$this->startingAt();
}
关于php - 如何通过 Controller 中的构造函数为所有函数传递 $variables (Laravel),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65306792/