Laravel5 关联查询问题

php中文网
发布: 2016-06-06 20:30:09
原创
1343人浏览过

关联查询一致报错,不得已来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

jQuery省市二级联动关联店面查询表单代码
jQuery省市二级联动关联店面查询表单代码

JQuery是继prototype之后又一个优秀的Javascript库。它是轻量级的js库 ,它兼容CSS3,还兼容各种浏览器(IE 6.0+, FF 1.5+, Safari 2.0+, Opera 9.0+),jQuery2.0及后续版本将不再支持IE6/7/8浏览器。jQuery使用户能更方便地处理HTML(标准通用标记语言下的一个应用)、events、实现动画效果,并且方便地为网站提供A

jQuery省市二级联动关联店面查询表单代码 55
查看详情 jQuery省市二级联动关联店面查询表单代码
<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="{{ URL('news/'.$page->id) }}"> <h4>{{ $page->title }}</h4></a>
                    </div>
                    <div class="panel-body">
                        <a title="{{ $page->title }}" href="{{ URL('news/'.$page->id) }}">@@##@@avatars_id->url}}" alt="" width="180px"></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="{{ URL('news/'.$page->id) }}"> <h4>{{ $page->title }}</h4></a>
                    </div>
                    <div class="panel-body">
                        <a title="{{ $page->title }}" href="{{ URL('news/'.$page->id) }}">@@##@@avatars_id->url}}" alt="" width="180px"></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>@@##@@avatars_id->url}}" alt="" width="180px">
</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>
登录后复制

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

Laravel5 关联查询问题Laravel5 关联查询问题
相关标签:
php
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号