I am developing a social network website and one of the functions is followed and unfollow that get you to follow/unfollow another user on the website, I want to know how to make Laravel go back to the profile page after the follow or unfollow happened, is there is a way that I can redirect to the profile controller with the id of the user I just follow/unfollow because that is what the profile controller requires to get the data of the user.
The Following Controller
public function following(Request $request) { $follower=followers::create([ 'id'=>Auth::id(), 'follower_id'=>$request->input('user_id'), ]); return back(); } public function unfollowing(Request $request) { $u=DB::table('followers')->where('id',auth()->id())->where('follower_id',$request->input('user_id'),)->delete(); return back(); }
Get Profile Controller
public function get_all_data(Request $request) { $id=$request->input('user_id'); $userdata=DB::table('users')->select('id','first_name','last_name','profile_img','cover_img') ->where('id',$id)->first(); $is_exist=false; $user=DB::table('followers')->where('follower_id',$id)->where('id',auth()->id())->get(); if(count($user)==1){$is_exist=true;} if($id!=auth()->id()) { return view('show-prof')->with(compact('userdata','is_exist')); } else { return view('profile'); }
web.php
Route::post('get-profile', ['as' => 'get-profile', 'uses' => 'AppHttpControllersshowprofile@get_all_data'])->middleware('auth'); Route::post('follow', [AppHttpControllersfollow::class, 'following'])->name('follow'); Route::post('unfollow', [AppHttpControllersfollow::class, 'unfollowing'])->name('unfollow');
TLDR:- I want to get back to the profile page of the user I just follow or unfollow
Advertisement
Answer
There are so many ways to do it with laravel but one the best is:
return redirect('/profile/{id}');
you can use Laravel wildcard