在laravel上的创建函数上传递ID时未找到视图
我想在创建页面上传递在laravel上的创建函数上传递ID时未找到视图,laravel,Laravel,我想在创建页面上传递用户id 我的控制器 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
用户id
我的控制器
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::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”
我想要实现的是,从用户文件夹中的用户列表中,我将在grant loan amount创建页面时传递用户id。但我不明白为什么找不到路线
有什么建议吗?因为您有以下路线:
Route::get('loan/grant loan amounts/create/{userId}','loan\\GrantLoanAmountsController@create')->name('grant-loan-amounts.create');
您为路线命名,因此应按其名称命名:
return redirect()->route('grant-loan-amounts.create',$userId);
不要在view()
函数的第一个参数中传递id。仅在view函数的第一个参数中提及view文件。使用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
}
Pull需要一个以上的参数来获取数据
public function create($user)
{
$loanAmount=LoanAmount::all()->pluck('amount','id');
$userId=User::findOrFail($user);
return view('YourBladeFileFolderLocationOrFileLocationHere')->compact('userId','loanAmount');
}
现在您可以在blade文件中获取或写入,如
对于Id:
{{$userId->id}
用户名:
{{$userId->name}这似乎不是路由问题。。。我认为问题在于返回视图('loan/grant loan amounts/create/'。$userId)
。。。我认为您的数据库中没有每个用户id的视图(如1.blade.php、2.blade.php等),我相信您希望将用户重定向到该贷款金额路径,该路径将返回视图,因此您只需使用redirect
方法即可尝试此return redirect()->路径(“grant-loan-amounts.create”,$userId)->with($userId)->with($loanAmount);
hi@SalmanZafar这给了我一个错误,ErrorException(E_警告)非法的偏移量类型hi@IlGala是的,我没有一个对应的。我只需要抓取用户的id,这样我就可以把它放在隐藏字段中。放置压缩实际上是有效的。这就是我的返回视图现在的样子,“返回视图('loan.grant loan amounts.create',压缩('userId','loanAmount');"