我的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', ['post'=>$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="big-info p-5">
<a href = "{{route('hello.index')}}">Home</a>
<a href = "{{route('hello.create')}}">Create</a>
</div>
<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>{{$post->title}}</h3>
</div>
<div class="card-body">
<h3>{{$post->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
我想我写的都没错..索引页正在从数据库发送数据的ID ...控制器正在接收数组$ post中的数据,并且显示刀片应该显示数据$ post-> title或$ post-> content,但这没有发生 我认为这是一个愚蠢的错误,但无法弄清楚 问题是我的显示页面未显示数据 请帮助
#post {#199 ▼
#fillable: array:2 [▼
0 => "title"
1 => "content"
]
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:5 [▶]
#original: array:5 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▼
0 => "*"
]
}
在展示刀片中添加{{dd($ post)}}
post {#195 ▼
#fillable: array:2 [▶]
#connection: null
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: false
+wasRecentlyCreated: false
#attributes: []
#original: []
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
答案 0 :(得分:1)
如果$ hello是帖子的ID,则您的显示控制器应为:
public function show($hello)
{
$post = Post::find($hello);
return view('posts.show', ['post'=>$post]);
}