Laravel每个公司的独特部门

时间:2018-07-09 01:38:35

标签: laravel laravel-5 uniqueidentifier

我有一个名为部门的表,其中有2列1.名称2.公司,我想对单个公司的名称应用验证。

 public function store(Request $request)
 {
    $company = Auth::user()->company_id;
    $this->validate($request, [
        'company'=>'unique:departments,company',
    ]);

    $department = Department::create([
        'name' => $request->name,
        'company' => $request->company,
    ]);
  }

部门的预期结果是

name      company
======================
Accounts    1
Purchase    1
Accounts    2
Sales       2
Accounts    2  //wrong i want validation here
Purchase    1  //wrong i want validation here

2 个答案:

答案 0 :(得分:1)

这是唯一验证 <DataGrid x:Name="details" x:Uid="MyDataGrid" AutoGenerateColumns="False" AlternationCount="1" SelectionMode="Single" IsReadOnly="True" HeadersVisibility="Column" Margin="0,0,10,2" HorizontalAlignment="Right" Width="410" Grid.Column="1" Grid.Row="1" > <DataGrid.Columns> <DataGridTemplateColumn> <DataGridTemplateColumn.Header> <CheckBox Content=" Select All" x:Name="headerCheckBox" /> </DataGridTemplateColumn.Header> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <CheckBox Name="chkDiscontinue" IsChecked="{Binding Path=IsChecked,ElementName=headerCheckBox,Mode=OneWay}" Margin="45 2 0 0" /> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> <DataGridTextColumn Binding="{Binding Path=file_name}" Header="File Name" Width="2*" IsReadOnly="True" /> <DataGridTextColumn Binding="{Binding Path=file_path}" Header="File Path" Width="0.9*"/> </DataGrid.Columns> </DataGrid> 详细信息https://laravel.com/docs/5.6/validation#rule-unique的格式。 加上附加的where条件,将像这样$inputXML = $inputXML -replace 'mc:Ignorable="d"','' -replace "x:N",'N' -replace '^<Win.*', '<Window' [void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework') [xml]$XAML = $inputXML #Read XAML $reader=(New-Object System.Xml.XmlNodeReader $xaml) try{$Form=[Windows.Markup.XamlReader]::Load( $reader )} catch{Write-Host "Unable to load Windows.Markup.XamlReader. Double-check syntax and ensure .net is installed."} #=========================================================================== # Store Form Objects In PowerShell #=========================================================================== #$xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name "var_$($_.Name)" -Value $Form.FindName($_.Name) -Scope Global } $xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name "var_$($_.Name)" -Value $Form.FindName($_.Name) } $var_search_button.Add_Click({ #JUST TO DISPLAY SOME RANDOM DATA(IT WORKS) $t = Import-Csv .\imp.csv $t | Select @{name = "file_name"; ex={$_.id}},@{name = "file_path"; ex={$_.name}} |% {$var_details.AddChild($_)} #TRYING TO RETRIVE THE ROWS WHICH I CHECK IN UI echo "__Show FORM DATA" | Out-File "imp.txt" $var_details.SelectedCells | Out-File "imp.txt" -Append $var_details.SelectedItems | Out-File "imp.txt" -Append }) $Form.ShowDialog() | out-null #-------------------------------------

在部门名称上添加验证

unique:table,column,except,idColumn

注意:如果您将部门中的unique:table,column,except,idColumn,anotherColumn,anotherColumnValue列名更改为public function store(Request $request) { //If getting company from request $company = $request->company; //if getting company from auth user $company = Auth::user()->company_id; $request->validate([ 'name' => 'required|max:255|unique:departments,name,NULL,id,company,'. $company ]); $department = Department::create([ 'name' => $request->name, 'company' => $company, ]); } ,则更好,因为您要在其中存储公司ID。

答案 1 :(得分:0)

那么,这种关系是多对多的?每个公司都有相同的部门组。 我建议创建一个新的关系表。将为company_department,至少包含两列:department_idcompany_id-这样,您将轻松管理数据,也轻松查询访问数据库。 希望它会有用。