【发布时间】:2020-10-18 06:31:38
【问题描述】:
我从我的数据库中得到一个集合
$events = DB::table('events')
->select('id', 'eventId', 'resourceId', 'title', 'start', 'end')
->get();
并想添加一个字段color,以便我可以根据字段title 的值设置颜色值。我的方法不起作用,echo 和 put 给出了错误。
$events->put('color', 'blue');
$events->each(function($item, $key){
if($item == 'Garage')
echo $item;
});
【问题讨论】:
-
那是query builder,而不是Eloquent。如果你使用 Eloquent,你可以创建一个 accessor
-
这不是一个雄辩的查询,而是使用查询生成器的查询。如果它是一个雄辩的查询,您将直接使用 Event 模型,您可以将
color添加到visible数组并在模型上定义一个getColorAttribute函数:laravel.com/docs/7.x/eloquent-mutators#defining-an-accessor 你看到了什么单词 5?跨度> -
我知道我可以使用访问器来访问已经存在的列。但我还需要将
color列添加到数据库中不存在的集合中。