【问题标题】:Laravel ,can't put php in the href linkLaravel,不能将php放在href链接中
【发布时间】:2018-02-17 21:56:15
【问题描述】:

我只想制作一个按钮,将我重定向到类似“/loans/loans->id/edit”的内容

其中的loans->id来自数据库,href应该怎么看?

<button href="{{ URL('/loans/{{$loan->id}}/edit')}}" 

这是我到目前为止所拥有的并且正在出错

【问题讨论】:

    标签: php html laravel url href


    【解决方案1】:

    您正在将刀片括号写入另一个括号内,这将引发 Parse 错误。因为它会翻译成这段代码。

    <button href="<?php echo e(URL('/loans/{{$loan->id); ?>/edit')}}" 
    

    此外,如果您想在字符串中编写表达式,请使用双引号 ""{} 单花括号。

    试试这个。

    <button href="{{ URL("/loans/{$loan->id}/edit") }}" 
    

    也在你的路由文件中添加这条路由。

    Route::get('/loans/{id}/edit', 'controller@method');
    

    【讨论】:

    • yes.First 标记将结束 href 的参数,因此 /loans/{$loan->id}/edit") }}" 不会进入 href
    • @Andrei 你对引号感到困惑。因为第一个引号与刀片括号内的引号无关,因为这些是 HTML 属性引号而不是 PHP 引号。你的刀片代码从{{开始
    • @Andrei Laravel 会将{{ }} 中的代码翻译成&lt;button href="&lt;?php echo e(URL("/loans/{$loan}/edit")); ?&gt;"&gt;
    • 它成功了。我使用可视代码,当我将你的代码粘贴到其中时,看起来不太好
    • @Andrei Oh haha​​ :-) Visual Studio Code 的荧光笔不太支持这种深度嵌套的引号。
    【解决方案2】:

    你的href应该是这样的

    href='loans/{{$load->id}}/edit'
    

    您的 \routs\web.php 中应该包含以下内容

    Route::get('/loans/{loan}/edit', 'LoansController@edit');
    

    【讨论】:

      【解决方案3】:

      第一个href你需要写在标签a!你可以使用辅助路由

      <a htef="{{route('loans.edit',[$loans->id])}}">link</a>
      

      并在路由器的文件中写入

       Route::get('loans/{$id}/edit', 'yourcontroller@method')->name('loans.edit')
      

      【讨论】:

      • 我应该使用什么方法?
      • 你在说这个吗? 'yourcontroller@method' .你在控制器中的方法。
      猜你喜欢
      • 1970-01-01
      • 2018-04-04
      • 1970-01-01
      • 2016-07-01
      • 2017-04-27
      • 2017-04-10
      • 1970-01-01
      • 2011-04-07
      • 2016-09-04
      相关资源
      最近更新 更多