Laravel API: How to get data from foreign key from different table

  laravel

I currently want to make an API that will get all the lessons by its module_id. These are currently my codes:

Module Migration Table:

<?php
use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;

class CreateModulesTable extends Migration {
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up() {
        Schema::create('modules', function (Blueprint $table) {
            $table->id();
            $table->string('module_name');            
            $table->bigInteger('created_by')->unsigned()->nullable(); 
            $table->bigInteger('updated_by')->unsigned()->nullable(); 

            $table->timestamps();
        });

        Schema::table('modules', function(Blueprint $table) {
            $table->foreign('created_by')->references('id')->on('admins');
            $table->foreign('updated_by')->references('id')->on('admins');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down() {
        Schema::dropIfExists('modules');
    }
}

Lesson Migration Table:

<?php
use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;

class CreateLessonTable extends Migration {
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up() {
        Schema::create('lesson', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->bigInteger('module_id')->unsigned();
            $table->longText('content');
            $table->bigInteger('created_by')->unsigned()->nullable(); 
            $table->bigInteger('updated_by')->unsigned()->nullable(); 
            $table->string('enabled')->nullable(); 
            $table->string('position')->nullable();
            $table->timestamps();
        });

        Schema::table('lesson', function(Blueprint $table) {
            $table->foreign('module_id')->references('id')->on('modules');
            $table->foreign('created_by')->references('id')->on('admins');
            $table->foreign('updated_by')->references('id')->on('admins');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down() {
        Schema::dropIfExists('lesson');
    }
}

And this is the GetLessonByModule controller of that API I want to make (getting all the lessons by its module_id):

<?php
namespace AppHttpControllers;
use AppModelsLesson;
use AppModelsModules;
use IlluminateHttpRequest;

class GetLessonByModuleController extends Controller {
    public function index() {
        $lessons = Lesson::all();
        return response($lessons,200);
    }
}

The module_id and the ID in the modules table are currently connected as seen in the migration tables. I just don’t know how to compose it in my Controller. I also don’t know if I should make a new model. Any kind of help/suggestion is deeply appreciated. Thanks in advance.

Source: Laravel

Leave a Reply