0

here is my controller I have method request that I use to retrieve the date from database and then compact the variable with the view.

<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\generalForm;
use DB;
use App\Models\Admin;
use Illuminate\Support\Facades\Auth;

class AdminController extends Controller
{
    function check(Request $request){
         //Validate Inputs
         $request->validate([
            'email'=>'required|email|exists:admins,email',
            'password'=>'required|min:5|max:30'
         ],[
             'email.exists'=>'This email is not exists in admins table'
         ]);

         $creds = $request->only('email','password');

         if( Auth::guard('admin')->attempt($creds) ){
             return redirect()->route('admin.home' );
             
         }else{
             return redirect()->route('admin.login')->with('fail','Incorrect credentials');
         }
    }
    
    ///here is the function that i mean////

    public function requests(){
        $request = generalForm::all();
        return view('admin.home',compact('request'));

    }

    function logout(){
        Auth::guard('admin')->logout();
        return redirect('/');
    }
}

in the blade I use foreach and the problem occur here I think because it telling me Undefined variable $request. I go through the code several times to find out the problem but nothing finds.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="css/app.css" />
    <title>Request</title>
    @vite('resources/css/app.css')
    @vite('resources/js/script.js')
  </head>
  <body>
   
    <!--table-->
    <section>
    <div>
      <h2 class="text-4xl font-bold text-center pb-16"> Apply Request</h2>
    </div>
    <div
        class="container flex flex-col-reverse items-center px-6 mx-auto mt-10 space-y-0 md:space-y-0 md:flex-row"
      >

 
    
  <div class="table w-full p-2">
        <table class="w-full border">
            <thead>
                <tr class="bg-red-700 border-b">
                    
                    <th class="p-2 border-r cursor-pointer text-sm font-bold text-white">
                        <div class="flex items-center justify-center">
                            Name
                            
                        </div>
                    </th>
                    
                    <th class="p-2 border-r cursor-pointer text-sm font-bold text-white">
                        <div class="flex items-center justify-center">
                            Email
                           
                        </div>
                    </th>
                   
                    <th class="p-2 border-r cursor-pointer text-sm font-bold text-white">
                        <div class="flex items-center justify-center">
                            Action
                           
                        </div>
                    </th>
                </tr>
            </thead>
            <tbody>
                
            @foreach($request as $item)   
                <tr class="bg-gray-100 text-center border-b text-sm text-gray-600">
                
                    <td class="p-2 border-r">{{ $item->first_name }}</td>
                    <td class="p-2 border-r">{{ $item->email }}</td>
                    <td>
                        <a href="{{ url('/viewR')}}" class="bg-blue-500 p-2 text-white hover:shadow-lg text-xs font-thin">View</a>
                        <a href="#" class="bg-red-500 p-2 text-white hover:shadow-lg text-xs font-thin">Remove</a>
                    </td>
                </tr>
                
               
                @endforeach   

                
            </tbody>
        </table>
    </div>
</div>
</section>

</body>
</html>

in the web.php these are the routes.

Route::prefix('admin')->name('admin.')->group(function(){
       
    Route::middleware(['guest:admin','PreventBackHistory'])->group(function(){
          Route::view('/login','dashboard.admin.login')->name('login');
          Route::post('/check',[AdminController::class,'check'])->name('check');
    });

    Route::middleware(['auth:admin','PreventBackHistory'])->group(function(){
        Route::view('/home','dashboard.admin.home')->name('home');
        Route::post('/logout',[AdminController::class,'logout'])->name('logout');
    });

});

1 Answers1

0

Maybe u can use this instead,

return view('admin.home',['request' => $request]);