Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 关于部分匹配的雄辩关系_Php_Laravel_Laravel 5 - Fatal编程技术网

Php 关于部分匹配的雄辩关系

Php 关于部分匹配的雄辩关系,php,laravel,laravel-5,Php,Laravel,Laravel 5,我能有一个雄辩的关系,其中只有部分匹配的连接 为了解释,这里有一个模型 class PostcodeCoord extends Model { public function payment() { return $this->hasMany('App\Models\Payment', 'Vendor ZIP', 'postcode'); } } 还有我的另一个模型 class Payment extends Model { public function pos

我能有一个雄辩的关系,其中只有部分匹配的连接

为了解释,这里有一个模型

class PostcodeCoord extends Model {
  public function payment()
  {
    return $this->hasMany('App\Models\Payment', 'Vendor ZIP', 'postcode');
  }
}
还有我的另一个模型

class Payment extends Model {
  public function postcodeCoord()
  {
    return $this->belongsTo('App\Models\PostcodeCoord', 'postcode', 'postcode');
  }
}
我的付款表有一个名为Vendor ZIP的列。我的postcode_coords表有一个名为postcode的列


问题是,英国邮政编码(如SW1A)的第一部分的邮政编码列只有3或4个字符长。而Vendor ZIP是完整的6或7个字符。例如SW1A2AA。

好的,无法在ID字段之间的部分匹配上创建关系。也就是说,我可以想出两种不同的方法来解决这个问题:

在付款表中添加一个名为postalcodegroup的新列。创建或更新模型时,请确保此列填写了邮政编码的第一部分。最好的方法是通过。然后,您可以在payments.postcodegroup和postcode_coords.postcode之间创建关系

在支付模型上编写一个函数,调用该函数时,该函数将执行数据库查询,以从postcode_coords表中检索相关数据

在这两个选项中,选项1可能更干净,尽管它确实需要向表中添加列