Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

You can't use

return $this->hasMany('File', 'hash', 'user_hash')->count();

as a relationship. Do you want to return another attribute with the project object which contains the number of files attached to that project?

If so you could do something like below in the projects model:

protected $appends = array('file_count');

public function getFileCountAttribute()
{
    return $this->files->count()
}

This should include a 'file_count' attribute within the json with the count of how many files are attached to each project.

Last updated 2 years ago.
0

DanielOReilly said:

You can't use

return $this->hasMany('File', 'hash', 'user_hash')->count();

as a relationship. Do you want to return another attribute with the project object which contains the number of files attached to that project?

If so you could do something like below in the projects model:

protected $appends = array('file_count');

public function getFileCountAttribute()
{
   return $this->files->count()
}

This should include a 'file_count' attribute within the json with the count of how many files are attached to each project.

Wow, this works perfectly for me, but one question: can I only return the file_count without files?

Last updated 2 years ago.
0

You can return the list of files for a project by doing the following.

$projects = Project::with('files')->where('id', $projectId)->first();

This should return a project object with all attached file objects and the file_count attribute.

Last updated 2 years ago.
0

DanielOReilly said:

You can return the list of files for a project by doing the following.

$projects = Project::with('files')->where('id', $projectId)->first();

This should return a project object with all attached file objects and the file_count attribute.

No, the json response is now:

{"project_name":"test","user_hash":"64650458b6Fhgd68ca57308222","file_count":1,"files":[{"id":1,"name":"example.pdf"}]}

I want file_count only

{"project_name":"test","user_hash":"64650458b6Fhgd68ca57308222","file_count":1}
Last updated 2 years ago.
0

Then use the getFileCountAttribute() as described above.

Last updated 2 years ago.
0
#Add this code in your Model. 
protected $hidden = ['files']; 

Note: files refers to your relationship's method name.

nasaorc said:

DanielOReilly said:

You can return the list of files for a project by doing the following.

$projects = Project::with('files')->where('id', $projectId)->first();

This should return a project object with all attached file objects and the file_count attribute.

No, the json response is now:

{"project_name":"test","user_hash":"64650458b6Fhgd68ca57308222","file_count":1,"files":[{"id":1,"name":"example.pdf"}]}

I want file_count only

{"project_name":"test","user_hash":"64650458b6Fhgd68ca57308222","file_count":1}
Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

nasaorc nasaorc Joined 19 Jun 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.