Laravel 5:未定义的变量:post
Laravel 5 : Undefined variable: post
在我看来,我得到了一个错误,Undefined variable: post (View: /Applications/MAMP/htdocs/night-copy/resources/views/posts/index.blade.php).
我不知道为什么会这样。请帮帮我。
我正在为管理端创建一个 CRUD 仪表板。
我会贴一些我的代码,所以如果你需要更多代码,请问我。
PostsController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\Posts\CreatePostsRequest;
use App\Http\Requests\Posts\UpdatePostRequest;
use App\Post;
class PostsController extends Controller
{
public function index()
{
return view('posts.index')->with('posts', Post::all());
}
public function create()
{
return view('posts.create');
}
public function store(CreatePostsRequest $request)
{
//upload the image to strage
//dd($request->image->store('posts'));
$image = $request->image->store('posts');
//create the posts
Post::create([
'image' => $image,
'title' => $request->title,
'place' => $request->place,
'map' => $request->map,
'date' => $request->date,
'tag' => $request->tag,
'description' => $request->description
]);
//flash message
session()->flash('success', 'Post created successfully.');
//resirect user
return redirect(route('posts.index'));
}
public function show($id)
{
//
}
public function edit(Post $post)
{
return view('posts.create')->with('post', $post);
}
public function update(UpdatePostRequest $request, Post $post)
{
$data = $request->only(['title', 'place', 'map', 'date', 'published_at', 'tag', 'description']);
//check if new image
if($request->hasFile('image')){
//upload it
$image = $request->image->store('posts');
//delete old image
$post->deleteImage();
$data['image'] = $image;
}
//update attributes
$post->update($data);
//flash message
session()->flash('success', 'Post updated sucessfully.');
//redirect user
return redirect(route('posts.index'));
}
public function destroy($id)
{
$post = Post::withTrashed()->where('id', $id)->firstOrFail();
if($post->trashed()) {
$post->deleteImage();
$post->forceDelete();
} else {
$post->delete();
}
session()->flash('success', 'Post deleted successfully.');
return redirect(route('posts.index'));
}
public function trashed()
{
$trashed = Post::onlyTrashed()->get();
return view('posts.index')->with('posts', $trashed);
}
public function restore($id)
{
$post = Post::withTrashed()->where('id', $id)->firstOrFail();
$post->restore();
session()->flash('success', 'Post restore successfully.');
return redirect()->back();
}
}
index.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 href="https://use.fontawesome.com/releases/v5.6.1/css/all.css" rel="stylesheet">
<link rel="stylesheet" href="{{ asset('/css/main-posts.css') }}">
<title>Event Confirmation</title>
</head>
<body>
<div class="container">
<div class="header">
<h2 class="logo">Dark Code</h2>
<input type="checkbox" id="chk">
<label for="chk" class="show-menu-btn">
<i class="fas fa-bars" style="color: white;"></i>
</label>
<ul class="menu">
<div class="menu-list">
<a href="#"><i class="fa fa-home" ></i></a>
<a href="#">Dashboard</a>
<a href="{{ route('posts.index') }}">Post</a>
<a href="{{ route('categories.index') }}">Category</a>
<a href="{{ route('trashed-posts.index') }}">Suspended</a>
<a href="#">Logout</a>
<label for="chk" class="hide-menu-btn">
<i class="fas fa-times" style="color: white;"></i>
</label>
</div>
</ul>
</div>
<div class="content">
<div class="userinfo">
<div class="content">
@if($posts->count() > 0)
<table class="table">
<thead>
<th>ID</th>
<th>Title</th>
<th>Location</th>
<th>Map</th>
<th>Date</th>
<th>Tags</th>
</thead>
<tbody>
@foreach($posts as $post)
<tr></tr>
<td>{{ $post->id }}</td>
<td>{{ $post->title }}</td>
<td>{{ $post->place }}</td>
<td>{{ $post->map }}</td>
<td>{{ $post->date }}</td>
<td>{{ $post->tag }}</td>
@if($post->trashed())
<form action="{{ route('restore-posts', $post->id) }}" method="POST">
@csrf
@method('PUT')
<button class="save-btn" type="submit">Restore</button>
</form>
<br>
@else
<button btn="" class="edit-btn">
<a href="{{ route('posts.edit', $post->id) }}">Edit</a>
</button><br>
@endif
</td>
<td>
<form action="{{ route('posts.destroy', $post->id) }}" method="POST">
@csrf
@method('DELETE')
<button type="submit" class="delete-btn" >
{{ $post->trashed() ? 'Delete' : 'Trash' }}
</button><br>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
@else
<h3 style="margin: 15rem; color: white;">No Posts Yet</h3>
@endif
</div>
</div>
</div>
</div>
@if($post->trashed())
@else
<a href="{{ route('posts.create') }}"><button btn="" class="add-btn">Add</button></a>
@endif
<div class="footer">
</div>
</body>
</html>
web.php
Route::resource('posts', 'PostsController');
Route::get('trashed-posts', 'PostsController@trashed')->name('trashed-posts.index');
Route::PUT('restore-post/{post}', 'PostsController@restore')->name('restore-posts');
在您看来,您是在 @endforeach
块之外调用 @if($post->trashed())
,因此 $post
那时在范围之外,因此是未定义的变量。
除了 @if
语句之外还有多余的内容,您希望用户能够创建一个新的 post 而不管特定的 post 是否被丢弃
<div class="content">
<div class="userinfo">
<div class="content">
@if($posts->count() > 0)
<table class="table">
<thead>
<th>ID</th>
<th>Title</th>
<th>Location</th>
<th>Map</th>
<th>Date</th>
<th>Tags</th>
</thead>
<tbody>
@foreach($posts as $post)
<tr></tr>
<td>{{ $post->id }}</td>
<td>{{ $post->title }}</td>
<td>{{ $post->place }}</td>
<td>{{ $post->map }}</td>
<td>{{ $post->date }}</td>
<td>{{ $post->tag }}</td>
@if($post->trashed())
<form action="{{ route('restore-posts', $post->id) }}" method="POST">
@csrf
@method('PUT')
<button class="save-btn" type="submit">Restore</button>
</form>
<br>
@else
<button btn="" class="edit-btn">
<a href="{{ route('posts.edit', $post->id) }}">Edit</a>
</button><br>
@endif
</td>
<td>
<form action="{{ route('posts.destroy', $post->id) }}" method="POST">
@csrf
@method('DELETE')
<button type="submit" class="delete-btn">
{{ $post->trashed() ? 'Delete' : 'Trash' }}
</button><br>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
@else
<h3 style="margin: 15rem; color: white;">No Posts Yet</h3>
@endif
</div>
</div>
</div>
</div>
<a href="{{ route('posts.create') }}"><button btn="" class="add-btn">Add</button></a>
在您的视图文件中选中此行 @if($post->trashed())
。这是在 foreach 循环之外。将此行注释掉或删除
在我看来,我得到了一个错误,Undefined variable: post (View: /Applications/MAMP/htdocs/night-copy/resources/views/posts/index.blade.php).
我不知道为什么会这样。请帮帮我。
我正在为管理端创建一个 CRUD 仪表板。
我会贴一些我的代码,所以如果你需要更多代码,请问我。
PostsController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\Posts\CreatePostsRequest;
use App\Http\Requests\Posts\UpdatePostRequest;
use App\Post;
class PostsController extends Controller
{
public function index()
{
return view('posts.index')->with('posts', Post::all());
}
public function create()
{
return view('posts.create');
}
public function store(CreatePostsRequest $request)
{
//upload the image to strage
//dd($request->image->store('posts'));
$image = $request->image->store('posts');
//create the posts
Post::create([
'image' => $image,
'title' => $request->title,
'place' => $request->place,
'map' => $request->map,
'date' => $request->date,
'tag' => $request->tag,
'description' => $request->description
]);
//flash message
session()->flash('success', 'Post created successfully.');
//resirect user
return redirect(route('posts.index'));
}
public function show($id)
{
//
}
public function edit(Post $post)
{
return view('posts.create')->with('post', $post);
}
public function update(UpdatePostRequest $request, Post $post)
{
$data = $request->only(['title', 'place', 'map', 'date', 'published_at', 'tag', 'description']);
//check if new image
if($request->hasFile('image')){
//upload it
$image = $request->image->store('posts');
//delete old image
$post->deleteImage();
$data['image'] = $image;
}
//update attributes
$post->update($data);
//flash message
session()->flash('success', 'Post updated sucessfully.');
//redirect user
return redirect(route('posts.index'));
}
public function destroy($id)
{
$post = Post::withTrashed()->where('id', $id)->firstOrFail();
if($post->trashed()) {
$post->deleteImage();
$post->forceDelete();
} else {
$post->delete();
}
session()->flash('success', 'Post deleted successfully.');
return redirect(route('posts.index'));
}
public function trashed()
{
$trashed = Post::onlyTrashed()->get();
return view('posts.index')->with('posts', $trashed);
}
public function restore($id)
{
$post = Post::withTrashed()->where('id', $id)->firstOrFail();
$post->restore();
session()->flash('success', 'Post restore successfully.');
return redirect()->back();
}
}
index.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 href="https://use.fontawesome.com/releases/v5.6.1/css/all.css" rel="stylesheet">
<link rel="stylesheet" href="{{ asset('/css/main-posts.css') }}">
<title>Event Confirmation</title>
</head>
<body>
<div class="container">
<div class="header">
<h2 class="logo">Dark Code</h2>
<input type="checkbox" id="chk">
<label for="chk" class="show-menu-btn">
<i class="fas fa-bars" style="color: white;"></i>
</label>
<ul class="menu">
<div class="menu-list">
<a href="#"><i class="fa fa-home" ></i></a>
<a href="#">Dashboard</a>
<a href="{{ route('posts.index') }}">Post</a>
<a href="{{ route('categories.index') }}">Category</a>
<a href="{{ route('trashed-posts.index') }}">Suspended</a>
<a href="#">Logout</a>
<label for="chk" class="hide-menu-btn">
<i class="fas fa-times" style="color: white;"></i>
</label>
</div>
</ul>
</div>
<div class="content">
<div class="userinfo">
<div class="content">
@if($posts->count() > 0)
<table class="table">
<thead>
<th>ID</th>
<th>Title</th>
<th>Location</th>
<th>Map</th>
<th>Date</th>
<th>Tags</th>
</thead>
<tbody>
@foreach($posts as $post)
<tr></tr>
<td>{{ $post->id }}</td>
<td>{{ $post->title }}</td>
<td>{{ $post->place }}</td>
<td>{{ $post->map }}</td>
<td>{{ $post->date }}</td>
<td>{{ $post->tag }}</td>
@if($post->trashed())
<form action="{{ route('restore-posts', $post->id) }}" method="POST">
@csrf
@method('PUT')
<button class="save-btn" type="submit">Restore</button>
</form>
<br>
@else
<button btn="" class="edit-btn">
<a href="{{ route('posts.edit', $post->id) }}">Edit</a>
</button><br>
@endif
</td>
<td>
<form action="{{ route('posts.destroy', $post->id) }}" method="POST">
@csrf
@method('DELETE')
<button type="submit" class="delete-btn" >
{{ $post->trashed() ? 'Delete' : 'Trash' }}
</button><br>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
@else
<h3 style="margin: 15rem; color: white;">No Posts Yet</h3>
@endif
</div>
</div>
</div>
</div>
@if($post->trashed())
@else
<a href="{{ route('posts.create') }}"><button btn="" class="add-btn">Add</button></a>
@endif
<div class="footer">
</div>
</body>
</html>
web.php
Route::resource('posts', 'PostsController');
Route::get('trashed-posts', 'PostsController@trashed')->name('trashed-posts.index');
Route::PUT('restore-post/{post}', 'PostsController@restore')->name('restore-posts');
在您看来,您是在 @endforeach
块之外调用 @if($post->trashed())
,因此 $post
那时在范围之外,因此是未定义的变量。
除了 @if
语句之外还有多余的内容,您希望用户能够创建一个新的 post 而不管特定的 post 是否被丢弃
<div class="content">
<div class="userinfo">
<div class="content">
@if($posts->count() > 0)
<table class="table">
<thead>
<th>ID</th>
<th>Title</th>
<th>Location</th>
<th>Map</th>
<th>Date</th>
<th>Tags</th>
</thead>
<tbody>
@foreach($posts as $post)
<tr></tr>
<td>{{ $post->id }}</td>
<td>{{ $post->title }}</td>
<td>{{ $post->place }}</td>
<td>{{ $post->map }}</td>
<td>{{ $post->date }}</td>
<td>{{ $post->tag }}</td>
@if($post->trashed())
<form action="{{ route('restore-posts', $post->id) }}" method="POST">
@csrf
@method('PUT')
<button class="save-btn" type="submit">Restore</button>
</form>
<br>
@else
<button btn="" class="edit-btn">
<a href="{{ route('posts.edit', $post->id) }}">Edit</a>
</button><br>
@endif
</td>
<td>
<form action="{{ route('posts.destroy', $post->id) }}" method="POST">
@csrf
@method('DELETE')
<button type="submit" class="delete-btn">
{{ $post->trashed() ? 'Delete' : 'Trash' }}
</button><br>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
@else
<h3 style="margin: 15rem; color: white;">No Posts Yet</h3>
@endif
</div>
</div>
</div>
</div>
<a href="{{ route('posts.create') }}"><button btn="" class="add-btn">Add</button></a>
在您的视图文件中选中此行 @if($post->trashed())
。这是在 foreach 循环之外。将此行注释掉或删除