Home » Php » How to make selection query for join tables in laravel?

How to make selection query for join tables in laravel?

Posted by: admin October 29, 2018 Leave a comment

Questions:

I have users and staffs tables. I want to display staff details after the login. The staff display will list the of staff with the same section with the user who login. For example, if Adam (section/seksyen=A) login, the staff list displayed are the staffs of section A.

How to make query for that in laravel?

table users:

  public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
        $table->string('section');
        $table->string('type');
    });
}

table staffs:

 public function up()
{
    Schema::create('staffs', function (Blueprint $table) {
        $table->increments('id');
        $table->timestamps();
        $table->string('nama');
        $table->string('emel')->unique();
        $table->string('seksyen');
        $table->string('jawatan');
        $table->string('alamat');
        $table->string('no kakitangan');
    });

I have tried this query but doesn’t work.

 $data['data'] = DB::table('staffs')
                 ->join('users', 'users.section', '=', 'staffs.seksyen')
                 ->where('seksyen','=','Auth(section)')->get();
Answers: