Home > OS >  Laravel cannot find a Controller Method
Laravel cannot find a Controller Method

Time:01-27

Method App\Http\Controllers\CommunityController::show does not exist

My route

Route::get('/community/{id}', [CommunityController::class, 'community'])
    ->name('profile.community');

Controller

public function community($id)
{
    $communityProfile = Community::find($id);
    /* $join = CommunityUser::join('communities', 'communities.user_id', '=', 'community_users.user_id')->get(); */

    return view('user.comprofile', compact('communityProfile', 'join'));

}

View community

@for ($i = 0; $i < 3; $i  )
    <a href=" {{ route('profile.community', $communities[$i]->id) }} ">
        <div >
            <h5 >{{$communities[$i]->community}}</h5>
            <p >
                <x-iconsax-out-location /> {{$communities[$i]->address}}, {{$communities[$i]->city}}
            </p>

            <div >
                <a href="#" >
                    {{ $communities[$i]->type }}
                </a>
                
                @if ($communities[$i]->user_id === Auth::user()->id)
                    <span >{{__('Created by: ')}}</span>
                    {{Auth::user()->name}}
                @else
                    <span >{{__('Created by: ')}}</span>
                    {{ $communities[$i]->name }}
                    
                @endif

            </div>
        </div>
    </a>
@endfor

I am trying to view the community created by users but it gives me "cannot find a Controller Method ".

CodePudding user response:

This can have several causes. Therefore, here is a small checklist:

  1. The controller path is missing from your route:
use App\Http\Controllers\CommunityController; // this line
Route::get('/community/{id}', [CommunityController::class, 'community'])->name('profile.community');
  1. Your CommunityController class name is misspelled
  2. The namespace of the CommunityController is wrong

And then just to be sure, run: php artisan route:clear.

  • Related