Laravel5 association query

Source: Internet
Author: User
An error is reported when the association query is consistent. If you have to come to SF, please help me solve the problem. Error message {code ...} my corresponding file is as follows: Avatar. php {code ...} article. php {code ...} controller. php {code ...} newHome. blade. php {code ...} article_table .... an error is reported when the association query is consistent. If you have to come to SF, please help me solve the problem.

Error Message

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

My corresponding files are as follows:

Avatar. php


   hasMany('App\Article');    }}

Article. php


   belongsTo('App\Avatar');    }}

Controller. php

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

NewHome. blade. php

    

@foreach ($articles as $page)

title }}" href="{{ URL('news/'.$page->id) }}"> {{ $page->title }}

title }}" href="{{ URL('news/'.$page->id) }}">avatars_id->url}}" alt="" width="180px">

@endforeach

{!!$articles->render()!!}

Article_table.php

    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');        });    }

Avatars_table.php

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

I don't know where the problem is. No matter how it is done, it is an error. Hope you can help me. Thank you.

Reply content:

An error is reported when the association query is consistent. If you have to come to SF, please help me solve the problem.

Error Message

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

My corresponding files are as follows:

Avatar. php


   hasMany('App\Article');    }}

Article. php


   belongsTo('App\Avatar');    }}

Controller. php

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

NewHome. blade. php

    

@foreach ($articles as $page)

title }}" href="{{ URL('news/'.$page->id) }}"> {{ $page->title }}

title }}" href="{{ URL('news/'.$page->id) }}">avatars_id->url}}" alt="" width="180px">

@endforeach

{!!$articles->render()!!}

Article_table.php

    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');        });    }

Avatars_table.php

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

I don't know where the problem is. No matter how it is done, it is an error. Hope you can help me. Thank you.

php// Print the SQL statement you are querying to see public function article () {return $ this-> hasexample ('app \ avatar ', 'id ', 'avatars _ id');} public function avatar () {return $ this-> belonsto ('app \ App \ article', 'avatars _ id ', 'id ');}

You can check dd ($ articles). I believe it will be null on relations. It always feels strange to your relationship statement.

Check which variable is output in row 15th, and then print the variable in dd. Solve the problem gradually.

The call Association is incorrect.

avatars_id->url}}" alt="" width="180px">

Use

{{$page->avatar->url}}

Because you are using

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

If you want to change the ing name, you must

$ Articles = Article: with (array ('avatars _ id' => function () {/* ing relationship */})-> latest () -> paginate (8 );

After that, you have to change a lot.

Thank you for your answers, but I did it myself.
My solution and the answer I selected are one method.
On
Acticle.phpChanged the content

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

That's it. The content associated with the template is
{$ Page-> avatar-> url}
Fields of the model Association Method

Thank you very much. I have also posted a correct solution to my problem.
I hope to provide a solution for my friends who encounter such problems in the future.

In fact, you only need to build tables along the way with convention.avatar_idThis will not cause these problems.

In addition, we recommend that you use the plural value when using the hasMany () relationship.

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

I am also clear about this writing ~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.