Laravel 5.8
我有一个问题,渴望在某些模型上加载而不是在其他模型上运行。
使用工匠修补匠,我可以奔跑;
$p = App\Programme::find(34)->reviews
并获得正确的结果。如果我将其更改为;
$p = App\Programme::with('reviews')->find(34)
为了使评论热切地加载,它会因错误而失败
PHP Notice: Trying to get property of non-object in .../vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php on line 301
来自工匠的输出,带有查询日志,绑定和执行时间
$p = App\Programme::with('destinations', 'reviews')->find(34)
"select * from `programmes` where `programmes`.`id` = ? and `programmes`.`deleted_at` is null limit 1"
array:1 [
0 => 34
]
1.08
"select `destinations`.*, `programme_destination`.`programme_id` as `pivot_programme_id`, `programme_destination`.`destination_id` as `pivot_destination_id`, `programme_destination`.`created_at` as `pivot_created_at`, `programme_destination`.`updated_at` as `pivot_updated_at` from `destinations` inner join `programme_destination` on `destinations`.`id` = `programme_destination`.`destination_id` where `programme_destination`.`programme_id` in (34)"
[]
0.88
"select `reviews`.*, `programme_reviews`.`programme_id` as `pivot_programme_id`, `programme_reviews`.`review_id` as `pivot_review_id`, `programme_reviews`.`created_at` as `pivot_created_at`, `programme_reviews`.`updated_at` as `pivot_updated_at` from `reviews` inner join `programme_reviews` on `reviews`.`id` = `programme_reviews`.`review_id` where `programme_reviews`.`programme_id` in (34)"
[]
0.85
如果手动运行,则最终查询可以正常工作。
我可以使用用户或目标模型运行完全相同的两个命令,并获得成功的响应。因此$programme->reviews
,与$programme->user
或时的关系一定有所不同$programme->destinations
这是我的模型(已修剪到相关功能);
应用\ BaseModel
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use OwenIt\Auditing\Contracts\Auditable;
use Log;
use DB;
class BaseModel extends Model
{
protected $guarded = ['alias', 'created_at', 'updated_at', 'deleted_at', 'slug'];
public $custom_attributes = [];
public $index_attributes = ['alias', 'user'];
public function alias()
{
return $this->morphOne('App\Alias', 'aliased');
}
public function user()
{
return $this->belongsTo('App\User');
}
}
应用程式
<?php
namespace App;
use App\BaseModel;
use Illuminate\Database\Eloquent\SoftDeletes;
use OwenIt\Auditing\Contracts\Auditable;
use Log;
class Programme extends BaseModel implements Auditable
{
use SoftDeletes;
use \OwenIt\Auditing\Auditable;
protected $table = 'programmes';
protected $dates = ['deleted_at'];
function __construct(array $attributes = array())
{
parent::__construct($attributes);
}
public function destinations()
{
return $this->belongsToMany('App\Destination', 'programme_destination')
->withTrashed()
->withTimestamps();
}
public function reviews()
{
return $this->belongsToMany('App\Review', 'programme_reviews')
->withTrashed()
->withTimestamps();
}
}
应用\评论
<?php
namespace App;
use App\BaseModel;
use Illuminate\Database\Eloquent\SoftDeletes;
use OwenIt\Auditing\Contracts\Auditable;
use Log;
class Review extends BaseModel implements Auditable
{
use SoftDeletes;
use \OwenIt\Auditing\Auditable;
protected $fillable = ['title', 'name', 'first_name', 'last_name', 'review_type', 'email_address', 'created_at'];
function __construct(array $attributes = array())
{
parent::__construct($attributes);
}
public function programmes()
{
return $this->belongsToMany('App\Programme', 'programme_reviews')
->withTrashed()
->withTimestamps();
}
}
我可以$p = App\Programme::with('destinations', 'alias')->find(34)
成功运行。这是目的地的模型
应用\目的地
<?php
namespace App;
use App\BaseModel;
use Illuminate\Database\Eloquent\SoftDeletes;
use OwenIt\Auditing\Contracts\Auditable;
class Destination extends BaseModel implements Auditable
{
use SoftDeletes;
use \OwenIt\Auditing\Auditable;
protected $table = 'destinations';
protected $dates = ['deleted_at'];
function __construct(array $attributes = array())
{
parent::__construct($attributes);
}
public function programmes()
{
return $this->belongsToMany('App\Programme', 'programme_destination')
->withTrashed()
->withTimestamps();
}
}
看来这种关系是基于第一个手工艺命令的,所以为什么当急切加载时这不起作用?
作为参考,这里是数据库创建代码;
程式
CREATE TABLE `programmes` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(191) NOT NULL COLLATE 'utf8_unicode_ci',
`destination_id` INT(10) UNSIGNED NULL DEFAULT NULL,
`user_id` INT(10) UNSIGNED NOT NULL DEFAULT '1',
`created_at` TIMESTAMP NULL DEFAULT NULL,
`updated_at` TIMESTAMP NULL DEFAULT NULL,
`deleted_at` TIMESTAMP NULL DEFAULT NULL,
PRIMARY KEY (`id`),
INDEX `programmes_user_id_foreign` (`user_id`),
INDEX `programmes_destination_id_foreign` (`destination_id`),
CONSTRAINT `programmes_destination_id_foreign` FOREIGN KEY (`destination_id`) REFERENCES `destinations` (`id`),
CONSTRAINT `programmes_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
)
COLLATE='utf8_unicode_ci'
ENGINE=InnoDB
;
评论
CREATE TABLE `reviews` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`title` VARCHAR(191) NULL DEFAULT NULL COLLATE 'utf8_unicode_ci',
`name` VARCHAR(191) NOT NULL COLLATE 'utf8_unicode_ci',
`first_name` VARCHAR(191) NULL DEFAULT NULL COLLATE 'utf8_unicode_ci',
`last_name` VARCHAR(191) NULL DEFAULT NULL COLLATE 'utf8_unicode_ci',
`email_address` VARCHAR(191) NULL DEFAULT NULL COLLATE 'utf8_unicode_ci',
`created_at` TIMESTAMP NULL DEFAULT NULL,
`updated_at` TIMESTAMP NULL DEFAULT NULL,
`deleted_at` TIMESTAMP NULL DEFAULT NULL,
PRIMARY KEY (`id`)
)
COLLATE='utf8_unicode_ci'
ENGINE=InnoDB
;
program_reviews-多对多
CREATE TABLE `programme_reviews` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`programme_id` INT(10) UNSIGNED NOT NULL,
`review_id` INT(10) UNSIGNED NOT NULL,
`created_at` TIMESTAMP NULL DEFAULT NULL,
`updated_at` TIMESTAMP NULL DEFAULT NULL,
PRIMARY KEY (`id`),
INDEX `programme_reviews_review_id_foreign` (`review_id`),
INDEX `programme_id` (`programme_id`),
CONSTRAINT `programme_reviews_programme_id_foreign` FOREIGN KEY (`programme_id`) REFERENCES `programmes` (`id`) ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT `programme_reviews_review_id_foreign` FOREIGN KEY (`review_id`) REFERENCES `reviews` (`id`) ON UPDATE CASCADE ON DELETE CASCADE
)
COLLATE='utf8_unicode_ci'
ENGINE=InnoDB
;
目的地
CREATE TABLE `destinations` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(191) NOT NULL COLLATE 'utf8_unicode_ci',
`user_id` INT(10) UNSIGNED NOT NULL DEFAULT '1',
`created_at` TIMESTAMP NULL DEFAULT NULL,
`updated_at` TIMESTAMP NULL DEFAULT NULL,
`deleted_at` TIMESTAMP NULL DEFAULT NULL,
PRIMARY KEY (`id`),
INDEX `destinations_parent_id_index` (`parent_id`),
INDEX `destinations_user_id_foreign` (`user_id`),
CONSTRAINT `destinations_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
)
COLLATE='utf8_unicode_ci'
ENGINE=InnoDB
;
program_destinations一对多
CREATE TABLE `programme_destination` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`programme_id` INT(10) UNSIGNED NOT NULL,
`destination_id` INT(10) UNSIGNED NOT NULL,
`created_at` TIMESTAMP NULL DEFAULT NULL,
`updated_at` TIMESTAMP NULL DEFAULT NULL,
PRIMARY KEY (`id`),
INDEX `programme_destination_programme_id_foreign` (`programme_id`),
INDEX `programme_destination_destination_id_foreign` (`destination_id`),
CONSTRAINT `programme_destination_destination_id_foreign` FOREIGN KEY (`destination_id`) REFERENCES `destinations` (`id`) ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT `programme_destination_programme_id_foreign` FOREIGN KEY (`programme_id`) REFERENCES `programmes` (`id`) ON UPDATE CASCADE ON DELETE CASCADE
)
COLLATE='utf8_unicode_ci'
ENGINE=InnoDB
;
$programme->destinations
和之间唯一真正的区别$programme->reviews
是reviews
多对多关系。
因此,由于有了外部帮助,我找到了此小问题的答案。
我的问题将无法回答,因为该问题实际上与$pivot
我在Program模型上分配的类变量有关(已删除,以供帖子尝试缩短)。
这个变量与Laravel的内部运作方式发生冲突,据我所知,这似乎非常明显。
更改$pivot
为其他任何内容都可以正确加载该关系。
不太可能有人会遇到与我相同的问题,但谁知道,这可能会帮助某人避免我遇到的错误。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句