in my scenario i am developing a blogging application my flow is that user will first either registered or login to start posting. i want when a user registered it self it shall be redirected to their page where they can see their post and perform crud on their post, and if user click on home button there they can see posts from all user including their own. how can i track the user in this multiple pages.
my user controller method where user is regitered
public function store(Request $request)
{
if (isset($request['register'])){
$name = $request->input('name');
$email = $request->input('email');
$password = Hash::make($request->input('password'));
$users = User::all();
foreach ($users as $user)
{
if($user->email == $email){
echo "<h2>Email '".$user->email. "'already exits!</h2><br/>";
echo "If you already have an account click here<a href='/login'>Log In</a> ";
return view('user.createuser');
}
}
DB::insert('insert into users (name,email,password) values(?,?,?)',[$name,$email,$password]);
return redirect('My Posts');
}
2. my route after registration
Route::get('My Posts','PostController@userposts');
3. my post controller function
public function userPosts(){
$posts = post::all();
return view('post.userposts',compact('posts'));
}
now how can i get posts of this user in the userposts view
@hidayat3676 that was just an example. You obviously need to use whatever route url, that corresponds to your application.
But the route you have set, does not work as is. You need to define a place, where each individual user can find their posts. Like /posts/user or /account/user etc.
public function userPosts(){
$posts = Post::where('user_id', auth()->id);
return view('post.userposts',compact('posts'));
}
how i can get the user id here