0

This is what I have, it is just storing the values multiple times even though I have a rules to check the uniqueness against "appointments" table in the database. Can anyone suggest me how I can store only unique date in my table. Thanks in advance.

    public function store()
        {
            date_default_timezone_set('Australia/Melbourne');
            $date = date('Y/m/d', time());
            $todDate = new DateTime($date);
            $dateFormated = $todDate->format('Y-m-d');

        $input = Input::all();

        $rules = array(
            'appoint_day' => 'required|unique:appointments'
        );

        $validator = Validator::make($input, $rules);

        if($validator->passes())
        {
            $schedule = new Appointment;
            $appoint_day = Input::get('appoint_date');

            if(strtotime($appoint_day) - strtotime($dateFormated) >= 0)
            {
                $schedule->user_id = Auth::user()->id;
                $schedule->appoint_day = $appoint_day;
                $schedule->save();
            }
        }
        else
        {
            echo 'failed';

        }
 return View::make('admin.professions.appointments');
    }

1 Answer 1

1

Your Input value is named appoint_date, but you are validating a field called appoint_day, which presumably does not exist. Change your field name to appoint_day and the validator should function correctly.

Sign up to request clarification or add additional context in comments.

4 Comments

Wow! Quite Strange! appoint_day is a column name in table where is appoint_date is a input field name. Yes, your solution worked but I didn't understand the reason behind it. Thank You so much!!.
It's because the validator expects the name of the fields to check (which is in the Input array), but you have no appoint_day field...so it's not validating anything. Also, the unique rule checks the database using the input field name as he column name, unless you specify otherwise. So if you kept your field name as appoint_date, you could alternatively do 'appoint_date' => 'required|unique:appointments,appoint_day'.
A quick help please related with the above question: How can I validate unique "appoint_date" with unique users ? Means, the same date like 01/01/2010 is unique for User1 also User 2! How about the following: $rules = array( 'appoint_day' => 'required|unique:appointments, user_id' );
I'm not sure I follow what you're trying to accomplish; the code you suggest would check if appoint_day was unique among the field user_id. More info about your intentions is needed...I suggest you post a new question with full details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.