我正在检查一些PHP 5.3.0功能,并在网站上遇到一些看起来很有趣的代码:
public function getTotal($tax)
{
$total = 0.00;
$callback =
/* This line here: */
function ($quantity, $product) use ($tax, &$total)
{
$pricePerItem = constant(__CLASS__ . "::PRICE_" .
strtoupper($product));
$total += ($pricePerItem * $quantity) * ($tax + 1.0);
};
array_walk($this->products, $callback);
return round($total, 2);
}
Run Code Online (Sandbox Code Playgroud)
作为匿名函数的一个例子.
有人知道吗?有文件吗?如果它被使用它看起来很邪恶?
我正在将项目移植到Laravel.
我有两个数据库表,彼此之间处于一对多关系.它们有三个条件.如何在Eloquent中建立这种关系模型?
我不应该修改数据库模式,因为它必须保持向后兼容其他东西.
我尝试了以下,但它不起作用.
拥有方:
use Illuminate\Database\Eloquent\Model;
class Route extends Model
{
public function trips()
{
return $this->hasMany('Trip', 'route_name,source_file', 'route_name,source_file')
}
}
Run Code Online (Sandbox Code Playgroud)
反面:
use Illuminate\Database\Eloquent\Model;
class Trip extends Model
{
public function routes()
{
return $this->belongsTo('Route', 'route_name,source_file', 'route_name,source_file');
}
}
Run Code Online (Sandbox Code Playgroud)
示例Route数据库值:
id | route_name | source_file
---------------------------------------
1 | Berlin - Paris | file1.xls
2 | Madrid - London| file2.xls
3 | Berlin - Paris | file3.xls
Run Code Online (Sandbox Code Playgroud)
示例Trip数据库值:
id | route_name | source_file | duration
--------------------------------------------------------- …Run Code Online (Sandbox Code Playgroud)