我正在使用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消息:PHP 324。 照亮\数据库\锋\型号 - &GT; attributesToArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2454 PHP消息:PHP 325。 照亮\数据库\锋\型号 - &GT; mutateAttributeForArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2518 PHP消息:PHP 326. Illuminate \ Support \ Collection-&gt; toArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2764 PHP消息:PHP 327. array_map() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1103 PHP消息:PHP 328。 照亮\支持\收藏 - &GT;照亮\支持{闭合}() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1103 PHP消息:PHP 329. Illuminate \ Database \ Eloquent \ Model-&gt; toArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1102 PHP消息:PHP 330。 照亮\数据库\锋\型号 - &GT; attributesToArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2454 PHP消息:PHP 331。 照亮\数据库\锋\型号 - &GT; mutateAttributeForArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2518 PHP消息:PHP 332. Illuminate \ Support \ Collection-&gt; 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: &#34;家用/流浪/银背/的httpdocs /供应商/ laravel /框架/ SRC /照亮/支持/ Collection.php:1103 PHP消息:PHP 370。 照亮\支持\收藏 - &GT;照亮\支持{闭合}() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1103 PHP消息:PHP 371. Illuminate \ Database \ Eloquent \ Model-&gt; toArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1102 PHP消息:PHP 372。 照亮\数据库\锋\型号 - &GT; attributesToArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2454 PHP消息:PHP 373。 照亮\数据库\锋\型号 - &GT; mutateAttributeForArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2518 PHP消息:PHP 374. Illuminate \ Support \ Collection-&gt; toArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2764 PHP消息:PHP 375. array_map() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1103 PHP消息:PHP 376。 照亮\支持\收藏 - &GT;照亮\支持{闭合}() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1103 PHP消息:PHP 377. Illuminate \ Database \ Eloquent \ Model-&gt; toArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1102 PHP消息:PHP 378。 照亮\数据库\锋\型号 - &GT; attributesToArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2454 PHP消息:PHP 379。 照亮\数据库\锋\型号 - &GT; mutateAttributeForArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2518 PHP消息:PHP 380. Illuminate \ Support \ Collection-&gt; toArray() / home / vagrant / silverback / httpdocs / vendor / larav 2016/12/04 14:21:16 [错误] 792#0:* 42880在stderr发送的FastCGI: &#34; CK /的httpdocs /供应商/ laravel /框架/ SRC /照亮/数据库/锋/ Model.php:2764 PHP消息:PHP 417. array_map() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1103 PHP消息:PHP 418。 照亮\支持\收藏 - &GT;照亮\支持{闭合}() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1103 PHP消息:PHP 419. Illuminate \ Database \ Eloquent \ Model-&gt; toArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1102 PHP消息:PHP 420。 照亮\数据库\锋\型号 - &GT; attributesToArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2454 PHP消息:PHP 421。 照亮\数据库\锋\型号 - &GT; mutateAttributeForArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2518 PHP消息:PHP 422. Illuminate \ Support \ Collection-&gt; toArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2764 PHP消息:PHP 423. array_map() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1103 PHP消息:PHP 424。 照亮\支持\收藏 - &GT;照亮\支持{闭合}() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1103 PHP消息:PHP 425. Illuminate \ Database \ Eloquent \ Model-&gt; toArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1102 PHP消息:PHP 426。 照亮\数据库\锋\型号 - &GT; attributesToArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2454 PHP消息:PHP 427。 照亮\数据库\锋\型号 - &GT; mutateAttributeForArray() /家庭/流浪者/银背/ httpdocs资料/供应商/ laravel /框架/ src目录/照亮/数据库/雄辩/模式 2016/12/04 14:21:16 [错误] 792#0:* 42880 FastCGI发送到stderr: &#34; e / Database / Eloquent / Model.php:2518 PHP消息:PHP 464。 照亮\支持\收藏 - &GT;指定者() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2764 PHP消息:PHP 465. array_map() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1103 PHP消息:PHP 466。 照亮\支持\收藏 - &GT;照亮\支持{闭合}() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1103 PHP消息:PHP 467. Illuminate \ Database \ Eloquent \ Model-&gt; toArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1102 PHP消息:PHP 468。 照亮\数据库\锋\型号 - &GT; attributesToArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2454 PHP消息:PHP 469。 照亮\数据库\锋\型号 - &GT; mutateAttributeForArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2518 PHP消息:PHP 470. Illuminate \ Support \ Collection-&gt; toArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2764 PHP消息:PHP 471. array_map() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1103 PHP消息:PHP 472。 照亮\支持\收藏 - &GT;照亮\支持{闭合}() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1103 PHP消息:PHP 473. Illuminate \ Database \ Eloquent \ Model-&gt; toArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1102 PHP消息:PHP 474。 照亮\数据库\锋\型号 - &GT; attributesToArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2454 PHP消息:PHP 475. Illuminate \ Database \ Eloquent \ Model-&gt; mu 2016/12/04 14:21:16 [错误] 792#0:* 42880 FastCGI发送到stderr: &#34; RY \文法\ MySqlGrammar-&GT; wrapValue() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Grammar.php:79 PHP消息:PHP 511。 照亮\数据库\查询\文法\ MySqlGrammar-&GT; 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, server:silverback.dev,request:&#34; GET / test HTTP / 1.1&#34;,upstream: &#34; fastcgi:// unix:/var/run/php5-fpm.sock:&#34;,主持人:&#34; silverback.dev&#34;
更新 我已经决定使用Eloquent 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'
);
}
}
答案 0 :(得分:1)
错误似乎是由附加属性引起的,您试图在其中获取数组中的其他模型实例。一种不同的方法可以是定义自我关系,如
$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();
Haven未经过测试,但试着看看它是否有效。
修改强>
尝试使用延迟加载的参数
FXMLLoader myLoader = new FXMLLoader(getClass().getResource("Main.fxml"));
MainController mainController = myLoader.getController();
mainController.setConnectedTokenLabel(context.getConnectedName());
答案 1 :(得分:0)
你需要退货
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();
}
}