Laravel 5 How to validate a unique customer name u

2019-03-02 13:41发布

问题:

I had three model: Activity Model, Customer Model and Customeritem Model

How to do the validation checking in store function whereby the customer name should be UNIQUE in each Activity?

Below is each migration files

Activity Model

public function up()
{
    Schema::create('activities', function (Blueprint $table) {
        $table->increments('id');
        $table->string('activityName');
        $table->string('activityCode');
        $table->string('activityVenue');
        $table->timestamps();
    });
}

Customer Model

public function up()
{
    Schema::create('customers', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('activities_id')->unsigned()->default(0);
        $table->foreign('activities_id')->references('id')->on('activities')->onDelete('cascade');
        $table->text('remark')->nullable();
        $table->timestamps();
    });
}

Customeritem Model

public function up()
{
    Schema::create('customeritems', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('customers_id')->unsigned()->default(0);
        $table->foreign('customers_id')->references('id')->on('customers')->onDelete('cascade');
        $table->string('customerName');
        $table->integer('customerAge')->unsigned();
        $table->timestamps();
    });
}

Controller for Store new entry

public function store(Request $request)
{
    $rules = array(
        'customerName' => 'required|distinct',         
    );
    $messages = array(
        'customerName.required'=>'Customer Name is required',
        'customerName.distinct'=>'Customer Name has a duplicate value.',
    );

Here how to do validation for unique customerName for each activity?

标签: laravel-5.3