首页 > 解决方案 > 在 laravel 上的创建功能上传递 ID 中找不到视图

问题描述

我想通过user_id创建页面。

我的controller

public function create($user)
{

    $loanAmount=LoanAmount::all()->pluck('amount','id');
    $userId=User::where('id',$user)->pluck('id')->first();
    // $user->id;
    // dd($user);
    return view('loan/grant-loan-amounts/create/'.$userId)
                ->with($userId)
                ->with($loanAmount);
}

这是我的route

Route::resource('loan/grant-loan-amounts', 'Loan\\GrantLoanAmountsController',[
    'except' => ['create']
]);
Route::get('loan/grant-loan-amounts/create/{userId}', 'Loan\\GrantLoanAmountsController@create')->name('grant-loan-amounts.create');

我将创建页面设为空白。只有“asd”才能显示。

我想要实现的是,从用户文件夹中的用户列表中,我将通过授予贷款金额的用户 ID 创建一个页面。但我无法弄清楚为什么找不到路线。

有什么建议吗?

标签: laravel

解决方案


不要在view()函数的第一个参数中传递 id。仅在视图函数的第一个参数中提及视图文件。compact()将所有带函数的变量作为 的第二个参数传递view()。例子:

public function create($user)
{
    $loanAmount=LoanAmount::all()->pluck('amount','id');
    $userId=User::query()->findOrFail($user)->id;

    return view('user.create',compact('userId','loanAmount')) //in this example 'user' is the folder and 'create' is the file inside user folder
}

推荐阅读