[英]laravel redirect with compact does not pass variable
我試圖將帶有緊湊的數組傳遞給視圖,我知道我可以像這樣return view('dashboard',compact('user'));
但我想使用重定向。 當我使用redirect('dashboard')->with(compact('user'));
它在我的儀表板視圖上顯示錯誤的undefined variable user
。 可能是什么問題呢 ? 請幫忙嗎?
請在我的方法下面找到重定向到我的儀表板視圖的方法
public function store(UsercreateRequest $request)
{
$image =$request->file('image');
if($request->hasFile('image'))
{
if($image->isValid())
{
$way=public_path('images');
$extension=$image->getClientOriginalExtension();
$name=$image->getClientOriginalName();
//echo $name;
if(file_exists($way.'/'.$name))
{
if($image->move($way,$name))
{
//echo'ok '; //75485205
//echo $name;
$data= array_merge($request->all(),['image'=>$name]);
$user=$this->userRepository->store($data);
return redirect('dashboard')->with(compact('user'));
}
}
}
}
return redirect('signup')->withErrors(" L'enrisgrement n'a pas abouti !");
//
}
返回redirect('dashboard')-> with(compact('user'));
你不能這樣做。 路線dashboard
將具有自己的控制器方法,該方法將加載視圖。 因此,在重定向過程中傳遞數據將無效。 您需要返回視圖響應才能使其像您自己所說的那樣工作。
通常,當存儲新項目時,重定向將是到新創建的項目。 由於您正在創建新用戶,因此如果有新用戶,則可以將其重定向到該用戶的個人資料頁面。
return redirect('users/' . $user->id);
演出晚了,但是如果有人碰到該頁面並需要一個答案:您可以將關聯數組傳遞給with
:
redirect('dashboard')->with([
'user' => $user,
'foo' => 'bar',
'dinner' => 'dolma'
]);
如果return redirect('login')->with('user',$user);
為您工作,並且您可以在“登錄”視圖中使用$user
,這意味着您的設置正確,並且也可以使用$foo
和$dinner
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.