Dronax
7 months ago
15
13
Vue

Display validation and custom errors Laravel in vuejs

Posted 7 months ago by Dronax

I have a method, where I validate request. I have and custom error, see please code:

public function createComment(Request $request) {
   $request->validate(['body' => 'string|min:10', 'type' => 'integer']);

   if($this->lastComment(Auth::user()->id)) {
      return response()->json(['errors' => 'Please try after 24 hours..'], 422);
   } else {
      $comment = Comment::create($request->all());
      return $comment;
   }
}

In vue I have axios:

   axios.post('/createReview', {
                    body: this.body,
                    user_id: this.user_id,
                    type: this.type,
                }).then(response => {
                    this.$emit('created', response.data);
                
                }).catch(error => {
                    this.isLoading = false;
                    this.flash(error.response.data, 'error');
                });

How I can correctly send these errors on component flash? When I get error on component I get [Object object]. I use this flash package: https://www.npmjs.com/package/vue-flash-message

Please sign in or create an account to participate in this conversation.