【问题标题】:laravel5.1 retrieve not directly related nested attributes using eloquentlaravel5.1 使用 eloquent 检索不直接相关的嵌套属性
【发布时间】:2016-02-09 20:35:13
【问题描述】:

我目前不知道如何以聪明的方式完成这项工作。我想防止编写大量查询。 首先是我的餐桌设计:

users:
|id|username|

tickets:
|id|user_id|

ticket_replies:
id|ticket_id|user_id|

files:
|id|ticket_replie_id|name

我的控制器:

user:
public function tickets()
{
    return $this->hasMany('App\ticket');
}

ticket:
public function ticket_replie() 
{
    return $this->hasMany('App\ticket_replie', 'ticket_id', 'id');
}

ticket_replie:
public function file() 
{
        return $this->hasOne('App\File', 'ticket_replie_id', 'id');
}

每个ticket_replie 只能与一个附件相关(每个ticket_replie 只能一个附件),这就是我使用hasOne 关系的原因。 现在我需要检索给定票证和票证_replie_id 的文件名。 在我的控制器中,我现在使用它:

 $ticket = Auth::user()->tickets()->where('tickets.id', $id)->where('files.ticket_replie_id', $attachment_id)->firstOrFail();

Laravel 生成我这个查询和错误:

select * from `tickets` where `tickets`.`user_id` = 1 and `tickets`.`user_id` is not null and `tickets`.`id` = 43 and `files`.`ticket_replie_id` = 39 limit 1

 Column not found: 1054 Unknown column 'files.ticket_replie_id' in 'where clause

查询必须是这样的:

select * from `tickets`, `files` where `tickets`.`user_id` = 1 and `tickets`.`user_id` is not null and `tickets`.`id` = 43 and `files`.`ticket_replie_id` = 39 limit 1

当我在我的数据库中运行此查询时,它会返回所需的信息。我检索信息的方式可以吗?我的错在哪里,因为目前 Eloquent 生成的查询无法按上述方式工作。如果有更简单的方法,请告诉我。

我知道 eagerload,我试过这个:

$ticket = Auth::user()->tickets()->with(['file'=>function($f) use ($attachment_id) { $f->where('files.ticket_replie_id', $attachment_id); } ])->where('tickets.id', $id)->where('files.ticket_replie_id', $attachment_id)->firstOrFail();`. 

结果:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'files.ticket_replie_id' in 'where clause' (SQL: select * from `tickets` where `tickets`.`user_id` = 1 and `tickets`.`user_id` is not null and `tickets`.`id` = 43 and `files`.`ticket_replie_id` = 39 limit 1) 

故障是因为票证和文件模型之间没有直接的“关系”还是我错了?

【问题讨论】:

    标签: php mysql eloquent laravel-5.1 relationship


    【解决方案1】:

    你必须预先加载你的关系,然后你可以应用 where 条件。看,这里是例子。

    $courses = Courses::with(['quizzes','chapters'=>function($q){
                            $q->where('status','1')->orderBy('orderby','ASC');
                        },'chapters.quizzes','chapters.lessons'=>function($q) use ($lessonid){
                            $q->where('status','1')->orderBy('orderby','ASC');
                        },'chapters.lessons.quizzes'])->where('status','1')->where('id',$courseid)->first();
    

    with([]) 用于加载模型关系,例如:quizzes 是与测验表的关系,chapters=>function($q).... 它也是一个关系,但我正在对章节关系等应用条件

    我希望这对你有意义

    【讨论】:

    • 我知道 eagerload,我试过这个:$ticket = Auth::user()->tickets()->with(['ticket_replie.file'])->where('tickets.id', $id)->where('files.ticket_replie_id', $attachment_id)->firstOrFail(); 没有效果,同样的错误,怎么了?
    • 据我了解你没有正确使用,在你的这一行,->where('files.ticket_replie_id', $attachment_id) 好像file 是关系,你不能这样用,就这样吧。 with(['file'=>function($f) use ($attachment_id) { $f->where('files.ticket_replie_id', $attachment_id); } ]) 和其他人一样......
    • 我使用我希望您会使用的查询更新了第一篇文章...这是一项艰巨的任务:/。
    • 您还需要检查一下,您的关系是否正常运行?因为ticket_replies好像是多对多关系表,把它当作一对多使用
    • 是的,每张票可以有很多票回复。但是每个ticket_replie 只能包含一个附件(不需要)。我的关系哪里不对?
    猜你喜欢
    • 2016-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多