【问题标题】:Laravel cannot find a Controller MethodLaravel 找不到控制器方法
【发布时间】:2023-01-27 03:18:58
【问题描述】:

方法 App\Http\Controllers\CommunityController::show 不存在

我的路线

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

控制器

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'));

    }

查看社区

@for ($i = 0; $i < 3; $i++) <a href=" {{ route('profile.community', $communities[$i]->id) }} "> <div class="w-full p-6 overflow-hidden bg-white border hover:bg-gray-50 border-gray-200 rounded-lg shadow-md dark:bg-gray-800 dark:border-gray-700">

                                <h5 class="mb-2 text-2xl font-semibold tracking-tight text-gray-900 dark:text-white">{{$communities[$i]->community}} </h5>
                                <p class="mb-3 font-normal text-gray-500 dark:text-gray-400 flex gap-3">
                                    <x-iconsax-out-location class="text-[#F15E4A] w-5"/> {{$communities[$i]->address}}, {{$communities[$i]->city}}
                                </p>

                                <div class="flex gap-2">
                                    <a href="#" class="inline-flex items-center font-semibold text-[#F15E4A] hover:underline cursor-default">
                                        {{ $communities[$i]->type }}
                                    </a>
                                    
                                    @if ($communities[$i]->user_id === Auth::user()->id)
                                        <span class="text-sm text-gray-500 pt-1">{{__('Created by: ')}}</span>
                                        {{Auth::user()->name}}
                                    @else
                                        <span class="text-sm text-gray-500 pt-1">{{__('Created by: ')}}</span>
                                        {{ $communities[$i]->name }}
                                        
                                    @endif

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

我正在尝试查看用户创建的社区,但它给我“找不到控制器方法”。

【问题讨论】:

  • 有没有社区控制器相关的资源路由?

标签: laravel laravel-9


【解决方案1】:

这可能有多种原因。因此,这里有一个小清单:

  1. 您的路线中缺少控制器路径:
    use AppHttpControllersCommunityController; // this line
    Route::get('/community/{id}', [CommunityController::class, 'community'])->name('profile.community');
    
    1. 您的 CommunityController 类名称拼写错误
    2. CommunityController的命名空间错误

    然后为了确定,运行:php artisan route:clear

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-04
    • 2014-03-09
    • 2014-05-12
    • 2014-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多