I create a multi-author blogging app on laravel 5 and the URL is demo.com/@username/post-url. Now, I have 2 questions regarding this.
First, How to make URL unique for every author only. The controller for publishing the post is as follows:
public function addPostRequest(Request $request){
if(Auth::check() && Auth::user()->role = 'author' && $request->isMethod('post')){
if($request->has('url') && $request->has('title') && $request->has('tags') && $request->has('content')){
$this->validate($request, [
'url' => 'Required|unique:posts',
'title' => 'Required|Min:3|Max:300|',
'tags' => 'Required|Min:3',
'content' => 'Required|Min:100',
]);
$post = new post;
$post->title = $request->input('title');
$post->user_id = Auth::user()->id;
$post->save();
$post->post_id = post::where('url','=',$post->url)->pluck('id');
$request->session()->flash('success', $post->url);
return Redirect::back();
}else{
return 'not allowed';
}
}
Second, if I put someone else username like demo.com/@username3/post-url the page still opens the same post, even username3 don't write that same url post.
public function post($username, $url){
$post = post::with('author')->where('url','=',$url)->first()
$author = User::where('id','=',$post->user_id)->first();
return view('pages.post')->with(compact('post','author'));
}
How I can overcome this issue. Thanks in advance