vendredi 1 mars 2019

User has many items with folders

I am trying to make an Eloquent query where I can get the Auths users expenses of a particual folder.

Example, get all Auth Users expenses that are linked to folder of id 2.

I have 3 tables,

    expenses
    -----------
    id


    folders
    -----------
    id


    folder_expense
    -----------
    id
    expense_id
    folder_id

Currently I have these as my models.

User
---------------
public function expenses() {
        return $this->hasMany('App\Expense', 'user_id', 'id');
    }

    public function folders() {
        return $this->hasMany('App\Folder', 'user_id', 'id');
    }


Expense
---------------
public function user() {
        return $this->belongsTo('App/User', 'user_id');
    }

    public function folders() {
        return $this->belongsToMany('App/Folder', 'folder_expense', 'folder_id', 'id');
    }


Folder
---------------
public function user() {
        return $this->belongsTo('App/User', 'user_id');
    }

    public function expenses() {
        return $this->belongsToMany('App/Expense', 'folder_expense', 'expense_id', 'id');
    }

I've made that pivot table thinking I could do something like this

Auth::user()->expenses()->folders()->where('folder_id', 2)->get()



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire