samedi 22 décembre 2018

Trying to get a record set containing all the files of the people you follow

I'm trying to make a small function to a website that I'm creating that let's you follow other users and then your dashboard fills up with the files they upload in that order of upload date. (You can compare it to youtube. You subscribe to a channel and on the subscriptions page you see every video in order by date of people you follow).

This is my model structure (It is simplified to only show the needed values for this to work).

Follow

  • my_id (This is your id)
  • user_id (This is the id of the user that you're following)

User

  • id (The user id)

Files

  • user_id (The id of the user who uploaded the file)
  • path (file path)

I have tried many things now to get the data:
Follow::where('me_id', '=', Auth::user()->id)->get()->all()->files;
Follow::where('me_id', '=', Auth::user()->id)->get()->files;
The only thing that seems to work is (But this is only for 1 follow):
Follow::where('me_id', '=', Auth::user()->id)->first()->files;
It would be amazing if someone could see what I'm doing wrong.
In my Follow.php model I have it connected like this.

class Follow extends Model
{
protected $fillable = ['user_id', 'me_id'];

public function me() {
    return $this->belongsTo(User::class, 'id', 'me_id');
}

public function user() {
    return $this->belongsTo(User::class, 'id', 'user_id');
}

public function files()
{
    return $this->hasManyThrough(
        'App\File', //The model I'm trying to reach
        'App\User', //The model I need to go through to reach the files
        'id', //This is where I'm going wrong I think
        'user_id', //Same here
        'user_id', //Same here
        'id' //Same here
    );
}
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire