paduraruionutandrei's avatar

Code review

Hello everyone, this is a question is not for solving problems purpose, but to see if I am on the right way.I'm building an API for my first web app(I am a beginner) and try to write clean code(always find ways to improve my code, but doing my best).I have 4 models and their migrations for now, Company, Project, Bucket, Task.Everything in my app will be tied up to company, but I cannot figure it out If I do the right thing here.So here is my idea.I tied up Projects to company(one to many relationship), and then tied up Bucket to Project(one to many relationship again) and then Tasks to Buckets(again, one to many).

/**
     * Company projects
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function projects()
    {
        return $this->hasMany('App\Models\Project', 'company_id', 'id');
    }

    /**
     * project->buckets
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function buckets()
    {
        return $this->hasMany('App\Models\Bucket', 'project_id', 'id');
    }

    /**
     * bucket->tasks
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function tasks()
    {
        return $this->hasMany('App\Models\Task', 'bucket_id', 'id');
    }

Now my doubt is: When I add a middleware(like I said everything will be tied up to Company), for authorization purpose, another user will be able to access for example (/projects/1/bucket/1/tasks/1).And if it is a good way to do this, I mean I made something like a "stair".I tied up projects to companies, and then buckets to projects, and then tasks to buckets.

Sorry, If you do not understand my question.I am still a beginner and try to improve myself everyday!Thanks in advice!

0 likes
3 replies
nolros's avatar

@paduraruionutandrei have you looked at Laravel Passport for APIs, unsure as to your requirements but purpose-built for APIs e.g. tokens, etc. https://laravel.com/docs/6.x/passport I assume you mean an API that can be consumed by other applications? Relations are data structures and have little to do with auth or access. If you are looking to share data with functionality outside of your own app then API like passport would work.

paduraruionutandrei's avatar

I implemented my own JWT Token authentication and everything, I just need to know If its a good practice to make relationships like I did.

nolros's avatar

@paduraruionutandrei got it. Here is the structure. Note: this would change if there is multi relationship i.e. if the same tasks belongs to multiple buckets or a bucket can belong to multiple projects


 class Company extends Model
    {
        /**
         * Company can have many projects
         */
        public function projects()
        {
            return $this->belongsToMany(Project::class, 'company_project');
        }

    }
    class Project extends Model
    {
        /**
         * assumes company_id in project table
         * project belongs to company
         */
        public function company()
        {
            return $this->belongsTo(Company::class);
        }

        /**
         * project has many tasks
         */
        public function buckets()
        {
            return $this->hasMany(Bucket::class);
        }

    }

    class Bucket extends Model
    {
        /**
         * assumes project_id in bucket table
         * task belongs to project
         */
        public function project()
        {
            return $this->belongsTo(Project::class);
        }

        /**
         * bucket  belongs to project
         */
        public function tasks()
        {
            return $this->hasMany(Bucket::class);
        }

    }

    class Task extends Model
    {
        /**
         * assumes bucket_id in tasks table
         * task belongs to project
         */
        public function bucket()
        {
            return $this->belongsTo(Bucket::class);
        }
    }


Please or to participate in this conversation.