Laravel5 关联查询问题

WBOY
WBOYオリジナル
2016-06-06 20:30:091192ブラウズ

关联查询一致报错,不得已来SF麻烦各位大神帮我解答。

报错内容

<code>ErrorException in 777fb9d9b961f2b2453d93297ba8a847 line 15:
Trying to get property of non-object (View: /home/millyn/www/x.x/resources/views/NewHome.blade.php)
</code>

我的对应文件如下

Avatar.php

<code><?php namespace App;

use Illuminate\Database\Eloquent\Model;


class Avatar extends Model {


    public function article()
    {
        return $this -> hasMany('App\Article');
    }
}
</code>

Article.php

<code><?php namespace App;

use Illuminate\Database\Eloquent\Model;

class Article extends Model {

    //
    public function avatar()
    {
        return $this -> belongsTo('App\Avatar');
    }

}
</code>

Controller.php

<code>    public function index()
    {
        $articles = Article::with('avatar')->latest()->paginate(8);
        return view('NewHome', compact('articles'));
    }
</code>

NewHome.blade.php

<code>    <div class="container">
        <div class="row">
            @foreach ($articles as $page)
            <div class="col-md-3">
                <div class="panel panel-default">
                    <div class="panel-heading" style="white-space:nowrap;overflow:hidden;text-overflow:clip;">
                        <a title="{{ $page->title }}" href="%7B%7B%20URL('news/'.%24page->id)%20%7D%7D"> <h4>{{ $page->title }}</h4></a>
                    </div>
                    <div class="panel-body">
                        <a title="{{ $page->title }}" href="%7B%7B%20URL('news/'.%24page->id)%20%7D%7D"><img src="%7B%7B%24page->avatars_id->url%7D%7D" alt=""    style="max-width:90%"></a>
                    </div>
                </div>
            </div>
            @endforeach
        </div>
    </div>
    <div class="col-md-12">
        {!!$articles->render()!!}
    </div>
</code>

article_table.php

<code>    public function up()
    {
        Schema::create('articles', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('title');
            $table->text('body')->nullable();
            $table->string('site')->nullable();
            $table->integer('avatars_id')->unsigned();;
            $table->integer('user_id');
            $table->string('nike')->nullable();
            $table->timestamps();

            $table->foreign('avatars_id')
                ->references('id')
                ->on('avatars')
                ->onDelete('cascade');
        });
    }
</code>

avatars_table.php

<code>    public function up()
    {
        Schema::create('avatars', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('title');
            $table->string('url');
            $table->text('path');
            $table->timestamps();
        });
    }
</code>

我不知道是哪里有问题,不管怎么弄都是报错..希望大神帮忙看看.谢谢了.

回复内容:

关联查询一致报错,不得已来SF麻烦各位大神帮我解答。

报错内容

<code>ErrorException in 777fb9d9b961f2b2453d93297ba8a847 line 15:
Trying to get property of non-object (View: /home/millyn/www/x.x/resources/views/NewHome.blade.php)
</code>

我的对应文件如下

Avatar.php

<code><?php namespace App;

use Illuminate\Database\Eloquent\Model;


class Avatar extends Model {


    public function article()
    {
        return $this -> hasMany('App\Article');
    }
}
</code>

Article.php

<code><?php namespace App;

use Illuminate\Database\Eloquent\Model;

class Article extends Model {

    //
    public function avatar()
    {
        return $this -> belongsTo('App\Avatar');
    }

}
</code>

Controller.php

<code>    public function index()
    {
        $articles = Article::with('avatar')->latest()->paginate(8);
        return view('NewHome', compact('articles'));
    }
</code>

NewHome.blade.php

<code>    <div class="container">
        <div class="row">
            @foreach ($articles as $page)
            <div class="col-md-3">
                <div class="panel panel-default">
                    <div class="panel-heading" style="white-space:nowrap;overflow:hidden;text-overflow:clip;">
                        <a title="{{ $page->title }}" href="%7B%7B%20URL('news/'.%24page->id)%20%7D%7D"> <h4>{{ $page->title }}</h4></a>
                    </div>
                    <div class="panel-body">
                        <a title="{{ $page->title }}" href="%7B%7B%20URL('news/'.%24page->id)%20%7D%7D"><img src="%7B%7B%24page->avatars_id->url%7D%7D" alt=""    style="max-width:90%"></a>
                    </div>
                </div>
            </div>
            @endforeach
        </div>
    </div>
    <div class="col-md-12">
        {!!$articles->render()!!}
    </div>
</code>

article_table.php

<code>    public function up()
    {
        Schema::create('articles', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('title');
            $table->text('body')->nullable();
            $table->string('site')->nullable();
            $table->integer('avatars_id')->unsigned();;
            $table->integer('user_id');
            $table->string('nike')->nullable();
            $table->timestamps();

            $table->foreign('avatars_id')
                ->references('id')
                ->on('avatars')
                ->onDelete('cascade');
        });
    }
</code>

avatars_table.php

<code>    public function up()
    {
        Schema::create('avatars', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('title');
            $table->string('url');
            $table->text('path');
            $table->timestamps();
        });
    }
</code>

我不知道是哪里有问题,不管怎么弄都是报错..希望大神帮忙看看.谢谢了.

<code>php</code><code>//打印下你查询的SQL语句看看
 public function article()
    {
        return $this -> hasMany('App\avatar','id','avatars_id');
    }

    public function avatar()
    {
        return $this -> belongsTo('App\App\Article','avatars_id','id');
    }
</code>

你可以dd($articles)来看看,相信在relations这一个上面会是null,总感觉你的relationship声明好奇怪

看第15行输出的是哪个变量,然后dd打印变量,根据问题再逐步排解。

应该是你的调用关联用错了

<code><img src="%7B%7B%24page->avatars_id->url%7D%7D" alt=""    style="max-width:90%">
</code>

这里应该用

<code>{{$page->avatar->url}}
</code>

因为你这里用的是

<code>$articles = Article::with('avatar')->latest()->paginate(8);
</code>

如果你要改变映射的名字,需要

<code>$articles = Article::with(array('avatars_id'=>function(){/*映射的关系*/}))->latest()->paginate(8);
</code>

这之后你要改变很多

谢谢各位的答案,但我是最后自己解决的.
我的解决方案和我挑选的答案是一个方法.
我在
Acticle.php里把内容改成了

<code>    public function avatar()
    {
        return $this -> belongsTo('App\Avatar','avatars_id');
    }
</code>

就行了,关于关联到模板上面的内容是
{{$page->avatar->url}
模型 关联方法 对应字段

非常感谢各位,我也贴出正确解决我问题的方法.
希望可以给以后遇到此类问题的朋友一个解决方案.

其实你只要一路跟 convention 走乖乖地建表就用 avatar_id 就不会出这些问题了。

另外建议你 hasMany() 关系的时候也用复数

<code>public function articles() 
{
    return $this->hasMany('App\Article');
}
</code>

这样写的时候自己也清楚了~

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。