Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Laravel 9 – Display Name Instead of ID in View

SOLVED

As the title says, I’m struggling to get the field name to show instead of the ID.
I’m new to Laravel and PHP and only started using Laravel 2 months ago. I have looked through the answered threads but still not coming right.

I have 2 tables members (club members) and teams (soccer teams).
Obviously a member can only belong to one team and a team can have many members.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

My DB tables:
Members Table:
Teams Table

My Migrations:

Members Migration

Schema::create('members', function (Blueprint $table) {
        $table->id();
        $table->string('member_image')->nullable();
        $table->string('member_first_name');
        $table->string('member_last_name');
        $table->string('member_type')->nullable();
        $table->unsignedBigInteger('team_id');
        $table->foreign('team_id')->references('id')->on('teams');
        $table->timestamps();

Table Migration

Schema::create('teams', function (Blueprint $table) {
        $table->id();
        $table->string('team_name');
        $table->string('team_coach')->nullable();
        $table->timestamps();

My Models

Member: App\Models\Backend\Member

protected $table = 'members';

protected $fillable = [
    'member_image',
    'member_first_name',
    'member_last_name',
    'member_type',
    'team_id',
    'member_safa',
    'member_email',
    'member_phone',
    'member_id_number',
    'member_subs'
];

public function team(){
    return $this->belongsTo(Team::class, 'team_id', 'id');
}

Team: App\Models\Backend\Member

protected $table = 'teams';

protected $fillable = [
    'team_name',
    'team_coach'
];

public function member(){
    return $this->hasMany(Member::class );
}

My Controllers:

Member:

use App\Models\Backend\Team;
use App\Models\Backend\Member;

class MembersController extends Controller
    {
        public function index()
        {
            $members = Member::orderBy('member_last_name')->get();
            return view('backend.members.index')
                ->with('members', $members);
    
        }

My View:

  @foreach ($members as $key => $member)
    <tr>
      ...
      <td>{{ $member->team_id }}</td>
  @endforeach

And finally how it displays when viewing page
display on view

I’m sure it is something simple I haven’t done.

*Edit: Solved – In my view I had to change

<td>{{ $member->team_id }}</td>

to

<td>{{ $member->team->team_name }}</td>

>Solution :

Please check this: $member->team->team_name if eager loading is properly set.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading