I made a crud and I would like to display it on the home page but I have an error. I explain : in views i have folder posts in this folder posts i have files create edit index master and show .blade.php in all files which are in folder posts can see the posts but in home.blade.php don't see i have error Undefined variable $posts
home.blade.php
@extends('layouts.app')
@section('content')
<div >
<div >
<div >
<div >
<div >{{ __('Dashboard') }}</div>
<div >
@if (session('status'))
<div role="alert">
{{ session('status') }}
</div>
@endif
{{ __('You are logged in!') }}
</div>
@foreach ($posts as $post)
// now you can access your $post variable here...
<h1> Title: {{ $post->title }} </h1>
@endforeach
</div>
</div>
</div>
</div>
@endsection
PostController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
class PostController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$posts = Post::all();
return view('posts.index', compact('posts'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('posts.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$post = Post::create([
'title' => $request->title,
'description' => $request->description
]);
if ($post) {
return redirect()->route('posts.index');
}
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show(Post $post)
{
return view('posts.show', compact('post'));
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit(Post $post)
{
return view('posts.edit', compact('post'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Post $post)
{
$post['title'] = $request->title;
$post['description'] = $request->description;
$post->save();
return redirect()->route('posts.index');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy(Post $post)
{
$post->delete();
return redirect()->route('posts.index');
}
}
web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('auth/login');
});
Auth::routes(['verify' => true]);
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::resource('posts', PostController::class);
The crud works very well and we see it very well on another page that I called show, but I would like us to see the posts on the home page too. But I have an error: Undefined variable $post How can I solve this, the posts must be displayed on the home page too
CodePudding user response:
The reason you cannot see $post
in your home.blade.php
is because you are passing in $posts
and you are referencing $post
in your blade file. There's nothing wrong with this, but you need to ensure to loop over $posts
so that you can assign each post to a $post
variable.
Something like:
@foreach ($posts as $post)
// now you can access your $post variable here...
<h1> Title: {{ $post->title }} </h1>
@endforeach
CodePudding user response:
I have resolve this question need to add
public function index()
{
$posts = Post::all();
return view('home', compact('posts'));
}
In Homecontroller