我的数据库如下所示:
菜肴(id,image)
dish_location (location_id,dish_id,dishcard_sharing)
位置(ID,名称)
列图像和 dishcard_sharing 都是 /images/1234/123123.jpg 之类的路径。对于 dishes.image-column ,我刚添加了一个访问器来添加S3-url。现在我也需要 dish_location.dishcard_sharing colunm 这样的访问器。但在哪里实施呢?
<?php
namespace App\Models;
use DB;
use Illuminate\Database\Eloquent\Model;
use Grimzy\LaravelMysqlSpatial\Types\Point;
use App\Models\DishLocationPivot;
class Dish extends Model
{
public $table_name = 'dishes';
public function locations() {
return $this
->belongsToMany('App\Models\Location', 'dish_location', 'dish_id', 'location_id')
->withPivot(['dishcard_sharing']);
}
/* Accessors */
public function getImageAttribute($value) {
return config('filesystems.store_url_prefix') . $value;
}
public function scopeFinalize($query) {
// Dish Card?
$query->addSelect('dish_location.dishcard_sharing AS dishcard_sharing');
return $query;
}
}
答案 0 :(得分:3)
一个选项是创建一个扩展Pivot的新类:
use Illuminate\Database\Eloquent\Relations\Pivot;
class DishLocation extends Pivot {
...
}