publish a post only if the required fields are stored in db
publish a post only if the required fields are stored in db
I have a link to publish a post that is in draft status (stats = 'D' in posts table):
<a href="{{route('posts.publish', ['id' => $post->id])}}">Publish</a>
I created a route for this link:
Route::get('post/{id}/publish', [ 'uses' => 'PostsController@publish', 'as'=>'posts.publish']);
When the link "Publish" the code goes to the PostsController publish(). In this method is necessary to verify if all mandatory fields for a post be published are not null. If they are null the post should not be published, it should appear a message informing the user that the required fields (name, categories, image, content, etc) needs to be introduced before the post be published. Otherwise the post should be published, that is the status should be updated from "D" to "P". Do you know how to achieve that? Is necessary to do a query with the post id and check for each required field if is not null?
public function publish($id)
{
dd($id);
}
Thanks, but the link "Publish" is not in a form. So how to use the " $this->validate($request, [... ]);"? The link "Publsih" is not in the create post page or edit post page, its on another page, so when the link "Publish" is clicked the $request dont have the post data.
– John
Jun 29 at 10:32
2 Answers
2
I assume you display publish
button after checking column stats
which is D
the draft status
publish
stats
D
So use like this
public function publish($id)
{
$p = DB::table('posts')->where('id',$id)->first();
if($p->name && $p->categories && $p->image && $p->content){
$p->stats = 'P';
$p->save();
return redirect('/posts')->with("message","Updated Successfully!!");
}else{
return redirect()->back()->with("message","Name, Categories, Image Or Content is blank!!");
}
}
very simple
public function publish($id)
{
$check_post_status = Db::table('posts')->where('id','=',$id)
->value('stats');
if($check_post_status === 'D')
{
return redirect()->back()
->with('flash_message','Post is not published yet');
}
$post = Db::table('posts')->where('id','=',$id)->get();
return $post;
}
Hope you got your answer
Thanks, but like that is just verifying the status of the post. What is necessary is verify if the required fields of the post are not null.
– John
Jun 29 at 10:29
you can use laravel validation to handle this laravel.com/docs/5.6/validation
– Salman Zafar
Jun 29 at 10:33
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Use the exists validation rule.
– DigitalDrifter
Jun 29 at 10:30