I need to get all the courses that the user owns with a for each loop in my view. I tried to set up a relationship that way: User.php
public function user_courses() { return $this->hasMany(User_Course::class)->orderBy('created_at'); }
Course.php
public function user_course() { return $this->belongsTo(User_Course::class); }
User_Course.php
class User_Course extends Model { protected $guarded = []; public function user() { return $this->belongsTo(User::class); } public function course(){ return $this->hasOne(Course::class,'id'); } }
dashboard.blade.php
@foreach($user->user_courses as $course) {{ dd($course) }} <div class="course-flex-item"> <div class="course-header"> <strong>{{ $course['title'] }}</strong> </div> <div class="course-description"> </div> <div class="continue-button"> <button>Continue</button> </div> <div class="image-container"> <img class="course-image" src="https://www.w3.org/html/logo/downloads/HTML5_Badge_256.png" alt="fasdf"> <div class="progress"> <div class="progress-bar" role="progressbar" aria-valuenow="70" aria-valuemin="0" aria-valuemax="100" style="width: %;background-color: #f64c71;font-family:'Montserrat';font-weight:bold"> <span class="">%</span> </div> </div> </div> </div> @endforeach
This is what the dd($course) returns:
AppUser_Course {#1232 ▼ #guarded: [] #connection: "mysql" #table: "user__courses" #primaryKey: "id" #keyType: "int" +incrementing: true #with: [] #withCount: [] #perPage: 15 +exists: true +wasRecentlyCreated: false #attributes: array:6 [▼ "id" => 6 "user_id" => 2 "course_id" => 2 "progress" => "40" "created_at" => null "updated_at" => null ] #original: array:6 [▶] #changes: [] #casts: [] #classCastCache: [] #dates: [] #dateFormat: null #appends: [] #dispatchesEvents: [] #observables: [] #relations: [] #touches: [] +timestamps: true #hidden: [] #visible: [] #fillable: [] }
Im trying to get each course detail via the course_id. for example if a user with the id of 3 owns a course with the course_id of 2, the database row will look like this: id|user_id|course_id 1 | 3 | 2
If you need more code, tell me in the comments.
users migration:
Schema::create('users', function (Blueprint $table) { $table->id(); $table->string('name'); $table->string('email')->unique(); $table->string('username')->unique(); $table->timestamp('email_verified_at')->nullable(); $table->string('password'); $table->rememberToken(); $table->timestamps(); });
courses migration:
Schema::create('courses', function (Blueprint $table) { $table->id(); $table->text('title')->nullable(); $table->text('description')->nullable(); $table->string('image')->nullable(); $table->string('color')->nullable(); $table->timestamps(); });
user__courses migration:
Schema::create('user__courses', function (Blueprint $table) { $table->id(); $table->unsignedBigInteger('user_id'); $table->unsignedBigInteger('course_id'); $table->text('progress')->nullable(); $table->timestamps(); $table->index('user_id'); });
Advertisement
Answer
You can do the following: inside user model you need to create this function
public function courses() { return $this->belongsToMany(Course::class, 'user_courses','user_id','course_id'); }