【问题标题】:"Undefined variable: posts" in Laravel 5.8 after post create创建后 Laravel 5.8 中的“未定义变量:帖子”
【发布时间】:2019-09-22 20:07:54
【问题描述】:

当我创建一个新帖子并点击提交时,帖子数据会验证,并保存到数据库中,但在返回视图中我收到 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');

resources/views/backend/posts/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>
...

资源/视图/后端/posts/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
...

【问题讨论】:

  • 我想你会想要做你在索引方法中做的同样的事情:获取所有帖子并发送到视图?或者只是重定向到索引?

标签: php laravel


【解决方案1】:

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

但是,当您存储新帖子时,您只是存储它,然后通过store() 方法的返回语句调用相同的页面视图没有集合:

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

您尚未从您的store() 方法向视图发送$posts 集合变量,并且您的视图希望该变量存在,因此出现错误。要修复,请在您的 store() 方法中,抓取帖子并压缩:

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

【讨论】:

    【解决方案2】:

    一种方法是在存储帖子后重定向。 因为视图需要$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');
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-22
      • 2020-01-21
      相关资源
      最近更新 更多