【问题标题】:Laravel question, How to use _GET on BladeLaravel 问题,如何在 Blade 上使用 _GET
【发布时间】:2021-02-24 06:08:34
【问题描述】:

我有一个暂时无法解决的问题。

我有这样的网址:https://www.example.com/schedule/2020-02-26

我用下一个函数把日期放在 URL 上:

   public function newdate($datevar)
    {       
        $Dates = Dates::all();
        return view('schedule', compact('Dates'), ['Dates' => 
     $Dates]);
    } //

在此示例中,$datevar 等于“2020-02-26”

我的问题是,如何在刀片中显示 $datevar?我尝试使用 {{$datevar}},但出现错误。

要将 datevar 放在我使用的函数和其他函数上作为请求:

public function addnewdate(Request $request){
   $datevar = $request->input('newdate');

    return redirect()->to('schedule/'.$datevar);
}

感谢您的帮助

【问题讨论】:

  • 调用视图函数时必须将变量传递给刀片文件

标签: php get laravel-5.7


【解决方案1】:

解决办法是在web.php文件中注册URL格式。在该文件中,添加以下行:

Route::get('/schedule/{date}', [ControllerName::class, 'addnewdate']);

现在,每当调用控制器方法时,它都会将日期作为参数传递给您的 addnewdate 方法,您需要像这样对其进行重组:

public function addnewdate($date){
    //Do sth with the date
}

【讨论】:

  • 是的,我这样做并且工作正常,但是如何在我的 Blade.php 中显示日期?
  • 您已经使用 view('schedule', compact('Dates'), ['Dates' => $Dates]); 进行了操作,只需从您的刀片文件中访问 $Dates 变量即可。
  • 不,我不需要 Blade.php 上的 $Dates 我需要查看 Blade.php 上的 $datevar
  • 好的,所以添加进去。return view('schedule')->with('Dates', $Dates)->with('dateVar', $datevar)
  • 感谢您的帮助,现在正在工作:D
猜你喜欢
  • 2016-05-29
  • 2017-12-31
  • 2021-04-07
  • 2020-08-18
  • 2019-05-06
  • 2015-04-18
  • 2019-01-19
  • 2018-02-21
  • 2021-07-22
相关资源
最近更新 更多