1

Basically I'm building a pet adoption site and now getting an error which says "Target class [admin] does not exist" when my admin user account is trying to approve 'adoption request' of any user who have requested to adopt a pet. What should i do in this case?

kernel.php

<?php

namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class kernel extends HttpKernel
{
    /**
     * The application's global HTTP middleware stack.
     *
     * @var array
     */
    protected $middleware = [
        // Existing global middleware
    ];

    /**
     * The application's route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [
        'web' => [
            // Existing web middleware group
        ],

        'api' => [
            // Existing api middleware group
        ],
    ];

    /**
     * The application's route middleware.
     *
     * @var array
     */
    protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        'admin' => \App\Http\Middleware\IsAdmin::class,
        // Other middleware
    ];
}

AdoptionRequestController

<?php

namespace App\Http\Controllers;

use App\Models\AdoptionRequest;
use App\Models\Pet;
use App\Notifications\RequestApprovedNotification;
use Illuminate\Http\Request;

class AdoptionRequestController extends Controller
{
    // Method to display the adoption request form
    public function create(Pet $pet)
    {
        return view('pets.request_adoption', compact('pet'));
    }

    // Method to store the adoption request form data
    public function store(Request $request, Pet $pet)
    {
        $request->validate([
            'name' => 'required|max:255',
            'address' => 'required|max:255',
            'phone' => 'required|max:255',
            'meeting_date' => 'required|date',
            'reason' => 'required'
        ]);

        $adoptionRequest = new AdoptionRequest([
            'pet_id' => $pet->id,
            'user_id' => $request->user()->id, // Assuming you have user authentication
            'name' => $request->name,
            'address' => $request->address,
            'phone' => $request->phone,
            'meeting_date' => $request->meeting_date,
            'reason' => $request->reason,
            'is_approved' => false
        ]);
        $adoptionRequest->save();

        return redirect()->route('pets.show', $pet->id)->with('success', 'Adoption request submitted successfully.');
    }

    // List all adoption requests (for admin view)
    public function index()
    {
        $requests = AdoptionRequest::with('pet')->get(); // Ensure you load the pet relationship
        return view('admin.adoption_requests', compact('requests'));
    }

    // Approve an adoption request
    public function approve(Request $request, $id)
    {
        $adoptionRequest = AdoptionRequest::with('user')->findOrFail($id);
        $adoptionRequest->is_approved = true;
        $adoptionRequest->save();

        // Send notification
        if ($adoptionRequest->user) {
            $adoptionRequest->user->notify(new RequestApprovedNotification($adoptionRequest));
        }

        return redirect()->back()->with('success', 'Adoption request approved.');
    }
}

web.php

// Admin Routes for Adoption Requests
Route::middleware(['auth', 'admin'])->group(function () {
    Route::get('admin/requests', [AdoptionRequestController::class, 'index'])->name('admin.requests.index');
    Route::patch('admin/requests/{request}', [AdoptionRequestController::class, 'approve'])->name('adoption_requests.approve');
});

IsAdmin.php

<?php

// app/Http/Middleware/IsAdmin.php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class IsAdmin
{
    public function handle(Request $request, Closure $next)
    {  
        
        if (!auth()->check() || !auth()->user()->is_admin) {
            abort(403, 'Unauthorized action.');
        }

        return $next($request);
    }
}
12
  • Which laravel version your are using? Commented Sep 19, 2024 at 12:07
  • Where is this error triggered (class, line)? Commented Sep 19, 2024 at 12:07
  • Did you run composer dump-autoload? Commented Sep 19, 2024 at 12:27
  • @aynber Yes, I did run composer dump-autoload but still getting the error Commented Sep 19, 2024 at 13:01
  • @Subha Laravel Framework 11.14.0 Commented Sep 19, 2024 at 13:04

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.