2016-04-13 17 views
0

Ich habe eine Tabelle, die Produkte für eine Organisation enthält. Das System enthält mehrere Organisationen auf mehrere Mandanten.Laravel 5.2 Validierung mit Spaltenprüfung

Meine products Tabelle hat die folgenden Spalten:

id organisation_id product_id other_columns 

Ich habe eine Anfrage für products geschaffen und ich bin zu wollen nun die Eingabe von der create product Form zu validieren. Die Anforderung ist für product_id für dieses organisation_id

Die rules() Methode in meinem request ist einzigartig zu sein Die

public function rules() 
{ 
    return [ 
     'part_number' => 'required|max:20|unique:products,part_number,'.$request->organisation_id.',organisation_id', 

     'other_fields_here' => ... 

    ]; 
} 

oben nicht funktioniert, so habe ich es versäumt, die Laravel Dokumentation zu verstehen. Danke für alle Hinweise.

Antwort

2

so wollen Sie das Tupel (organisation_id, part_number) eindeutig zu sein. Versuchen Sie das mal:

// ... other rules 
'part_number' => 'unique:products,part_number,NULL,id,organisation_id,'.$organisation_id, 
// other rules ... 

die Optionen sind: unique:table,field,id_to_be_ignored,id_field_of_the_table,additional_where_conditions

wo zusätzliche, wo die Bedingungen Paare sind gleich verglichen werden, so in Ihrem Fall organisation_id, das Feld und $ organisation_id, den Wert.

0

Die Antwort war:

public function rules() 
{ 
    return [ 
     'part_number' => 'unique:products,part_number,NULL,id,organisation_id,' . $orgId), 
Verwandte Themen