2017-08-01 44 views
0

我已经花了几个小时试图弄清楚这一点,我希望有人能帮助..问题与1-多Laravel雄辩模型查询

我只是想输出文章的标题与文章的平均等级为每个。我已经建立了数据库表文章和评级来保存数据,在评级表中我有rating_id,primary和article_id作为链接到Articles表[主键]的外键。

我已创建为每个表的模型和文章和评级模型之间的一对多关系:

namespace App\Models; 

use Illuminate\Database\Eloquent\Model; 

class Article extends Model 
{ 
    protected $table = 'articles'; 
    protected $primaryKey = 'article_id'; 

    /** 
    * Define a 1-many relationship in eloquent 
    */ 
    public function ratings() 
    { 

    return $this->hasMany(Rating::class); 

    } 
} 

我还设置(等级和文章模型之间的反比关系是实际需要?): - 应该有2条记录返回,这是你的第2个评级

namespace App\Models; 

use Illuminate\Database\Eloquent\Model; 

class Rating extends Model 
{ 
    protected $table = 'rating'; 
    protected $primaryKey = 'rating_id'; 

    /** 
    * Define a 1-many inverse relationship in eloquent 
    */ 
    public function articles() 
    { 

    return $this->belongsTo(Article::class); 

    } 
} 

在我的控制,我通过“收视率”属性,其中的article_id = 2调用收视率的集合。代码示例:

public function ShowRating() { 

$ratings = Article::find(2)->ratings; 

var_dump($ratings); 

    } 

我收到SQL错误,是我的模型关系不正确吗?代码示例:

> QueryException in Connection.php line 647: SQLSTATE[42S22]: Column not 
> found: 1054 Unknown column 'rating.article_article_id' in 'where 
> clause' (SQL: select * from `rating` where 
> `rating`.`article_article_id` = 2 and `rating`.`article_article_id` is 
> not null) 

一旦我管理了这个阶段,我可以使用avg方法对评分进行平均。

提前道歉 - 我相信这是愚蠢的,但我无法弄清楚。非常感谢..

+1

我是很新的larvel。我很困惑这篇article_article_id从哪里来?你可以尝试在hasMany和belongsTo函数的第二个参数中添加外键。返回$ this-> hasMany(Rating :: class,'rating_id');并返回$ this-> belongsTo(Article :: class,'rating_id'); (它应该是公共领域) – Farsay

+1

在您的Article模型中尝试'返回$ this-> hasMany(Rating :: class,'article_id')'。 –

+0

谢谢Fairsay&Taylor Foster - HasMany方法缺少指定评级表中的外键的参数,在这种情况下,该参数为'article_id'。然而,仍然不完全在那里..使用下面的急切加载我可以得到文章标题和评分数据,但是当我尝试访问rating_score或任何其他特定的字段时,我得到'Property [rating_id]在此收集实例上不存在“。错误。语法错误? $ articles = Article :: all(); ($ articles as $ article){ echo $ article-> ratings-> rating_score; echo $ article-> headline; }' – pfeatherstone

回答

0

我认为你的迁移/数据库结构是错误的。 - >Unknown column 'rating.article_article_id' 如果您发布代码,我可以帮助您。

+0

嗨Benni - 作为Fairsay&泰勒福斯特指出,我没有指定任何参数。很高兴分享更多的代码,你需要看什么.. – pfeatherstone

0

我想发布我的工作代码,再次感谢您的帮助。

第一个问题 - 没有在我的模型中定义hasMany和belongsTo方法定义我的文章和评分表之间关系的参数 - 专门定义表的外键/主键; Laravel信息:https://laravel.com/docs/5.4/eloquent-relationships

文章型号:

namespace App\Models; 

use Illuminate\Database\Eloquent\Model; 

class Article extends Model 
{ 
    protected $table = 'articles'; 
    protected $primaryKey = 'article_id'; 

    /** 
    * Define a 1-many relationship in eloquent 
    */ 
    public function ratings() 
    { 

    //NOTE ignore IDE error 
    return $this->hasMany(Rating::class, 'article_id'); 

    } 
} 

评级模型:

namespace App\Models; 

use Illuminate\Database\Eloquent\Model; 

class Rating extends Model 
{ 
    protected $table = 'rating'; 
    protected $primaryKey = 'rating_id'; 

    /** 
    * Define a 1-many inverse relationship in eloquent 
    */ 
    public function articles() 
    { 

    return $this->belongsTo(Article::class, 'article_id'); 

    } 
} 

第二个问题是我使用预先加载到输出的一对多关系,而不是反比关系(即使用评级对象)。我也用在集合类的方法来平均适用于我的收视率和输出在我的控制器都按如下:

public function GetAllRatings() { 

    $ratings = Rating::all(); 

    $ratings->average = $ratings->avg('rating_score'); 

    foreach ($ratings as $rating) { 
    echo $rating->articles->headline; 
    echo $ratings->average; 
}  
    } 

我希望这可以帮助别人花不到2天的代码15行,仍然我必须学习!任何错误(即使认为似乎工作正常),请让我知道,我会修改这个答案。再次感谢所有。