【问题标题】:How pass $variables for all functions with constructor in controller (Laravel)如何在控制器中使用构造函数为所有函数传递 $variables (Laravel)
【发布时间】:2021-03-26 03:24:07
【问题描述】:

我想在另一个函数中传递一个变量,我使用 __construct 函数,但我的浏览器返回“无法解析的依赖解析 [Parameter #0 [ $startingAt ]]”。感谢您的帮助!

我的控制器:

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;
}

我的刀片.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');

【问题讨论】:

标签: php laravel controller php-carbon


【解决方案1】:

只需调用您创建的函数来填充构造函数中的变量并从构造函数方法中删除参数$startingAt

public function __construct()
{
    $this->startingAt();
}

【讨论】:

  • 它的作品,但我想收集 $variable 结果而不是它不可能的功能?
  • @ValentinLeguy 从哪里收集?
  • 我想从另一个函数获取 $startingAt 结果(我想收集 $startingAt 和 $endingAt 结果并将它们显示在另一个函数上)并且在同一个类中
  • 哎呀抱歉,我只是看到如何从另一个函数 lmao 收集参数,谢谢你的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-26
  • 2016-08-22
  • 1970-01-01
  • 2018-06-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多