Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 在返回不同对象的关系上阐明雄辩的返回空关系_Laravel_Eloquent_Illuminate Container - Fatal编程技术网

Laravel 在返回不同对象的关系上阐明雄辩的返回空关系

Laravel 在返回不同对象的关系上阐明雄辩的返回空关系,laravel,eloquent,illuminate-container,Laravel,Eloquent,Illuminate Container,我在照明中有一个对象,它有一个关系,可以根据主对象的属性返回不同的对象 public function relation(){ switch($this->type){ case "type_1": return $this->belongsTo('\Models\Type1', 'idElement'); break; case "type_2": return $thi

我在照明中有一个对象,它有一个关系,可以根据主对象的属性返回不同的对象

public function relation(){
    switch($this->type){
        case "type_1":
            return $this->belongsTo('\Models\Type1', 'idElement');
            break;
        case "type_2":
            return $this->belongsTo('\Models\Type2', 'idElement');
            break;
        default:
            return null;
    }
}
当执行“default”部分时,这会产生一个错误“Relationship method必须返回一个类型为illumb\Database\elount\Relations\Relations”的对象”

另外,我不能实例化
新的illumb\Database\Eloquent\Relations\Relations()
,因为它是一个抽象类

我可以创建一个空表并返回一个与该空表的关系,该关系将始终返回一个空值,但这不是一个解决方案

如何在默认选项中返回空关系

更新:

我已将其更改为使用多态关系,但现在的问题是:如何将多态关系设置为可选关系

Relation::morphMap([
    'type_1' => \App\Models\Type1::class,
    'type_2' => \App\Models\Type2::class
]);

....

public function relation(){
    return $this->morphTo(null, 'type', 'idElement');
}

嗯……这可能不是最好的选择,但很有效

public function relation(){
    switch($this->type){
        case "type_1":
            return $this->belongsTo('\Models\Type1', 'idType1');
            break;
        case "type_2":
            return $this->belongsTo('\Models\Type2', 'idType2');
            break;
        default:
            return $this->hasOne('\Models\ThisModel', 'id','id')->where('id','<',0 );//Assuming you have an id autoincrement field that starts in 1  in your table, 
    }
}
公共职能关系(){
开关($this->type){
案例“类型1”:
返回$this->belongsTo('\Models\Type1',idType1');
打破
案例“类型2”:
返回$this->belongsTo('\Models\Type2',idType2');
打破
违约:

return$this->hasOne('\Models\ThisModel','id','id')->where('id','p>Well..这可能不是最好的选择,但很有效

public function relation(){
    switch($this->type){
        case "type_1":
            return $this->belongsTo('\Models\Type1', 'idType1');
            break;
        case "type_2":
            return $this->belongsTo('\Models\Type2', 'idType2');
            break;
        default:
            return $this->hasOne('\Models\ThisModel', 'id','id')->where('id','<',0 );//Assuming you have an id autoincrement field that starts in 1  in your table, 
    }
}
公共职能关系(){
开关($this->type){
案例“类型1”:
返回$this->belongsTo('\Models\Type1',idType1');
打破
案例“类型2”:
返回$this->belongsTo('\Models\Type2',idType2');
打破
违约:

return$this->hasOne('\Models\ThisModel',id',id')->where('id',')我建议创建一个存根关系类,并在需要返回总是空的(或固定的)集合时使用它

<?php

namespace App;

use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\Eloquent\Collection;

/**
 * Stub relationship
 */
class EmptyRelation extends Relation
{
    public function __construct() {}
    public function addConstraints() {}
    public function addEagerConstraints(array $models) {}
    public function initRelation(array $models, $relation) {}

    public function match(array $models, Collection $results, $relation)
    {
        return $models;
    }

    public function getResults()
    {
        return new Collection();
    }
}

我建议创建一个存根关系类,并在需要返回始终为空(或固定)集合时使用它

<?php

namespace App;

use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\Eloquent\Collection;

/**
 * Stub relationship
 */
class EmptyRelation extends Relation
{
    public function __construct() {}
    public function addConstraints() {}
    public function addEagerConstraints(array $models) {}
    public function initRelation(array $models, $relation) {}

    public function match(array $models, Collection $results, $relation)
    {
        return $models;
    }

    public function getResults()
    {
        return new Collection();
    }
}

你好,Hilmi,我没有使用Laravel,它只是纤细框架项目中的照明组件。这种关系将打破Eloquent的热切加载功能。我认为你应该确定类型,并从模型加载不同的关系,而不是这个单一模型。我不确定(我不知道你的情况)但是多态关系可能也会起作用。似乎多态关系是我要寻找的,我将尝试一下。好吧,我已经能够成功地使用多态关系,但当一个关系是可选的时,我面临着同样的问题:你好,Hilmi,我没有使用Laravel,只有纤细框架p中的照明组件项目。这种关系将破坏Elount的急切加载功能。我认为您应该确定类型,并从模型加载不同的关系,而不是单一的模型。我不确定(我不知道您的情况)但是多态关系可能也会起作用。似乎多态关系是我正在寻找的,我将尝试一下。好吧,我已经能够成功地使用多态关系,但当一个关系是可选的时,我面临同样的问题:是的,它应该起作用,但我不想查询数据库中我知道的对象不存在。这将执行一个无意义的查询。它应该可以工作,但我不想在数据库中查询我知道不存在的对象。这将执行一个无意义的查询