Reputation: 3289
When I create a new Post and hit submit, the post data validates, persists to the database but in the return view I get an Undefined variable: posts (View:/Users/charles/Desktop/Development/fiestas/resources/views/backend/posts/index.blade.php)
error.
So, it says its undefined, but when I clear the page and visit my index page, the posts show up as they should with no error. I moved over from Rails and am still green at Laravel. Any help is greatly appreciated.
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>
...
resources/views/backend/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
...
Upvotes: 0
Views: 1832
Reputation: 1814
One method is to redirect after storing the post.
Because the view needs $posts
.If we simply return to view,it will display error.So we can redirect to index function to retrieve $posts
and load it in view.
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');
}
Upvotes: 1
Reputation: 8178
Your index method is correct - it grabs the $posts
variable and compacts it into the return view.
When you are storing a new post, however, you are only storing it, but then calling that same page view through the return statement of the store()
method without the collection:
return view('backend.posts.index');
You haven't sent the view a $posts
collection variable from your store()
method, and your view expects that variable to be there, thus the error. To fix, within your store()
method, grab the posts and compact:
$posts = Post::all();
return view('backend.posts.index', compact('posts'));
Upvotes: 2