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

Jdubstep1357's avatar

Livewire Validating with multiple models

Hello everyone! I am new to laracasts and also newish to livewire and laravel.

I am trying to use $this->validate on multiple models. It appears that $this->validate only validates the first $var. Is there any way to use $this->validate for all of the other $vars at the same time?

My code:

  $validatedData_Class_Date_Bitmapp = $this->validate([
        'Visible_Public' => 'required',
        'Start_8AM' => 'required',
        'DHS_Certified' => 'required',
        'In_Person' => 'required',
        'IL_Class' => 'required'
    ]);
    Class_Date_BitmappModel::create($validatedData_Class_Date_Bitmapp);
    
    $validatedData_Class_LocationModel = $this->validate([
        'Location_Name' => 'required',
        'Max_Students' => 'required'
    ]);
    Class_LocationModel::create($validatedData_Class_LocationModel);
    
    $validatedData_Class_DateModel = $this->validate([
        'Course_ID' => 'required',
        'Date' => 'required'
    ]);
    Class_DateModel::create($validatedData_Class_DateModel);

    $validatedData_Instructor_Model = $this->validate([
        'Instructor_First_Name' => 'required'
    ]);
    InstructorModel::create($validatedData_Instructor_Model);

    $validated_Course_TypeModel = $this->validate([
        'course_type_id' => 'required'
    ]);
    Course_TypeModel::create($validated_Course_TypeModel);

// HOW TO USE $this->validate to validate ALL of the different models at once?

0 likes
5 replies
tykus's avatar
tykus
Best Answer
Level 104

We validate the Request, not on the Models, so the validation should be a single step:

$validatedData = $this->validate([
        'Visible_Public' => 'required',
        'Start_8AM' => 'required',
        'DHS_Certified' => 'required',
        'In_Person' => 'required',
        'IL_Class' => 'required',
        'Location_Name' => 'required',
        'Max_Students' => 'required',
        'Course_ID' => 'required',
        'Date' => 'required',
        'Course_ID' => 'required',
        'Date' => 'required',
        'course_type_id' => 'required',
]);
// Then model creation
Class_Date_BitmappModel::create($request->only(['Visible_Public', 'Start_8AM', 'DHS_Certified', 'In_Person'));
// etc.

I would consider organising the form input names to namespace the different inputs, e.g.

<input name="bitmapp[Visible_Public]">
4 likes
Jdubstep1357's avatar

@tykus Thank you! What about using wire:model? Is there any major difference? Also, how do you tie in the $validatedData variable? There seems to be no way to reference it.

1 like
thinkverse's avatar

@Jdubstep1357 wire:model binds the input field to the public property of the same name, it's not actual "models" in the case of say Laravels Eloquent models, though you can use wire:model to bind to properties on an Eloquent model.

Also, how do you tie in the $validatedData variable?

$validatedData would be a local variable containing the return value from validate, which is an array containing the value passed in the input, which the property name as the key.

public function save() {
    $validatedData = $this->validate([
        'property_one' => 'required',
        'property_two' => 'required',
    ]);

    // If validation passed, $validateData should be equal to.
    // $validatedData = [
    //     'property_one' => 'what has on the input when saved was called',
    //     'property_two' => 'what has on the input when saved was called',
    // ];
}

You don't have to use $validatedData, you can skip capturing the return value from validate. But it's a great option to use if you have properties that can be optional and you want to make sure to only handle data that has passed validation.

If you're just starting out learning Livewire it's best to think of wire:model as the value on an input field, which you in turn validate with the validate method on your component when an action is triggered, like a submit button.

I'd recommend reading the documentation for properties, maybe focusing a bit on the data binding. Then the validation which @vincent15000 also provided above and the actions section.

2 likes
Jdubstep1357's avatar

Sorry for the late response! Thank you for all of your help. It's been very enlightening! So far, the validation works like a charm. However, for whatever reason, all of the values that get sent to the database appear to be NULL. What's strange is that the success message still shows every time I click the submit button. I would really appreciate a second pair of eyes to look at what I have:

// List of all variables in data
public $Date, $Visible_Public, $Start_8AM, $DHS_Certified,
$In_Person, $IL_Class, $Instructor_First_Name, $Location_Name, $Max_Students,
$Location_ID,  $Course_Name,
$Instructor, $Location;

public function store(Request $request) { $validatedData = $this->validate([ 'Visible_Public' => 'required', 'Start_8AM' => 'required', 'DHS_Certified' => 'required', 'In_Person' => 'required', 'IL_Class' => 'required', 'Location_Name' => 'required', 'Max_Students' => 'required', 'Course_Name' => 'required', 'Date' => 'required', ]);

    Class_Date_BitmappModel::create($request->only([
        'Visible_Public', 'Start_8AM', 'DHS_Certified', 'In_Person'
    ]));

    Class_LocationModel::create($request->only([
        'Location_Name', 'Max_Students'
    ]));

    Class_DateModel::create($request->only([
        'Date' => 'required'
    ]));

    InstructorModel::create($request->only([
        'Instructor_First_Name'
    ]));

    CourseModel::create($request->only([
        'Course_Name'
    ]));

    session()->flash('message', 'Post made successfully');


}

Here is an example of one of my input forms:

    <div class="absolute right-25 md:w-2/3">    
        <input wire:model="Start_8AM" id="Start8" type="radio" value='true' class="w-4 h-4 text-blue-600 bg-gray-100 border-gray-300 focus:ring-blue-500 dark:focus:ring-blue-600 dark:ring-offset-gray-800 focus:ring-2 dark:bg-gray-700 dark:border-gray-600">
        <label name="Start_8AM" for="Start_8AM">Yes</label>    
        <input wire:model="Start_8AM" id="Start8" type="radio" value='false' class="w-4 h-4 text-blue-600 bg-gray-100 border-gray-300 focus:ring-blue-500 dark:focus:ring-blue-600 dark:ring-offset-gray-800 focus:ring-2 dark:bg-gray-700 dark:border-gray-600">
        <label for="Start_8AM">No</label>  
        @error('Start_8AM') <span class="text-red-600/100 error">{{ $message }}</span>@enderror
    </div>

Please or to participate in this conversation.