Restrict Only Lessons from a Course via Laravel Controller

Asked

Viewed 25 times

0

Well, even I don’t know how to ask properly why it’s been less than a week since I started studying Laravel. Please, I ask for everyone’s understanding!

My case is the following: I have a table of Courses and a Table of Classes, where the relationship is 1:N, A course can have many Classes but the Class can only belong to one course. Follow me on my Migrations:

Table Courses:

Schema::create('cursos', function (Blueprint $table) {
    $table->bigIncrements('id')->unsigned();;
    $table->bigInteger('categoria_id')->unsigned();
    $table->bigInteger('dono_id')->unsigned();
    $table->string('nome');
    $table->string('descricao');
    $table->float('valor');
    $table->string('imagem');
    $table->timestamps();
});

Table Classes:

Schema::create('aulas', function (Blueprint $table) {
   $table->bigIncrements('id');
   $table->bigInteger('curso_id')->unsigned();
   $table->string('nome');
   $table->text('descricao');
   $table->time('duracao');
   $table->string('link');
   $table->timestamps();
});

Foreignkey:

Schema::table('aulas', function (Blueprint $table) {
    $table->foreign('curso_id')->references('id')->on('cursos');
});

I also did the relationships via Model.

Model Curso:

[...]
    public function aulas()
    {
        return $this->hasMany('app\Aula', 'curso_id', 'id');
    }

Model Aula:

[...]
public function curso()
{
    return $this->belongsTo('app\Curso', 'curso_id', 'id');
}

I wanted to do a route that would be something like ".../user/course/Cursoid/aula/Aulaid" where I pass both the course id and the class id. Now coming to the part where I get lost, how would my Controller functions or functions look like in a way that when going to the next class only the class id changes, and that if for some remote reason the user manually goes to the link and places an id of a class that does not belong to the course in which it is currently active, the model returns the last id validates?

In case this route is being very "complicated", what would be a route that would reach my idea?

I tried it this way:

Route:

Route::prefix('usuario')->namespace('Usuario')->group(function () {
        [...]
        Route::prefix('curso')->group(function(){
            Route::get('/{cursoid}/aula/{aulaid}', 'CursosController@aulas')->name('curso.aula');
        });
    });

Controller:

public function aulas($cursoid,$aulaid)
    {
        $curso = Curso::findOrFail($cursoid);
        $aula = Aula::findOrFail($aulaid);
        $aulas = $curso->aulas;
        return view('usuario.aula',compact('curso','aulas','aula'));
    }

And although I arrived on the desired route it simply does not limit to only active course classes. Missing anything, just ask me to add on an Dit or doubt shot in the comments!

1 answer

0


Is that you do not make the check if the class you seek is of course.

// pega o curso já com a relação das aulas dele
$curso = Curso::with(['aulas' => function($query) use ($aulaid) {
    $query->where('id', $aulaid);
}])->findOrFail($cursoid);

So check if there’s a class and take it.

$class = null;

if($curso->aulas->isNotEmpty())
{
    // faça isso porque 1 N retorna uma colection
    // Se fosse pegar um curso atravez de uma aula retornaria um objeto Curso
    $aula = $curso->aulas->first(); 

}

=========================================================

Give a read on relatioships and Collections... Da para fazer de vários meios.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.