Home > Software engineering >  Undefined variable: post in home.blade.php
Undefined variable: post in home.blade.php

Time:06-22

This is my Admincontroller

    <?php
    
    namespace App\Http\Controllers;
    
    use App\Models\Post;
    use Illuminate\Http\Request;
    use Illuminate\Support\Facades\DB;
    
    class AdminController extends Controller
    {
        public function index(){
            $data=DB::table('post')->get();
            return view('admin.home',['post'=>$data]);
        }
        public function add(){
            return view('admin.add');
        }
        public function addpost(Request $request){
            request()->validate([
                'title'=>'required|max:50',
                'post'=>'required|max:500'
            ]);
    
            DB::table('post')->insert([
                'title'=>request('title'),
                'post'=>request('post')
            ]);
    
            return view('admin.home');
        }
    
        public function edit(Post $post){
            return view('admin.edit',['post'=>$post]);
        }
        public function editpost(Request $request, Post $post){
            request()->validate([
                'title'=>'required|max:50',
                'post'=>'required|max:500'
            ]);
            DB::table('post')->update([
                'title'=>request('title'),
                'post'=>request('post')
            ]);
            return redirect('home');
        }
        public function deletepost(Post $post){
            $post->delete();
            return redirect('home');
        }
    }

This is my routes

    <?php
    
    use App\Http\Controllers\AdminController;
    use App\Http\Controllers\LoginController;
    use App\Http\Controllers\LogoutController;
    use App\Http\Controllers\RegisterController;
    
    /*
    |--------------------------------------------------------------------------
    | 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('welcome');
    });
    Route::get('/home',[AdminController::class,'index']);
    Route::get('/add',[AdminController::class,'add'])->middleware('auth');
    Route::post('/addpost',[AdminController::class,'addpost']);
    Route::get('/edit/{post}',[AdminController::class,'edit']);
    Route::post('/editpost/{post}',[AdminController::class,'editpost']);
    Route::get('/delete/{post}',[AdminController::class,'deletepost']);
    Route::post('/logout',[LogoutController::class,'logout']);
    Route::get('/login',[LoginController::class,'login'])->name('login');
    Route::post('/logindetails',[LoginController::class,'logindetails']);
    Route::get('/register',[RegisterController::class,'register']);
    Route::post('/registerdetails',[RegisterController::class,'registerdetails']);

and this is home.blade.php

    <html>
        <head>
        </head>
        <body>
            @auth
                <a href="\home"><button>HOME</button></a>
                <a href="\add"><button>ADD</button><a>
                <form method="post" action="\logout" style="display:inline-block;">
                @csrf
                <button>LOGOUT</button>
                </form> <!--here-->
            @endauth
            @guest
                <a href="\login"><button>LOGIN</button></a>
                <a href="\register"><button>REGISTER</button></a>    
            @endguest
            
            
            @foreach($post as $data)
                <article> {{$data->title}}<br> {{$data->post}} 
                @auth <a href="\edit\{{$data->id}}">edit</a>| <a href="\delete\{{$data->id}}">delete</a>@endauth
                </article>
            @endforeach
        </body>
    </html>

The error says Undefined variable: post (View: C:\installationxampp\htdocs\samplelaravel1\resources\views\Admin\home.blade.php)

In home.blade.php the calling of the $post is invalid because its undefined while my edit pages are successfully retrieving the information from my database while when I call $post in home.blade.php it does not work. I'm in Laravel Framework 5.8.38

I've also done :


    public function index(){
        $data=DB::table('post')->get();
        return view('admin.home',compact('post'));
    }
       
    public function index(){
        $data=DB::table('post')->get();
        return view('admin.home')with(['posts' => $data]);
    }
       
    public function index(){
        $data['post']=DB::table('post')->get();
        return view('admin.home',$data);
    }

but did not work

CodePudding user response:

change your function index into :

public function index(){
    $post=DB::table('post')->get();
    return view('admin.home', compact('post'));
}

CodePudding user response:

You didn't pass $post to view or redirection in your controller

CodePudding user response:

public function index()
{
    $post = Post::all();
    return view('admin.home', compact('post'));
}
  • Related