Thursday, March 16, 2017

Laravel: Access relationship table

I need to return a list of missions and the current user progress for each mission. For this, I created the following eloquent expression:

return DailyMission::with('userProgress')->get();

Using the following relationship in my DailyMission Model (daily_missions table):

public function userProgress() {
        return $this->hasOne(UserDailyMission::class,'mission_id');
    }

The problem is that it's not user specified, which means I get the first progress of random user in my database.

I tried to change the expression into this:

return DailyMission::with('userProgress')
        ->where('user_daily_missions.user_id',$user->id)
        ->get();

but I get the following error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'user_daily_missions.user_id' in 'where clause

The user progress is stored at the user_daily_missions table using the following structure:

id | user_id | mission_id | current_progress

Any ideas how to output the progress of the current user instead of a random user? Is there anything wrong with the structure?



via TheUnreal

Advertisement