基于数据透视表laravel的唯一名称规则验证 [英] unique name rule validation based on pivot table laravel

查看:100
本文介绍了基于数据透视表laravel的唯一名称规则验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个出租公司拥有汽车的系统.许多租赁公司都可以提供汽车.因此,这是一个多对多的关系.

I have a system where renting companies have cars. A car can be available in many renting companies. So it is a many-to-many relationship.

我希望为公司提供购买新车时的可能性.如果汽车已经存在,系统将不会创建它,但是会闪烁一条错误消息,提示他们已经拥有该汽车.

I want to provide the companies with the possibility to add new cars when they buy them. If the car already exists, the system wouldn't create it, but flash an error message saying they already have that car.

如何在要添加的汽车名称字段上使用唯一验证规则?挑战在于Car模型没有公司的ID,数据透视表也没有汽车的名称,它仅包含car_id和company_id.

How to use the unique validation rule on the name field of the Car being added? The challenge is that the Car model doesn't have the id of the company, and the pivot table doesn't have the name of the car, it contains just the car_id and the company_id.

非常感谢

我的车模

class Car extends Model
{
    protected $fillable = ['name'];

    protected $dates = ['purchased_at'];

    public function company(){
        return $this->belongsToMany('App\Company')->withPivot('quantity', 'purchased_at')->withTimestamps();
    }
}

我的公司模型

class Company extends Model implements AuthenticatableContract, CanResetPasswordContract
{
    use Authenticatable, CanResetPassword;

    protected $table = 'companies';

    protected $hidden = ['password', 'remember_token'];

    public function setPasswordAttribute($password){
        $this->attributes['password'] = bcrypt($password);
    }

    public function cars(){
        return $this->belongsToMany('App\Car')->withPivot('quantity', 'purchased_at')->withTimestamps();
    }

}

我的汽车控制器

class CarsController extends Controller
{


    public function store(CarRequest $request)
    {
        $car = new Car;
        $car->name = $request->input('name');
        $car->save();
        $car->company()->attach(Auth::user()->id,array('quantity' => $request->input('quantity'),'purchased_at' => \Carbon\Carbon::now()));

        return Redirect('companies/'. Auth::user()->id .'/cars')->with(['success'=>'You have just created a new car!']);
    }
}

我的购车请求

class CarRequest extends Request
{
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'name'          => 'required | unique:car_company,car_id',
            'quantity'      => 'required'
        ];
    }
}

推荐答案

我找到了解决方案.基本上,我们可以有条件地修改规则条目.在这种情况下,我在经过身份验证的公司内寻找汽车,如果汽车名称存在,那么我会将规则更改为cars表中的唯一规则,因为该表中已经存在具有相同名称的汽车,所以该规则将失败. 这是我的CarRequest类中的新规则函数:

I found a solution. Basically, we can conditionally modify a rule entry. In this case, I look for cars inside the authenticated company, if the car name exists, then I change the rule to be unique on the cars table, which will fail because there is already a car with the same name in this table. Here is my new rules function inside my CarRequest class:

public function rules()
{
    $rules = [
        'quantity'      => 'required',
        ]; 
    $car = Auth::user()->cars->where('name', $this->input('name'));

    if (count($car) === 0)
    {
        $rules['name'] = 'required';
    }
    else{
        $rules['name'] = 'required | unique:cars';
    }
    return $rules;
}

这篇关于基于数据透视表laravel的唯一名称规则验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆