I have three models:
User:
public function projects()
{
return $this->hasMany('StoredFile', 'user_id', 'id')->with('files');
}
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
but this $projects variable need to return to client side by 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
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"
How can I receive the count value in relations??
please help! thanks