首页 文章

在Eloquent属性中返回查询的模型

提问于
浏览
0

我正在使用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. 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消息:PHP 326 . 照亮\ Support \ Collection-> 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. Illuminate \ Support \ Collection-> Illuminate \ Support ()/ home / vagrant / silverback / httpdocs / vendor / laravel / framework / src / Illuminate / Support / Collection.php:1103 PHP消息:PHP 329. Illuminate \ Database \ Eloquent \ Model-> toArray()/ home / vagrant / silverback / httpdocs / vendor / laravel /框架/ src目录/ Illuminate / Support / Collection.php:1102 PHP消息:PHP 330. Illuminate \ Database \ Eloquent \ Model-> attributesToArray()/ home / vagrant / silverback / httpdocs / vendor / laravel / framework / src / Illuminate / Database / Eloquent / Model.php:2454 PHP消息:PHP 331. Illuminate \ Database \ Eloquent \ Model-> mutateAttributeForArray()/ home /vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php: 2518 PHP消息:PHP 332.照亮\ Support \ Collection-> toArray()/ home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2764 PHP消息:PHP 333 .dode_map()/ home / vagrant / silverback / 2016/12/04 14:21:16 [错误] 792#0:* 42880 FastCGI发送到stderr:“home / vagrant / silverback / httpdocs / vendor / laravel / framework / src / Illuminate / Support / Collection.php:1103 PHP消息:PHP 370. Illuminate \ Support \ Collection-> Illuminate \ Support ()/ home / vagrant / silverback / httpdocs / vendor / laravel / framework / src / Illuminate /Support/Collection.php:1103 PH P消息:PHP 371. Illuminate \ Database \ Eloquent \ Model-> toArray()/ home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1102 PHP消息:PHP 372 . Illuminate \ Database \ Eloquent \ Model-> attributesToArray()/ home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2454 PHP消息:PHP 373. Illuminate \ Database \ Eloquent \ Model-> mutateAttributeForArray()/ home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2518 PHP消息:PHP 374. Illuminate \ Support \ Collection-> 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. Illuminate \ Support \ Collection-> Illuminate \ Support ()/ home / vagrant / silverback / httpdocs / vendor / laravel / framework / src目录/ ILLU minate / Support / Collection.php:1103 PHP消息:PHP 377. Illuminate \ Database \ Eloquent \ Model-> toArray()/ home / vagrant / silverback / httpdocs / vendor / laravel / framework / src / Illuminate / Support / Collection . php:1102 PHP消息:PHP 378. Illuminate \ Database \ Eloquent \ Model-> attributesToArray()/ home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2454 PHP消息:PHP 379. Illuminate \ Database \ Eloquent \ Model-> mutateAttributeForArray()/ home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2518 PHP消息:PHP 380 . 照亮\支持\收藏 - > toArray()/ home / vagrant / silverback / httpdocs / vendor / larav 2016/12/04 14:21:16 [错误] 792#0:* 42880 FastCGI发送到stderr:“ck / httpdocs / vendor / laravel / framework / src / Illuminate / Database / Eloquent / Model.php:2764 PHP消息:PHP 417 . array_map()/ home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1103 PHP消息:PHP 418. Illuminate \ Support \ Collection-> Illuminate \ Support () /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1103 PHP消息:PHP 419. Illuminate \ Database \ Eloquent \ Model-> toArray()/ home / vagrant / silverback /httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1102 PHP消息:PHP 420. Illuminate \ Database \ Eloquent \ Model-> attributesToArray()/ home / vagrant / silverback / httpdocs / vendor / laravel /framework/src/Illuminate/Database/Eloquent/Model.php:2454 PHP消息:PHP 421. Illuminate \ Database \ Eloquent \ Model-> mutateAttributeForArray()/ home / vagrant / silverback / httpdocs / vendor / laravel / framework / src /Illuminate/Database/Eloquent/Model.php:2518 PHP消息:PHP 422. Illuminate \ Support \ Collection-> 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.照亮\支持\ Collection-> Illuminate \ Support ()/ home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1103 PHP消息:PHP 425. 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 message:PHP 427. 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消息:PHP 464.Ilg minate \ Support \ Collection-> toArray()/ 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. Illuminate \ Support \ Collection-> Illuminate \ Support ()/ home / vagrant / silverback /httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1103 PHP消息:PHP 467. Illuminate \ Database \ Eloquent \ Model-> toArray()/ home / vagrant / silverback / httpdocs / vendor / laravel /framework/src/Illuminate/Support/Collection.php:1102 PHP消息:PHP 468. Illuminate \ Database \ Eloquent \ Model-> attributesToArray()/ home / vagrant / silverback / httpdocs / vendor / laravel / framework / src / Illuminate /Database/Eloquent/Model.php:2454 PHP消息:PHP 469. Illuminate \ Database \ Eloquent \ Model-> mutateAttributeForArray()/ home / vagrant / silverback / httpdocs / vendor / laravel / framework / src / Illuminate / Database / Eloquent /模式l.php:2518 PHP消息:PHP 470.Iluminate \ Support \ Collection-> 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. Illuminate \ Support \ Collection-> Illuminate \ Support ()/home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1103 PHP消息:PHP 473. Illuminate \ Database \ Eloquent \ Model-> toArray()/ home / vagrant / silverback / httpdocs / vendor / laravel / framework / src / Illuminate / Support / Collection.php:1102 PHP message:PHP 474. Illuminate \ Database \ Eloquent \ Model-> attributesToArray()/ home / vagrant / silverback / httpdocs / vendor / laravel / framework / src / Illuminate / Database / Eloquent / Model.php:2454 PHP消息:PHP 475.照亮\数据库\ Eloquent \ Model-> mu 2016/12/04 14:21:16 [错误] 792#0:* 42880 FastCGI发送到stderr:“ry \ Grammars \ MyS qlGrammar-> 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.dev”

UPDATE: I 've decided this just isn' t可能使用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'
        );
    }
}

2 回答

  • 1

    该错误似乎是由附加属性引起的,您试图在其中获取数组中的其他模型实例 . 一种不同的方法可能是定义一个关于自我的关系

    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();
    

    没有测试过,但试着看它是否有效 .

    EDIT

    尝试使用延迟加载的参数

    $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();
    
  • 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();   
        }
    }
    

相关问题