简体   繁体   中英

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

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 . 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

You can also try:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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