多个hasOne关系出错,试图获取非对象的属性

时间:2016-10-15 01:09:07

标签: php laravel eloquent blade

这是我的迁移:

<?php    

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

class CreateUserHabitsTable extends Migration
{
    public function up()
    {
        Schema::create('user_habits', function($table) 
        {
            $table->increments('id');
            $table->integer('user_id')->unsigned();
            $table->integer('smo')->unsigned()->default(1);
            $table->integer('drink')->unsigned()->default(1);
            $table->integer('gym')->unsigned()->default(1);
            $table->integer('sch')->unsigned()->default(1);//School
            $table->integer('work')->unsigned()->default(1);
            $table->integer('re')->unsigned()->default(1);//Leer
            $table->integer('mo')->unsigned()->default(1);//Movies
            $table->integer('con')->unsigned()->default(1);//Concerts
            $table->timestamps();    

            $table->foreign('user_id')->references('id')->on('users');
            $table->foreign('smo')->references('id')->on('frequences');
            $table->foreign('drink')->references('id')->on('frequences');
            $table->foreign('gym')->references('id')->on('frequences');
            $table->foreign('sch')->references('id')->on('frequences');
            $table->foreign('work')->references('id')->on('frequences');
            $table->foreign('re')->references('id')->on('frequences');
            $table->foreign('mo')->references('id')->on('frequences');
            $table->foreign('con')->references('id')->on('frequences');
            $table->softDeletes();
        });
    }    

    public function down()
    {
        Schema::drop('user_habits');
    }
}

这是我的模型UserHabit     

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;    

class UserHabit extends Model
{
    use SoftDeletes;    

    protected $table = 'user_habits';
    protected $fillable = ['user_id','smo','drink','gym','sch','work','drugs','re','mo','con'];    

    public function user()
    {
        return $this->belongsTo('App\User');
    }    

    public function smoke()
    {
        return $this->hasOne('App\Frequence','id', 'smo');
    }    

    public function drink()
    {
        return $this->hasOne('App\Frequence','id', 'drink');
    }    

    public function gym()
    {
        return $this->hasOne('App\Frequence','id', 'gym');
    }    

    public function school()
    {
        return $this->hasOne('App\Frequence','id', 'sch');
    }    

    public function work()
    {
        return $this->hasOne('App\Frequence','id', 'work');
    }    

    public function drugs()
    {
        return $this->hasOne('App\Frequence','id', 'drugs');
    }    

    public function read()
    {
        return $this->hasOne('App\Frequence','id', 're');
    }    

    public function movies()
    {
        return $this->hasOne('App\Frequence','id', 'mo');
    }    

    public function concerts()
    {
        return $this->hasOne('App\Frequence','id', 'con');
    }
}

但是当我尝试在视图中打印时,不同的关系只有一些作品,例如饮料不起作用

<div class="row">
                            <div class="col-md-4">
                                <p>{{ $user->habit->smoke->name }}</p>
                            </div>
                            <div class="col-md-4">
                                <p>{{ $user->habit->drink->name }}</p>
                            </div>
                            <div class="col-md-4">
                                <p>Se ejercita: </p>
                            </div>
                            <div class="col-md-4">
                                <p>{{ $user->habit->school->name }}</p>
                            </div>
                            <div class="col-md-4">
                                <p>Trabaja: </p>
                            </div>
                            <div class="col-md-4">
                                <p>{{ $user->habit->read->name }}</p>
                            </div>
                            <div class="col-md-4">
                                <p>{{ $user->habit->movies->name }}</p>
                            </div>
                            <div class="col-md-4">
                                <p>{{ $user->habit->concerts->name }}</p>
                            </div>
                        </div>

但拉拉维尔说,关系健身和工作不起作用 试图获得非对象的属性

0 个答案:

没有答案