【问题标题】:How to set the route in <a> tag with user id in Laravel 6?如何在 Laravel 6 中使用用户 ID 在 <a> 标签中设置路由?
【发布时间】:2020-07-08 03:57:47
【问题描述】:

这是我的索引刀片,我想在其中单击用户名,它会在编辑用户刀片上重定向我

@extends('layouts.admin')

        @section('content')
          <h1><b>Users</b></h1>
          <table class="table table-hover">
              <thead>
              <tr>
                  <th scope="col">#</th>
                  <th scope="col">Profile</th>
                  <th scope="col">Name</th>
              </tr>
              </thead>
              <tbody>
              @if($users)
                  @foreach($users as $user)
              <tr>
                  <td>{{$user->id}}</td>
                  <td><img height="25" src="{{$user->photo ? $user->photo->file:'No photo exist'}}"></td>


                <!-- Problem is here -->
                  <td><a href="{{route('admin.users.edit', $user->id)}}" style="text-decoration: none"> 
                {{$user->name}}</a></td>


*it through an exception* 

**Route [admin.users.edit] not defined**       
              </tr>
                  @endforeach
                  @endif
              </tbody>
          </table>
        @endsection

如果我像这样使用url() 方法{{url('admin/users/edit',$user-&gt;id)}},它会将我重定向为admin/users/edit/1,但我的路线设置为admin/users/1/edit。如何打开这条路线?

【问题讨论】:

    标签: laravel laravel-blade laravel-6


    【解决方案1】:

    我不建议使用admin/users/1/edit,即使你想使用它

    改变

    {{url('admin/users/edit',$user->id)}}
    

    到

    {{url('admin/users/'.$user->id.'/edit')}}
    

    参考:

    Laravel ->URL Generation -> Generating Basic Url

    【讨论】:

      【解决方案2】:

      您可以使用route() 助手:

      route('your.route.name1, [$user->id])
      

      这种方式你查看不需要知道如何构建url,只需要知道路由名称。您的路由 url 中的任何未来更改仍然有效。

      【讨论】:

      • 我已经在使用该路由,但它通过异常“Route [admin.users.edit] not defined”
      • 可能您的路线名称有误。使用php artisan route: list查看路由名称。
      • 没有先生路线名称是正确的,我用这个命令“php artisan route:list”仔细检查了路线
      • 我确定路由名称是错误的(其实laravel肯定有)。仔细检查一下。手动编写路由 url 是一种不好的坏习惯。
      猜你喜欢
      • 2018-07-01
      • 1970-01-01
      • 2016-12-12
      • 1970-01-01
      • 2018-03-25
      • 2017-11-06
      • 1970-01-01
      • 1970-01-01
      • 2021-10-07
      相关资源
      最近更新 更多