我的PostCOntroller
class PostController extends Controller
{
public function index()
{
$posts = post::all();
return view('posts.index',['p'=>$posts]);
}
public function show(post $post)
{
return view('posts.show', ['x'=>$post]);
}
master.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href = "/css/bootstrap.css">
<title>Document</title>
</head>
<body>
<div class="container">
<center><h1>POST<h1></center>
@yield('body')
</div>
index.blade.php
@extends('posts.master')
@section('body')
@foreach($p as $post)
<div class="card mt-2">
<a href = "{{ route('hello.show'),$post->id}}">{{$post->title}}</a>
</div>
@endforeach
@endsection
show.blade.php
@extends('posts.master')
@section('body')
<div class="card">
<div class="card-body">
<h3>{{$x->title}}</h3>
</div>
<div class="card-body">
<h3>{{$x->content}}</h3>
</div>
</div>
@endsection
web.php
Route::resource('hello','PostController');
php artisan route:list
GET|HEAD | hello | hello.index | App\Http\Controllers\PostController@index | web |
POST | hello | hello.store App\Http\Controllers\PostController@store | web |
GET|HEAD | hello/create | hello.create | App\Http\Controllers\PostController@create | web |
GET|HEAD | hello/{hello} | hello.show | App\Http\Controllers\PostController@show | web
在许多情况下,我已经将以上内容视为解决方案。 如果我转到链接-127.0.0.1:8000/hello/2,那么它将向我显示数据..这意味着hello.show页面还可以...索引页面显示错误吗?我在做什么错了?
我尝试过
<a href = "{{"route('hello.show'), [$post->id]"}}
<a href = "{{"route('hello.show'), ['id'=>$post->id]"}}
<a href = "{{"route('hello.show'), auth()->post->id]"}}
没有任何作用
错误
public static function forMissingParameters($route)
{
return new static("Missing required parameters for [Route: {$route-
>getName()}] [URI: {$route->uri()}].");
}
Missing required parameters for [Route: hello.show] [URI: hello/{hello}].
(View: F:\xampp\htdocs\sample for Laravel -
Copy\resources\views\posts\index.blade.php
答案 0 :(得分:1)
将参数放入路线函数
<a href = "{{ route('hello.show',$post->id)}}">{{$post->title}}</a>