创建帖子后,在Laravel 5.8中出现“未定义的变量:帖子”

时间:2019-05-04 15:25:45

标签: php laravel

当我创建新帖子并点击提交时,帖子数据会验证并持久保存到数据库中,但在返回视图中会出现Undefined variable: posts (View:/Users/charles/Desktop/Development/fiestas/resources/views/backend/posts/index.blade.php)错误。

因此,它说它是未定义的,但是当我清除页面并访问我的索引页面时,帖子按预期显示,没有错误。我从Rails移了过来,现在在Laravel还是个绿色。任何帮助将不胜感激。

App / Models / Post.php

<?php

namespace App\Models;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    protected $guarded = [];

}

app / Http / Controllers / Backend / PostController.php

<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use App\Models\Post;

class PostController extends Controller
{

    public function index(Post $post)
    {
        $posts = Post::all();
        return view('backend.posts.index', compact('posts'));
    }

    public function create()
    {
        return view('backend.posts.create');
    }

    ...

    public function store(Request $request)
    {
        $rules = [
            'title' => ['required', 'min:3'],
            'body' => ['required', 'min:5']
        ];
        $request->validate($rules);
        $user_id = Auth::id();
        $post = new Post();
        $post->user_id = $user_id;
        $post->title = request('title');
        $post->body = request('body');
        $post->save();

        return view('backend.posts.index');
    }
}

routes / backend / posts.php

<?php

use App\Http\Controllers\Backend\PostController;

// All route names are prefixed with 'admin.'.
Route::redirect('/', '/admin/posts', 301);
Route::get('posts', [PostController::class, 'index'])->name('index');
Route::get('posts/create', [PostController::class, 'create'])->name('create');
Route::post('posts', [PostController::class, 'store'])->name('store');
Route::get('posts/{post}', [PostController::class, 'show'])->name('show');
Route::get('posts/{post}/edit', [PostController::class, 'edit'])->name('edit');
Route::patch('posts/{post}', [PostController::class, 'update'])->name('update');
Route::delete('posts/{post}', [PostController::class, 'destroy'])->name('destroy');

资源/视图/后端/帖子/create.blade.php

...
<form method="Post" action="/admin/posts">
        @csrf
        <input type="text" name="title" placeholder="Post Title" value="{{ old('title') }}">
        <textarea name="body" placeholder="Body" id="" cols="30" rows="10">{{ old('body') }}</textarea>
        <button type="submit">Submit</button>
    </form>
...

资源/视图/后端/帖子/index.blade.php

...
@section('content')
    @foreach ($posts as $post)
        <h5><a href="/posts/{{ $post->id }}">{{ $post->title }}</a></h5>
        <p>{{ Str::limit($post->body, 100) }}</p>
    @endforeach
@endsection
...

2 个答案:

答案 0 :(得分:2)

您的索引方法是正确的-它捕获$posts变量并将其压缩到返回视图中。

但是,当您存储新帖子时,您只是在存储它,然后通过store()方法没有集合的的return语句调用同一页面视图:< / p>

return view('backend.posts.index');

您还没有通过$posts方法向视图发送store()集合变量,并且您的视图期望该变量在那里,从而导致错误。要解决此问题,请在您的store()方法中,抓住这些帖子并压缩:

$posts = Post::all();
return view('backend.posts.index', compact('posts'));

答案 1 :(得分:1)

一种方法是在存储帖子后重定向。 因为视图需要$posts。如果我们仅返回视图,它将显示错误。因此,我们可以重定向到索引函数以检索$posts并将其加载到视图中。

 public function store(Request $request)
{
    $rules = [
        'title' => ['required', 'min:3'],
        'body' => ['required', 'min:5']
    ];
    $request->validate($rules);
    $user_id = Auth::id();
    $post = new Post();
    $post->user_id = $user_id;
    $post->title = request('title');
    $post->body = request('body');
    $post->save();

    return redirect()->route('index');
}