Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

NoTimeForCaution's avatar

Validate Vue Object in Laravel Controller

Sending data from Vue to Laravel Controller via:

urgent: {
      title: 'Message Title',
      body: 'Body of the message',
      svg: 'exclamation'
}

axios.post('/urgent', { urgent: this.urgent })

Laravel Controller:

$attributes = request('urgent')->validate([
     'title' => 'required|min:5|max:24', 
     'body' => 'required|min:10|max:255', 
     'svg' => 'required|min:3|max:25'
]);
        
$urgent = Urgent::create($attributes);

Telescope Payload:

urgent: {
     title: "VTO BID CLOSES TODAY",
     body: "If you got a VTO option during initial bid - don't forget to finish bid.",
     svg: "exclamation"
}

Telescope Response:

message: "Call to a member function validate() on array",
exception: "Symfony\Component\Debug\Exception\FatalThrowableError"
0 likes
14 replies
rawilk's avatar

You're trying to validate on the data itself and not the request object. Try this instead:

$attributes = request()->validate([
     ...
]);
NoTimeForCaution's avatar

I have, it yields:

{
message: "The given data was invalid.",
errors: {
title: [
"The title field is required."
],
body: [
"The body field is required."
],
svg: [
"The svg field is required."
]
}
}
rawilk's avatar

In your rules, try this:

'urgent.title' => 'required|min:5|max:24',
...

If you don't like doing that, then make the ajax request like this instead:

axios.post('/urgent', { ...this.urgent });
NoTimeForCaution's avatar

I have actually tried that as well.... ;-)

Response:

message: "SQLSTATE[HY000]: General error: 1364 Field 'title' doesn't have a default value (SQL: insert into `urgents` (`updated_at`, `created_at`) values (2019-02-08 22:25:58, 2019-02-08 22:25:58))",
rawilk's avatar

Do you have those fields in $fillable on your model?

NoTimeForCaution's avatar

I do. FWIW, the following works......

VUE

axios.post('/urgent', { title: this.urgent.title, body:this.urgent.body, svg:this.urgent.svg })

CONTROLLER

$attributes = request()->validate([
                'title' => 'required|min:5|max:24', 
                'body' => 'required|min:10|max:255', 
                'svg' => 'required|min:3|max:25'
            ]);
NoTimeForCaution's avatar

I just trying to refactor so I don't have to send each attribute individually.......

rawilk's avatar

What happens if you dd($attributes) in your controller?

rawilk's avatar

Well then if you're calling Urgent::create($attributes) it should create the model record correctly, unless you haven't provided those attributes in the model's $fillable array or just made $guarded = [] on the model.

NoTimeForCaution's avatar

It's all there. I can update Models with the same approach but calling validate() fails when I pass the entire object instead of individual attributes????

NoTimeForCaution's avatar

It seems as if I'm using values instead of keys in the controller.

CONTROLLER:

request('urgent.title')    // returns 'A Valid Title'
rawilk's avatar

If you're still passing the data to the controller as an array inside of urgent, then just do this:

Urgent::create($attributes['urgent']);
NoTimeForCaution's avatar

What you've suggested works to create and update but I still want to to validate the entire object. It's the validate() method that throws the error.

For now, I will just send each attribute via { id: this.urgent.id, ....etc } but if someone stumbles upon this with a solution - I'll take it.

Thanks @wilk_randall

Please or to participate in this conversation.