簡體   English   中英

在Eloquent屬性中返回查詢的模型

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM