简体   繁体   English

Laravel 5.6-一对多关系

[英]Laravel 5.6 - One to many relation

I'm learning laravel and php. 我正在学习laravel和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). 我想测试我是否正确设置了我的关系,但是在测试时看不到期望的结果(我应该只看到在language字段中选择的语言,而不是每个成员的所有列属性,如下所示)。 So this is the output of my show.blade.php file: 这是我的show.blade.php文件的输出:

在此处输入图片说明

This my show.blade.php file: 这是我的show.blade.php文件:

@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? 我要去哪里错了?

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}} . 您看到对象的JSON表示形式的原因是,这就是您在放入{{$member->language}}时在代码中要的全部内容。 This represents the entire App\\Language object. 这代表了整个App\\Language对象。

Try using {{$member->language->first()->name}} instead to specify that you are asking for only the name variable of the object. 尝试使用{{$member->language->first()->name}}代替指定您仅要求对象的name变量。

In order to show all languages, you'll need to loop through the $member->language collection using: 为了显示所有语言,您需要使用以下命令遍历$member->language集合:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM