Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.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
Php 使用Laravel雄辩词通过多态性具有多种关系_Php_Laravel_Polymorphism_Laravel 5 - Fatal编程技术网

Php 使用Laravel雄辩词通过多态性具有多种关系

Php 使用Laravel雄辩词通过多态性具有多种关系,php,laravel,polymorphism,laravel-5,Php,Laravel,Polymorphism,Laravel 5,我得到了一个相当简单的应用程序,用户可以报告其他用户的评论和食谱。我使用多态关系来存储报告。它工作得很好;然而,我现在正试图了解用户所犯的罪行 获取用户报告不是问题,这可以直接使用user->reports()完成,但我非常希望获取其他人报告所述用户的报告。我可以使用hasManyThrough关系或查询来实现这一点,但一次只能在一个模型上实现 ex. public function offenses() { return $this->hasManyThrough('Recipe

我得到了一个相当简单的应用程序,用户可以报告其他用户的评论和食谱。我使用多态关系来存储报告。它工作得很好;然而,我现在正试图了解用户所犯的罪行

获取用户报告不是问题,这可以直接使用
user->reports()
完成,但我非常希望获取其他人报告所述用户的报告。我可以使用
hasManyThrough
关系或查询来实现这一点,但一次只能在一个模型上实现

ex.

public function offenses() {
    return $this->hasManyThrough('Recipe', 'Reports');
}

问题是,我的可报告对象不仅仅是配方,还可能是注释、图像等。因此,不必使用多个函数,合乎逻辑的方法是通过各种参数解析hasmany之间的关系

理论上看起来是这样的:

public function offenses() {
    return $this->hasManyThrough(['Recipe', 'RecipeComments'], 'Reports');
}
这有可能吗?使用一些未记录的语法?如果没有,是否有任何聪明的解决方法/技巧

可能的解决方案? 一个可接受的解决方案是在我的报告表中添加另一列,并且只添加这样的id吗

ID |用户ID |罪犯ID|可报告类型|可报告ID

这意味着我可以在我的用户模型上建立一个关系,通过该列连接犯罪。但这会被认为是多余的吗?既然我已经通过可报告模型找到了罪犯


模型 多态模型

class Report extends Model {
    public function reportable() {
        return $this->morphTo();
    }

    public function User() {
        return $this->belongsTo('App\User');
    }
}
配方模型

class Recipe extends Model {
    public function user() {
        return $this->belongsTo('App\User');
    }

    public function reports() {
        return $this->morphMany('App\Report', 'reportable');
    }
}
注释模型

class RecipeComment extends Model {   
    public function user() {
        return $this->belongsTo('App\User');
    }

    public function reports() {
        return $this->morphMany('App\Report', 'reportable');
    }
}

使用当前模型,您可以通过以下代码接收用户的所有报告模型:

$recipeCommentReport = RecipeComment::whereHas('reports',function($q){
   return $q->where('user_id','=',Auth::user()->id) 
});

$recipeReport = Recipe::whereHas('reports',function($q){
   return $q->where('user_id','=',Auth::user()->id) 
});

//Get all reports into one
$reports = $recipeReport->merge([$recipeCommentReport]);
这充其量也很混乱,因为:

  • 我们无法对结果进行排序,因为我们使用的是两个单独的db查询
  • 如果你有其他有报告关系的模型,想象一下混乱
  • 最好的解决方案是,正如您在上面所指出的:

    罪犯id
    列添加到您的
    报告
    表中

    它更干净,遵循干燥原理

    典型案例场景

    获取用户的所有配方注释报告

    Report::where('offender_id','=',Auth::check()->id)
    ->where('reportable_type','=','RecipeComment')
    ->get();
    
    按用户的类型统计违法行为

    Report::where('offender_id','=',Auth::check()->id)
    ->grouBy('reportable_type')
    ->select(Db::raw('count(*)'),'reportable_type')
    ->get();
    
    我没有一个解决方案$reports=Report::whereHas('reportable',function($q)use($user_id){$q->where('user_id','=',$user_id);})->get()-它不工作,但它是一个已确认的错误:()
    Report::where('offender_id','=',Auth::check()->id)
    ->grouBy('reportable_type')
    ->select(Db::raw('count(*)'),'reportable_type')
    ->get();