Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

artisticre's avatar

Not sure how to select based on input field name

I have three tables and migrations are listed below. Everything is working fine. It is listing the applicant name and details by a join I have below. My question is I have a view button that I want to click and select from the tables by applicant name. How do I do that?

Select statement for listing the applicant names

public function getSponsorDashboard() {
        $sponsor = DB::table('users')
        ->join('sponsor_application', 'sponsor_application.user_id', 'users.id')
        ->leftJoin('profiles', 'profiles.user_id', 'users.id')
    
       ->where('profiles.user_id', '=' ,Auth::user()->id)
       ->get();
  
       return view('sponsor.dashboard', compact('sponsor'))->with('data', Auth::user()->profile);
     }

Users Migration

public function up()
    {
        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->timestamps();
        });
    }

Profile Migration

public function up()
    {
        Schema::create('profiles', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id');
            $table->string('address')->nullable;
            $table->string('apt')->nullable;
            $table->string('city')->nullable;
            $table->string('state')->nullable;
            $table->string('zipcode')->nullable;
            $table->string('homephone')->nullable;
            $table->string('cellphone')->nullable;
            $table->boolean('baptized')->nullable;
            $table->string('lastweekendserved')->nullable;
            $table->timestamps();
        });
    }

Sponsor Application Migration

public function up()
    {
        Schema::create('sponsor_application', function (Blueprint $table) {
            $table->id();
            $table->integer('user_id');
            $table->integer('profile_id');
            $table->string('besttime');
            $table->string('applicantname');
            $table->boolean('applicantbaptized');
            $table->boolean('spousediscuss');
            $table->boolean('bothspouseattend');
            $table->string('ifnospouseattend')->nullable;
            $table->text('whygoodcandidate');
            $table->text('applicantattitude');
            $table->text('leadershipexpectations');
            $table->text('sponsorsupport');
            $table->text('otherinfo');
            $table->string('sponsorsignature');

            $table->date('datesigned');
          
            $table->timestamps();
        });
    }
0 likes
1 reply
bajjouayoub's avatar
Level 5

use Laravel Eloquent Relationships in user Model:

public function profile() { return $this->hasOne('App\Profile', 'user_id'); }

public function sponsor() { return $this->hasOne('App\your sponsor application model', 'user_id'); }

the function in your controller :

public function getSponsorDashboard() { $sponsor = User::with(['profile', 'sponsor'])->whereHas('profile', function ($query) { $query->where('user_id', Auth::id()'); })->get();

return view('sponsor.dashboard', compact('sponsor'));

}

try this !

Please or to participate in this conversation.