简体   繁体   English

如何使用 id 获取 laravel 中控制器的 href

[英]How can i get the href for controllers in laravel using id

I'm trying to create a hyperlink that redirects to user resource.我正在尝试创建一个重定向到用户资源的超链接。

This is my route这是我的路线

Route::get('/user/{id}', 'UserController@show');

This is my userController.php file这是我的 userController.php 文件

public function show($id) {
    $users = User::find($id);
    return view('user.show', compact('users'));
}

I'm doing this :我正在这样做

不工作

My blade file looks like this我的刀片文件看起来像这样

@extends('layouts.app')

@section('content')

    <h1>All Crads</h1>

    @foreach($users as $user)
        <p>
            <b><a href="{{ url('/user/{id}') }}">{{ $user->name }}:</a></b>
            <span>{{ $user->email }}</span>
        </p>

    @endforeach
@stop

Use named routes.使用命名路由。 Here's the documentation .这是文档

By following those instructions your template will contain route .按照这些说明,您的模板将包含route Something like this:像这样的东西:

<a href="{{ route('foo', ['id' => 1]) }}">

Here is named routing documentation which explained how to use:这是命名路由文档,它解释了如何使用:

@extends('layouts.app')

@section('content')

    <h1>All Crads</h1>

    @foreach($users as $user)
        <p>
            <b><a href="{{ route('route_name', array('id' => $user->id)) }}">{{ $user->name }}:</a></b>
            <span>{{ $user->email }}</span>
        </p>

    @endforeach
@stop

You can do this:你可以这样做:

<a href="{{ url("/user/{$user->id}") }}">{{ $user->name }}</a>

please follow: https://laravel.com/docs/5.8/urls#generating-basic-urls请关注: https://laravel.com/docs/5.8/urls#generating-basic-urls

You can also try:你也可以试试:

<a href="{{ url("/user/".$user->id) }}">{{ $user->name }}</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM