Support the ongoing development of Laravel.io →
Forms Validation Queues

I am new to Laravel coming from another framework and needing some help creating a custom validation method.

Basic example is a model that has a foreign_id take User->country_id for example when saving the select form Australia with value of 36 how can i check that the Country Model does infact have a ID of 36

In the other framework I simply created a public function checkModelValues($data, $field, $model, $modelField){ return $this->$model->hasAny(array($modelField => $data[$field])); }

So this ensured that the $model (Countries) has a $modelField (id) that has a value of $data[$field] (36)

How would you go about creating this in Laravel so it can be used anytime you need to validate a foreign_key.

Thanks, Dave

Last updated 2 years ago.
0

I guess you don't even to have to build a custom validation rule.

Just by using the Exists built-in validation rule : http://laravel.com/docs/validation#rule-exists

Like in your case :

$country_id =  Input::get('country_id')
'country_id' => "exists:Countries,id,$country_id"

With the validator :

$validator = Validator::make(
    array(
        'country_id' => $country_id,
    ),
    array(
        'country_id' => "exists:Countries,id,$country_id"
    )
);
Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.