Laravel 5 How to validate a unique customer name under each Activity when save

我是研究僧i 提交于 2019-12-20 03:52:36

问题


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?

来源:https://stackoverflow.com/questions/43341798/laravel-5-how-to-validate-a-unique-customer-name-under-each-activity-when-save

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!