如何在 Laravel 6 中使用用户 ID 在 <a> 标签中设置路由?

How to set the route in <a> tag with user id in Laravel 6?

这是我的索引 blade,我想在其中单击用户名,它会将我重定向到编辑用户 blade

@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->id)}} 它会将我重定向为 admin/users/edit/1 但我的路线设置为 admin/users/1/edit。我怎样才能打开这条路线?

我不建议使用admin/users/1/edit即使你想使用这个

改变

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

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

参考:

Laravel ->URL Generation -> Generating Basic Url

您可以使用 route() 助手:

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

这样你的视图不需要知道如何构建url,它只需要知道路由名称。您以后对路线所做的任何更改 url 仍然有效。