doproj123
6 months ago

laravel 5.8 - cutom rule validation with array with values as paramter and query database

Posted 6 months ago by doproj123

I need to create a method that will retrieve several data from the form and substitute for querying to database. This is simple reservation. Before entering the database I need to know if the given date and car are aviable. Is it possible? Or maybe there are better solutions?

view/add.blade.php

 <form method="POST" action="{{ route('/new/store') }}">
 <table style="width:100%">
  <tr>
    <th>Date 1</th>
    <th>Date 2</th>
    <th>Car</th>
  </tr>  
  <tr>
    <td><input type="text" name="date1"></td>
    <td><input type="text" name="date2"></td>
    <td><input type="text" name="car"></td>
  </tr>
</table> 
<button type="submit" class="btn btn-primary">{{ __('Add') }}</button>
 </form>

CarController.php

 public function store(Request $request)
 {
    $reqquest->({
        'date1' => ['required', 'date', 'date_format:Y-m-d'],
        'date2' => ['required', 'date', 'date_format:Y-m-d'],
        'car' => ['required'],
    });
 }

ValidateDate.php // class which implements Rule

public function passes($attribute, $value) <------ $value can be array ?
{
    //if $value can be array can i do something like this

    $results = DB::select('select * from reseravation where (date_from <= '$value['date1']' and date_to >= '$value['date2']') and car = '$value['car']' ');

    if(!empty($results))
    {
        return true;
    }
    else
    {
        return false;
    }
}

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