从视图中获取外键数据(Laravel 5.6)

时间:2018-03-18 05:37:33

标签: php laravel laravel-5 eloquent laravel-eloquent

我已经搜索了答案,但我找不到任何可以解决问题的答案。

我有2个表:phonesphone_types。第一个具有与phone_types主键相关联的外键。我想通过电话对象显示phone_type的名称。像$phone->phone_type->name这样的东西。

我的代码会生成此错误:Trying to get property of non-object (View: /home/ablono/dev/MisServices/resources/views/painel/phones/home.blade.php)

我的代码列在下面。

phone_types miration table:     

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePhoneTypesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('phone_types', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name', 20);
            $table->string('description', 30);
            $table->timestamps();
        });
    }
/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::dropIfExists('phone_types');
}

} ```

手机迁移表:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePhonesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('phones', function (Blueprint $table) {
            $table->increments('id');
            $table->string('ddd')->default('13');
            $table->string('number', 20);
            $table->integer('phone_type_id')->unsigned();
            $table->foreign('phone_type_id')
                ->references('id')
                ->on('phone_types')
                ->onDelete('cascade');
            $table->timestamps();
        });

        Schema::create('phone_user', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('phone_id')->unsigned();
            $table->integer('user_id')->unsigned();

            $table->foreign('phone_id')
                ->references('id')
                ->on('phones')
                ->onDelete('cascade');

            $table->foreign('user_id')
                ->references('id')
                ->on('users')
                ->onDelete('cascade');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('phone_user');
        Schema::dropIfExists('phones');
    }
}

我没有在PhoneType模型上编写任何代码,但这里是代码:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class PhoneType extends Model
{
    //
}

手机型号:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Phone extends Model
{
    public function users()
    {
        return $this->belongsToMany(\App\User::class);
    }

    public function phoneType()
    {
        return $this->hasMany(\App\PhoneType::class);
    }
}

发送该视频的方法:

public function index()
    {
        $phones = Phone::all();

        return view('painel.phones.home', compact('phones'));
    }

列出数据的部分视图:

<table class="table">
            <thead>
                <th>ID</th>
                <th>DDD</th>
                <th>Número</th>
                <th>Tipo de Telefone</th>
                <th width="150px">Ações</th>
            </thead>
            <tbody>
                @foreach($phones as $phone)
                    <tr>
                        <div class="loading"></div>
                        <td>{{ $phone->id }}</td>
                        <td>{{ $phone->ddd }}</td>
                        <td>{{ $phone->number }}</td>
                        <td>{{ $phone->phone_type_id->name }}</td>

                        <td>
                            <a href="/admin/phone/users/{{ $phone->id }}"><i class="fa fa-lg fa-phone" title="Visualizar usuários que usam {{ $phone->number }}"></i></a>
                            <a href="/admin/phone/edit/{{ $phone->id }}"><i class="fa fa-lg fa-pencil" title="Editar {{ $phone->number }}"></i></a>
                            <a href="/admin/phone/delete/{{ $phone->id }}"><i class="fa fa-lg fa-times" title="Excluir {{ $phone->number }}"></i></a>
                            <a href="/admin/phone/view/{{ $phone->id }}"><i class="fa fa-lg fa-eye" title="Visualizar {{ $phone->number }}"></i></a>
                        </td>
                    </tr>
                @endforeach
            </tbody>
        </table>

2 个答案:

答案 0 :(得分:1)

将您的关系更改为belongsTo()

public function phoneType()
{
    return $this->belongsTo(\App\PhoneType::class, 'phone_type_id');
}

并在view

 <td>{{ $phone->phone_type->name }}</td>

答案 1 :(得分:0)

请检查您雄辩的模型关系。访问雄辩的财产就像。 $ main-model-&gt; $ sub-model(您想要访问的模型的方法名称) - &gt; property 注意:当您访问方法时,请将方法名称转换为蛇案例