问题
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