如何:验证 Laravel 4 中数据库关系的存在?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21444964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:56:04  来源:igfitidea点击:

How to: validate the existence of a database relationship in Laravel 4?

phpvalidationlaravellaravel-4

提问by koosa

I have a model Productthat belongs to a Trend:

我有一个Product属于a 的模型Trend

class Product extends Eloquent {

    public function trend()
    {
      return $this->belongsTo('Trend');
    }

}

And as part of my validation rules I would like to check that this relationship exists, and if not trigger an error using:

作为验证规则的一部分,我想检查这种关系是否存在,如果不存在,则使用以下方法触发错误:

$validator = Validator::make(Input::all(), $rules, $messages);

if ($validator->fails())
{
    ... some redirection code here

is called. I have tried to use the validation existslike below, but it never fires.

叫做。我曾尝试使用如下所示的验证存在,但它永远不会触发。

$rules = array(
    'brand_name'  => 'required|min:3',
    'blurb'       => 'required',
    'link'        => 'required',
    'trend'       => 'exists:trends'
);

I have also tried a few variations on the existsmethod, but nothing ever seems to fire. I know that the instance I am testing on definitely doesn'thave a relationship set.

我还尝试了该exists方法的一些变体,但似乎没有任何效果。我知道我正在测试的实例肯定没有关系集。

What am I doing wrong here?

我在这里做错了什么?

EDIT: I see now from typing this out that I am validating the inputand not the models values. How would I actually validate a model instance's properties instead?

编辑:我现在从输入中看到我正在验证输入而不是模型值。我将如何实际验证模型实例的属性?

回答by Rubens Mariuzzo

I have the following code in a ExchangeRateclass:

我在一个ExchangeRate类中有以下代码:

/**
 * Return the validator for this exchange rate.
 * 
 * @return Illuminate\Validation\Validator A validator instance.
 */
public function getValidator()
{
    $params = array(
        'from_currency_id' => $this->from_currency_id,
        'to_currency_id'   => $this->to_currency_id,
        'valid_from'       => $this->valid_from,
        'rate'             => $this->rate,
        'organization_id'  => $this->organization_id,
    );

    $rules = array(
        'from_currency_id' => ['required', 'exists:currencies,id'],
        'to_currency_id'   => ['required', 'exists:currencies,id', 'different:from_currency_id'],
        'valid_from'       => ['required', 'date'],
        'rate'             => ['required', 'numeric', 'min:0.0'],
        'organization_id'  => ['required', 'exists:organizations,id'],
    );

    return Validator::make($params, $rules);
}

Of course, this ExchangeRateclass also have the associations defined:

当然,这个ExchangeRate类也定义了关联:

public function from_currency()
{
    return $this->belongsTo('Currency', 'from_currency_id');
}

public function to_currency()
{
    return $this->belongsTo('Currency', 'to_currency_id');
}

And all this glued together works like a clock:

所有这些粘合在一起就像一个时钟:

$validator = $exchangeRate->getValidator();

if ($validator->fails())
    throw new ValidationException($validator);