1

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
        );
    }
}

EDIT:
I have it working doing 2 seperate database calls. But is this possible in a single call. See code underneath.

public function following(Request $request)
    {
        $follows = Follow::where('me_id', '=', Auth::user()->id)->get()->all();
        $userIds = [];

        foreach($follows as $key => $follow) {
            $userIds[] = $follow->user_id;
        }
        $files = File::where('visible', '=', '1')->whereIn('user_id', $userIds)->orderBy('created_at', 'desc')->paginate(12)->onEachSide(3);
    }

2 Answers 2

1

Your model definition is ok since you can get files using:

Follow::where('me_id', '=', Auth::user()->id)->first()->files;  

Your mistake

The following code returns a laravel collection:

Follow::where('me_id', '=', Auth::user()->id)->get()

The following code returns an array:

Follow::where('me_id', '=', Auth::user()->id)->get()->all()

And files is a dynamic property of Follow model (Not collection nor array). So this is not gonna work as you expect.

Your right solution

You should use eloquent relationships feature:

Follow::with('files')->where('me_id', '=', Auth::user()->id)->get()

This returns a collection of Follow containing all their respective files. like:

[
    'my_id' -> 1,
    'user_id' -> 1,
    'files' -> [
        'user_id' -> 1,
        'path' -> '/foo/bar'
    ]
]
3
  • Okay this seems like the right path! Would it be at all possible to sort all the files on "created_at"? So not sorted at upload date per follow. But 1 sorting for all the files in the record set? Sorry for the really bad explanation. I don't know how to explain this one better. Commented Dec 22, 2018 at 21:03
  • 1
    Assuming the final result is stored in $result as a collection, you can do this: $result->pluck('files')->flatten()->sortByDesc('created_at') Commented Dec 22, 2018 at 21:07
  • Thank you I'll try this. I've added a Edit to the post with a working piece of code that does 2 separate database calls. I'll try to make it 1 with your suggestion! Thank you :) Commented Dec 22, 2018 at 21:09
0

I think you could do this in two steps.

  1. Get the user that the user is following
  2. Get the files from users with ids from the first query
2
  • If I do it that way. How can I sort all the files by their upload date. I need 1 record set of files from the people that I follow so I can order them by created_at Commented Dec 22, 2018 at 20:52
  • Laravel collections have method for sorting - laravel.com/docs/5.7/collections#method-sort Commented Dec 22, 2018 at 20:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.