[英]Laravel multiple models relationship return count
I have three models: 我有三种模式:
User: 用户:
public function projects()
{
return $this->hasMany('StoredFile', 'user_id', 'id')->with('files');
}
StoredFile: StoredFile:
public function files()
{
return $this->hasMany('File', 'hash', 'user_hash');
}
and File models which does not have relation method 和没有关联方法的文件模型
The problem: 问题:
I use the following piece of code to get projects and each project stored files: 我使用以下代码来获取项目以及每个项目存储的文件:
$projects = $user->projects;
var_dump(json_encode($projects))
// {"project_name":"test","user_hash":"64650458b6Fhgd68ca57308222","files":[{"id":1,"name":"example.pdf"}]}
I can use count($projects[0]->files) to get files count 我可以使用count($ projects [0]-> files)来获取文件计数
but this $projects variable need to return to client side by Response::json 但是这个$ projects变量需要通过Response :: json返回到客户端
return Response::json($project);
and I can't leak the files to the visitor, I want to modify the StoredFile model to return files count only 而且我无法将文件泄漏给访问者,我想修改StoredFile模型以仅返回文件计数
just like: 就像:
return $this->hasMany('File', 'hash', 'user_hash')->count();
but this will throw me an error: 但这会抛出一个错误:
Call to a member function addEagerConstraints() on a non-object" 在非对象上调用成员函数addEagerConstraints()
How can I receive the count value in relations?? 如何接收关系中的计数值?
please help! 请帮忙! thanks
谢谢
@Warren Moore is almost correct, but I just found a better solution: @沃伦·摩尔(Warren Moore)几乎是正确的,但我刚刚找到了一个更好的解决方案:
protected $appends = array('file_count');
public function getFileCountAttribute()
{
return $this->files->count()
}
this will append a file_count
attribute to each file object 这会将
file_count
属性附加到每个文件对象
Relationship methods must return an object of type Illuminate\\Database\\Eloquent\\Relations\\Relation
. 关系方法必须返回类型为
Illuminate\\Database\\Eloquent\\Relations\\Relation
。 There is nothing wrong with your files()
function, but it is not a relationship, so it can't be used in your projects()
functions. 您的
files()
函数没有任何问题,但这不是关系,因此不能在您的projects()
函数中使用。
The problem piece of code is with('files')
, as Laravel is expecting an object of type Illuminate\\Database\\Eloquent\\Relations\\Relation
to be returned from the files()
function, and instead it receives an int
. 问题的代码段是
with('files')
,因为Laravel期望从files()
函数返回类型为Illuminate\\Database\\Eloquent\\Relations\\Relation
的对象,而它接收一个int
。
You can manually add this value to the model though. 不过,您可以手动将此值添加到模型中。 Try adding this to your
StoredFile
model: 尝试将其添加到
StoredFile
模型中:
public function fileCount() {
return $this->hasMany('File', 'hash', 'user_hash')->count();
}
Change the User
model: 更改
User
模型:
public function projects() {
return $this->hasMany('StoredFile', 'user_id', 'id');
}
Now use: 现在使用:
$projects = $user->projects;
$project = $project->first();
$project->fileCount = $project->fileCount();
var_dump(json_encode($project));
// {..., "fileCount":"1"}
Bare in mind that the number of files is being queried from the database once everytime you ask for it, so this is not efficient if you need to return more than one project. 切记每次需要从数据库中查询一次文件的数量,因此如果您需要返回多个项目,则这样做效率不高。 Given that you were returning a JSON response, I assumed that this would be the case though!
鉴于您正在返回JSON响应,我认为情况确实如此!
Otherwise, why not just do something like this: 否则,为什么不做这样的事情:
public function files() {
return $this->hasMany('File', 'hash', 'user_hash');
}
public function projects() {
return $this->hasMany('StoredFile', 'user_id', 'id')->with('files');
}
Now use: 现在使用:
$projects = $user->projects;
$project = $project->first();
$project->fileCount = $project->files->count();
unset($project->files);
var_dump(json_encode($project));
// {..., "fileCount":"1"}
Something like that should work? 这样的事情应该起作用吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.