[英]Return queried model in Eloquent attribute
我使用的是Laravel雄辩的ORM,并且希望能够将查询的laravel模型作为一个属性或更理想地作为雄辩的关系返回。 这就是我想要做的:
class CalendarEvent extends Model
{
protected $appends = array('conflicts');
public function getConflictsAttribute () {
$conflicts = CalendarEvent::where('calendar_event_type','=',$this->calendar_event_type)
->where('start','<',$this->end)
->where('end','>',$this->start)
->get();
return $conflicts;
}
}
这是一种尝试获取重叠的日历事件(该事件可能是有冲突的事件),并作为每个事件的对象返回。
然后,这意味着任何查询运行都将返回冲突:
$event = CalendarEvent::where('id','=',123)->first()->toJson();
$event; // JSON object should contain conflicting events
这将导致非Laravel 500错误和空白页。 存储器/日志/ laravel未附加。 我的/var/log/site-error.log在请求期间附加了以下内容:
PHP消息:PHP324。Illuminate \\ Database \\ Eloquent \\ Model-> attributesToArray()/home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2454 PHP消息:PHP 325.Illuminate \\ Database \\ Eloquent \\ Model-> mutateAttributeForArray()/home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2518 PHP消息:PHP326。Illuminate \\ Support \\ Collection-> toArray()/home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2764 PHP消息:PHP327。array_map()/ home / vagrant / silverback / httpdocs / vendor / laravel / framework / src / Illuminate / Support / Collection.php:1103 PHP消息:PHP328。Illuminate\\ Support \\ Collection-> Illuminate \\ Support {closure}()/ home / vagrant / silverback / httpdocs /vendor/laravel/framework/src/Illuminate/Support/Collection.php:1103 PHP消息:PHP329。Illuminate\\ Database \\ Eloquent \\ Model-> toArray()/ home / vagrant / silverback / httpdocs / vendor / laravel / framework / src / Il luminate / Support / Collection.php:1102 PHP消息:PHP330。Illuminate \\ Database \\ Eloquent \\ Model-> attributesToArray()/ home / vagrant / silverback / httpdocs / vendor / laravel / framework / src / Illuminate / Database / Eloquent / Model.php:2454 PHP消息:PHP331。Illuminate \\ Database \\ Eloquent \\ Model-> mutateAttributeForArray()/home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php: 2518 PHP消息:PHP332。Illuminate \\ Support \\ Collection-> toArray()/home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2764 PHP消息:PHP 333 。array_map()/ home / vagrant / silverback / 2016/12/04 14:21:16 [错误] 792#0:* 42880在stderr中发送的FastCGI:“ home / vagrant / silverback / httpdocs / vendor / laravel / framework / src / Illuminate / Support / Collection.php:1103 PHP消息:PHP370。Illuminate\\ Support \\ Collection-> Illuminate \\ Support {closure}()/ home / vagrant / silverback / httpdocs / vendor / laravel / framework / src / Illuminate /Support/Collection.php:1103 PHP 消息:PHP371。Illuminate\\ Database \\ Eloquent \\ Model-> toArray()/home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1102 PHP消息:PHP 372。 \\ Database \\ Eloquent \\ Model-> attributesToArray()/home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2454 PHP消息:PHP373。Illuminate\\ Database \\ Eloquent \\ Model-> mutateAttributeForArray()/home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2518 PHP消息:PHP374。Illuminate\\ Support \\ Collection-> toArray( )/home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2764 PHP消息:PHP375。array_map()/ home / vagrant / silverback / httpdocs / vendor / laravel /framework/src/Illuminate/Support/Collection.php:1103 PHP消息:PHP376。Illuminate\\ Support \\ Collection-> Illuminate \\ Support {closure}()/ home / vagrant / silverback / httpdocs / vendor / laravel / framework / src / Illumi nate / Support / Collection.php:1103 PHP消息:PHP377。Illuminate\\ Database \\ Eloquent \\ Model-> toArray()/ home / vagrant / silverback / httpdocs / vendor / laravel / framework / src / Illuminate / Support / Collection。 php:1102 PHP消息:PHP378。Illuminate\\ Database \\ Eloquent \\ Model-> attributesToArray()/home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2454 PHP消息:PHP379。Illuminate \\ Database \\ Eloquent \\ Model-> mutateAttributeForArray()/home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2518 PHP消息:PHP 380 。Illuminate \\ Support \\ Collection-> toArray()/ home / vagrant / silverback / httpdocs / vendor / larav 2016/12/04 14:21:16 [错误] 792#0:* 42880在stderr中发送的FastCGI:“ ck / httpdocs / vendor / laravel / framework / src / Illuminate / Database / Eloquent / Model.php:2764 PHP消息:PHP417。array_map()/ home / vagrant / silverback / httpdocs / vendor / laravel / framework / src / Illuminate / Support /Collection.php:1103 PHP消息:PHP 418。 lluminate \\ Support \\ Collection-> Illuminate \\ Support {closure}()/home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1103 PHP消息:PHP419。Illuminate \\ Database \\ Eloquent \\ Model-> toArray()/home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1102 PHP消息:PHP420。Illuminate\\ Database \\ Eloquent \\ Model-> attributeToArray()/home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2454 PHP消息:PHP421。Illuminate\\ Database \\ Eloquent \\ Model-> mutateAttributeForArray()/ home / vagrant / silverback / httpdocs / vendor / laravel / framework / src / Illuminate / Database / Eloquent / Model.php:2518 PHP消息:PHP422。Illuminate\\ Support \\ Collection-> toArray()/ home / vagrant / silverback / httpdocs / vendor / laravel / framework / src / Illuminate / Database / Eloquent / Model.php:2764 PHP消息:PHP423。array_map()/ home / vagrant / silverback / httpdocs / vendor / laravel / framework / src / Illuminate / Support / Collec tion.php:1103 PHP消息:PHP424。Illuminate\\ Support \\ Collection-> Illuminate \\ Support {closure}()/home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php :1103 PHP消息:PHP425。Illuminate \\ Database \\ Eloquent \\ Model-> toArray()/home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1102 PHP消息:PHP 426. Illuminate \\ Database \\ Eloquent \\ Model-> attributesToArray()/home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2454 PHP消息:PHP427。Illuminate \\ Database \\ Eloquent \\ Model-> mutateAttributeForArray()/ home / vagrant / silverback / httpdocs / vendor / laravel / framework / src / Illuminate / Database / Eloquent / Mode 2016/12/04 14:21:16 [错误] 792#0 :* 42880 FastCGI在stderr中发送:“ e / Database / Eloquent / Model.php:2518 PHP消息:PHP464。Illuminate\\ Support \\ Collection-> toArray()/ home / vagrant / silverback / httpdocs / vendor / laravel / framework /src/Illuminate/Database/Eloquent/Model.ph p:2764 PHP消息:PHP465。array_map()/home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1103 PHP消息:PHP466。Illuminate\\ Support \\ Collection- > Illuminate \\ Support {closure}()/home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1103 PHP消息:PHP467。Illuminate\\ Database \\ Eloquent \\ Model-> toArray()/home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1102 PHP消息:PHP468。Illuminate\\ Database \\ Eloquent \\ Model-> attributesToArray()/ home / vagrant / silverback / httpdocs / vendor / laravel / framework / src / Illuminate / Database / Eloquent / Model.php:2454 PHP消息:PHP469。Illuminate\\ Database \\ Eloquent \\ Model-> mutateAttributeForArray()/ home / vagrant / silverback / httpdocs / vendor / laravel / framework / src / Illuminate / Database / Eloquent / Model.php:2518 PHP消息:PHP470。Illuminate\\ Support \\ Collection-> toArray()/ home / vagrant / silverback / httpdocs / vendor / laravel /框架/ src / Illuminate / Database / Eloquent / Model.php:2764 PHP消息:PHP471。array_map()/home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1103 PHP消息:PHP 472. Illuminate \\ Support \\ Collection-> Illuminate \\ Support {closure}()/home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1103 PHP消息:PHP 473。 \\ Database \\ Eloquent \\ Model-> toArray()/home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1102 PHP消息:PHP474。Illuminate\\ Database \\ Eloquent \\ Model -> attributesToArray()/home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2454 PHP消息:PHP475。Illuminate\\ Database \\ Eloquent \\ Model-> mu 2016 / 12/04 14:21:16 [错误] 792#0:* 42880 FastCGI在stderr中发送:“ ry \\ Grammars \\ MySqlGrammar-> wrapValue()/ home / vagrant / silverback / httpdocs / vendor / laravel / framework / src /Illuminate/Database/Grammar.php:79 PHP消息:PHP 511. Illuminate \\ Database \\ Query \\ Grammars \\ MySqlGrammar-> isJsonSelector()/home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Query/Grammars/MySqlGrammar.php:225 PHP消息:PHP 512. Illuminate \\ Support \\ Str :: contains()/home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Query/Grammars/MySqlGrammar.php:255“,同时从上游读取响应标头,客户端:192.168.42.1,服务器:silverback.dev,请求:“ GET / test HTTP / 1.1”,上游:“ fastcgi:// unix:/var/run/php5-fpm.sock:”,主机:“ silverback。开发”
更新:我已经决定使用雄辩的ORM不可能做到这一点。 我本质上想做的是像这样的Mysql,以关系形式附加:
select * from calendar_events as calendar_events_2 where calendar_events_2.start < calendar_events_1.end AND calendar_events_2.end > calendar_events_1.start
取而代之的是,我无意中选择了foreach循环来将数据提取后附加。 我相信这是我唯一的选择。
foreach ($shoot_timeline_data as $shoot) {
foreach ($shoot->booking_cases as $booking_case) {
$booking_case->booking->conflicts = CalendarEvent::find_clashes(
$booking_case->booking->calendar_event_type,
$booking_case->booking->start,
$booking_case->booking->end,
[$booking_case->booking->id] // excludes this ID via 'whereNotIn'
);
}
}
该错误似乎是由附加属性引起的,在该属性中您试图在数组中获取其他模型实例。 一种不同的方法可能是定义关于自我的关系,例如
class CalendarEvent extends Model{
public function conflicts()
{
return $this->hasMany(CalendarEvent::class)
->where('calender_event_type','=', $this->calendar_event_type);
}
}
然后您可以尝试查询为
$event = CalendarEvent::where('id','=',123)->with('conflicts')->first()->toJson();
尚未测试,但尝试查看是否可行。
编辑
尝试在延迟加载中使用参数
$event = CalendarEvent::where('id', '=', 123)->first();
or
$event = CalendarEvent::findOrFail(123); //it will find the record by given primary key - 123 or fail i.e. throw 'MODELNOTFOUNDEXCEPTION'
$start = $event->start;
$end = $event->end;
$event->load('conflicts', function($query) use ($start, $end){
$query->where('start', '<', $start)
->where('end', '>', $end)
})->get();
您需要退货
class CalendarEvent extends Model
{
protected $appends = array('conflicts');
public function getConflictsAttribute ()
{
return CalendarEvent::where('calendar_event_type','=',$this->calendar_event_type)
->where('start','<',$this->end)
->where('end','>',$this->start)
->get();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.