在Eloquent属性中返回查询的模型 [英] Return queried model in Eloquent attribute

查看:61
本文介绍了在Eloquent属性中返回查询的模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Laravel雄辩的ORM,我希望能够将查询的laravel模型作为一个属性或更理想地作为雄辩的关系返回.这就是我想要做的:

I'm using Laravel's eloquent ORM, and I was hoping to be able to return a queried laravel model as an attribute, or more ideally as an eloquent relation. This is what I'm trying to do:

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;
    }
}

这是尝试获取重叠的日历事件,该事件将是冲突事件,并作为每个事件的对象返回.

This is an attempt to get over-lapping calendar events, which would be the conflicting events, returned as an object against each event.

这将意味着任何查询运行都将返回冲突:

This would then mean any query run would return the conflicts :

$event = CalendarEvent::where('id','=',123)->first()->toJson();
$event; // JSON object should contain conflicting events

这将导致非Laravel 500错误和空白页.存储器/日志/laravel未附加.我的/var/log/site-error.log在请求期间附加了以下内容:

This results in a non-laravel 500 error, and a blank page. The storage/logs/laravel is not appended to. My /var/log/site-error.log has this appended during the request:

PHP消息:PHP 324. 照亮\ Database \ Eloquent \ Model-> attributesToArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2454 PHP讯息:PHP 325. 照亮\ 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消息:PHP 328. Illuminate \ Support \ Collection-> Illuminate \ Support {关闭}() /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/Illuminate/Support/Collection.php:1102 PHP讯息:PHP 330. 照亮\ Database \ Eloquent \ Model-> attributesToArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2454 PHP讯息:PHP 331. 照亮\ 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消息:PHP333.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消息:PHP 370. Illuminate \ Support \ Collection-> Illuminate \ Support {关闭}() /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消息:PHP 373. 照亮\ 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消息:PHP 376. Illuminate \ Support \ Collection-> Illuminate \ Support {关闭}() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/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消息:PHP 378. 照亮\ Database \ Eloquent \ Model-> attributesToArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2454 PHP消息:PHP 379. 照亮\ Database \ Eloquent \ Model-> mutateAttributeForArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2518 PHP消息:PHP380.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. Illuminate \ Support \ Collection-> Illuminate \ Support {关闭}() /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消息:PHP 420. 照亮\ Database \ Eloquent \ Model-> attributesToArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2454 PHP消息:PHP 421. 照亮\ 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/Collection.php:1103 PHP消息:PHP 424. Illuminate \ Support \ Collection-> Illuminate \ Support {关闭}() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1103 PHP消息:PHP425.照亮\ Database \ Eloquent \ Model-> toArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1102 PHP消息:PHP 426. 照亮\ Database \ Eloquent \ Model-> attributesToArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2454 PHP消息:PHP 427. 照亮\ 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在stderr中发送的FastCGI: "e/Database/Eloquent/Model.php:2518 PHP消息:PHP 464. 照亮\ Support \ Collection-> toArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2764 PHP消息:PHP465.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消息:PHP467.Illuminate \ Database \ Eloquent \ Model-> toArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1102 PHP消息:PHP 468. 照亮\ Database \ Eloquent \ Model-> attributesToArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2454 PHP消息:PHP 469. 照亮\ 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/framework/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 {关闭}() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1103 PHP消息:PHP473.Illuminate\ Database \ Eloquent \ Model-> toArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1102 PHP消息:PHP 474. 照亮\ Database \ Eloquent \ Model-> attributesToArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2454 PHP消息:PHP475.照亮\ Database \ Eloquent \ Model-> mu 2016/12/04 14:21:16 [错误] 792#0:* 42880在stderr中发送的FastCGI: "ry \ Grammars \ MySqlGrammar-> wrapValue() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Grammar.php:79 PHP讯息:PHP 511. 照亮\ Database \ Query \ Grammars \ MySqlGrammar-> isJsonSelector() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Query/Grammars/MySqlGrammar.php:225 PHP消息:PHP512.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"

PHP message: PHP 324. Illuminate\Database\Eloquent\Model->attributesToArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2454 PHP message: PHP 325. Illuminate\Database\Eloquent\Model->mutateAttributeForArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2518 PHP message: PHP 326. Illuminate\Support\Collection->toArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2764 PHP message: PHP 327. array_map() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1103 PHP message: PHP 328. Illuminate\Support\Collection->Illuminate\Support{closure}() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1103 PHP message: PHP 329. Illuminate\Database\Eloquent\Model->toArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1102 PHP message: PHP 330. Illuminate\Database\Eloquent\Model->attributesToArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2454 PHP message: PHP 331. Illuminate\Database\Eloquent\Model->mutateAttributeForArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2518 PHP message: PHP 332. Illuminate\Support\Collection->toArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2764 PHP message: PHP 333. array_map() /home/vagrant/silverback/ 2016/12/04 14:21:16 [error] 792#0: *42880 FastCGI sent in stderr: "home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1103 PHP message: PHP 370. Illuminate\Support\Collection->Illuminate\Support{closure}() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1103 PHP message: PHP 371. Illuminate\Database\Eloquent\Model->toArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1102 PHP message: PHP 372. Illuminate\Database\Eloquent\Model->attributesToArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2454 PHP message: PHP 373. Illuminate\Database\Eloquent\Model->mutateAttributeForArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2518 PHP message: PHP 374. Illuminate\Support\Collection->toArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2764 PHP message: PHP 375. array_map() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1103 PHP message: PHP 376. Illuminate\Support\Collection->Illuminate\Support{closure}() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1103 PHP message: PHP 377. Illuminate\Database\Eloquent\Model->toArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1102 PHP message: PHP 378. Illuminate\Database\Eloquent\Model->attributesToArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2454 PHP message: PHP 379. Illuminate\Database\Eloquent\Model->mutateAttributeForArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2518 PHP message: PHP 380. Illuminate\Support\Collection->toArray() /home/vagrant/silverback/httpdocs/vendor/larav 2016/12/04 14:21:16 [error] 792#0: *42880 FastCGI sent in stderr: "ck/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2764 PHP message: PHP 417. array_map() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1103 PHP message: PHP 418. Illuminate\Support\Collection->Illuminate\Support{closure}() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1103 PHP message: PHP 419. Illuminate\Database\Eloquent\Model->toArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1102 PHP message: PHP 420. Illuminate\Database\Eloquent\Model->attributesToArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2454 PHP message: PHP 421. Illuminate\Database\Eloquent\Model->mutateAttributeForArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2518 PHP message: PHP 422. Illuminate\Support\Collection->toArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2764 PHP message: PHP 423. array_map() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1103 PHP message: PHP 424. Illuminate\Support\Collection->Illuminate\Support{closure}() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1103 PHP message: PHP 425. Illuminate\Database\Eloquent\Model->toArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1102 PHP message: 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 [error] 792#0: *42880 FastCGI sent in stderr: "e/Database/Eloquent/Model.php:2518 PHP message: PHP 464. Illuminate\Support\Collection->toArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2764 PHP message: PHP 465. array_map() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1103 PHP message: PHP 466. Illuminate\Support\Collection->Illuminate\Support{closure}() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1103 PHP message: PHP 467. Illuminate\Database\Eloquent\Model->toArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1102 PHP message: PHP 468. Illuminate\Database\Eloquent\Model->attributesToArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2454 PHP message: PHP 469. Illuminate\Database\Eloquent\Model->mutateAttributeForArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2518 PHP message: PHP 470. Illuminate\Support\Collection->toArray() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2764 PHP message: PHP 471. array_map() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1103 PHP message: PHP 472. Illuminate\Support\Collection->Illuminate\Support{closure}() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1103 PHP message: 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 message: PHP 475. Illuminate\Database\Eloquent\Model->mu 2016/12/04 14:21:16 [error] 792#0: *42880 FastCGI sent in stderr: "ry\Grammars\MySqlGrammar->wrapValue() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Grammar.php:79 PHP message: PHP 511. Illuminate\Database\Query\Grammars\MySqlGrammar->isJsonSelector() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Query/Grammars/MySqlGrammar.php:225 PHP message: PHP 512. Illuminate\Support\Str::contains() /home/vagrant/silverback/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Query/Grammars/MySqlGrammar.php:255" while reading response header from upstream, client: 192.168.42.1, server: silverback.dev, request: "GET /test HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "silverback.dev"

更新: 我已经决定使用雄辩的ORM不可能做到这一点.我本质上想做的是像这样的Mysql,以关系形式附加:

UPDATE: I've decided this just isn't possible using Eloquent ORM. What I'm essentially trying to do is a Mysql like this, attaching as a relationship:

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循环来在获取数据后附加数据.我相信这是我唯一的选择.

Instead I've relunctently opted for a foreach loop to attach the data after it has been fetched. I believe this is my only option.

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'
        );
    }
}

推荐答案

该错误似乎是由于试图在数组中获取其他模型实例的附加属性引起的.另一种方法可能是像

The error seems to be arising out of appended attribute, where you are trying to get other model instances in an array. A different approach could be to define a relation on self like

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

尚未测试,但尝试查看是否可行.

Haven't tested, but try to see if it works.

编辑

尝试使用延迟加载参数

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

这篇关于在Eloquent属性中返回查询的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆