Juan Rincón
Juan Rincón

Reputation: 327

How can I query to get results with belongsToMany in Laravel

UPDATE

I could solve the problem. Posting the update.

First of all, thank you for taking your time answering this threat and helping me.

I'm creating a courses website with Laravel 10, and I'm trying to get a query for a dashboard page. This page should show the courses that the people bought. In order to start or continuing the course or courses.

So, I have my courses table and contains this.

Schema::create('courses', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->string('subtitle');
            $table->text('description');
            $table->enum('status',[Course::BORRADOR,Course::REVISION,Course::PUBLICADO])->default(Course::BORRADOR);
            $table->string('slug');
            $table->unsignedBigInteger('user_id');
            $table->unsignedBigInteger('level_id')->nullable();
            $table->unsignedBigInteger('category_id')->nullable();
            $table->unsignedBigInteger('price_id')->nullable();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->foreign('level_id')->references('id')->on('levels')->onDelete('set null');
            $table->foreign('category_id')->references('id')->on('categories')->onDelete('set null');
            $table->foreign('price_id')->references('id')->on('prices')->onDelete('set null');
            $table->timestamps();
        });

And ofcourse I have my users table.

public function up(): void
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->foreignId('current_team_id')->nullable();
            $table->string('profile_photo_path', 2048)->nullable();
            $table->timestamps();
        });
    }

To access through those tables I added an intermediate table. Called course_user_table. That table contains the user_id and course_id, like so.

public function up(): void
    {
        Schema::create('course_user', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('course_id');
            $table->unsignedBigInteger('user_id');
            $table->foreign('course_id')->references('id')->on('courses')->onDelete('cascade');
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->timestamps();
        });
    }

Now, I have my User.php model and Course.php Model. I have this into them.

User.php Model:

public function courses_enrolled(){
        return $this->belongsToMany('App\Models\Course');
    }

And my Course.php Model:

public function students(){
        return $this->belongsToMany('App\Models\User');
    }

As I said, what I need is to obtain the purchased courses into a dashboard.blade.php so the user who bought a course/courses, can find it/them right there and start or continuing studying.

I will appreciate your help so much. I tried in so many ways, some with errors and some others without errors and without information back.

Updated code:

I moved all the controller's logic into a Livewire component. This is all i needed:

class UserDashboard extends Component
{
    public $user;

    public function mount(User $user){
        $this->user = $user;
    }

    public function render()
    {
        $users = $this->user->courses_enrolled()->paginate(8);

        return view('livewire.user-dashboard', compact('users'));
    }
}

Now everything's working fine =)

Upvotes: 2

Views: 48

Answers (0)

Related Questions