jproux
jproux

Reputation: 265

Laravel 5.6 - One to many relation

I'm learning laravel and php. I have a member object who can have only 1 language option but there is multiple language options to choose from. The member object is my main object. I want to test whether I've setup my relationships correctly but don't see the desired result when testing (I should only see the language selected in the language field, not all the column attributes per member as shown below). So this is the output of my show.blade.php file:

enter image description here

This my show.blade.php file:

@extends('layouts.master')

@section('title', $member->name)

@section('content')
    <h1>Member Details: </h1>
    <p>Name: {{$member->name}}</p>
    <p>Surname: {{$member->surname}}</p>
    <p>ID Nr: {{$member->id_number}}</p>
    <p>Mobile Nr: {{$member->mobile_number}}</p>
    <p>Email: {{$member->email}}</p>
    <p>D.O.B: {{$member->date_of_birth}}</p>
    <p>Language:{{$member->language}}</p>
    <p>Created At: {{$member->created_at}}</p>
    <div class="page-header">
        <a href="{{action('MemberController@edit', $member->id)}}" class="btn btn-info">Edit</a>
    </div>
@endsection

This is my languages table migration file:

public function up()
    {
        Schema::create('languages', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('member_id')->unsigned();
            $table->string('name');
            $table->timestamps();
        });

        Schema::table('languages', function(Blueprint $table) {
            $table->foreign('member_id')->references('id')->on('members');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('languages', function(Blueprint $table) {
            $table->dropForeign('languages_member_id_foreign');
        });
        Schema::dropIfExists('languages');
    }

This is my members table migration file:

public function up()
    {
        Schema::table('members', function (Blueprint $table) {
            $table->string('name');
            $table->string('surname');
            $table->string('id_number')->unique();
            $table->string('mobile_number');
            $table->string('email');
            $table->date('date_of_birth');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('members', function (Blueprint $table) {
            $table->dropColumn('name');
            $table->dropColumn('surname');
            $table->dropColumn('id_number')->unique();
            $table->dropColumn('mobile_number');
            $table->dropColumn('email');
            $table->dropColumn('date_of_birth');
        });
    }

And lastly my member model:

class Member extends Model
{
    public function language() {
        return $this->hasMany('App\Language');//('Language') = name of the Language Model
    }
}

Where am I going wrong?

Upvotes: 1

Views: 531

Answers (1)

Worthwelle
Worthwelle

Reputation: 1271

The reason you're seeing the JSON representation of the object is because that's all you're asking for in your code when you put {{$member->language}}. This represents the entire App\Language object.

Try using {{$member->language->first()->name}} instead to specify that you are asking for only the name variable of the object.

In order to show all languages, you'll need to loop through the $member->language collection using:

@foreach ($member->language as $language)
    {{ $language->name }}
@endforeach

Upvotes: 4

Related Questions