【问题标题】:Laravel 5.2: Trying to get property of non-objectLaravel 5.2:试图获取非对象的属性
【发布时间】:2016-12-25 15:32:08
【问题描述】:

这是我第一次使用 Laravel 5.2,我正在尝试查看其作者的两个引用列表

我要列出的数据是;

  1. quotes 来自表 quotes
  2. 姓名来自表作者

quotes 和 authors

的我的数据库架构

引号;

Schema::create('quotes', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('author_id');
    $table->text('quotes');
    $table->timestamps();
});

作者;

Schema::create('authors', function (Blueprint $table) {
    $table->increments('id');
    $table->text('name');
    $table->text('origin');
    $table->integer('died');
    $table->timestamps();
});

我的控制器是这样的;

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Quotes;
use App\Authors;

class QuotesController extends Controller {

public function index() {
    $quotes = Quotes::with('authors')->orderBy('created_at','desc')->paginate(25);
    return view('quotes.all',[
        'quotes' => $quotes
    ]);
}

我的作者模型;

namespace App;

use Illuminate\Database\Eloquent\Model;

class Authors extends Model {
    public function Quotes() {
        return $this->hasMany('App\Quotes');
    }
}

我的报价模型;

namespace App;

use Illuminate\Database\Eloquent\Model;

class Quotes extends Model {
    public function Authors() {
        return $this->belongsTo('App\Authors');
    }
}

最后,当然是我的观点;

<ul class="list-group">
    @forelse ($quotes as $quote)   
        <li class="list-group-item">{{ $quote->quotes }} <br>
            <span class="label label-primary">{{ $quote->authors->name }}</span>
        </li>
    @empty
        <p>No quotes</p>
    @endforelse
</ul>

我是 PHP 新手,希望您能帮我解决这个问题。我真的很感激。

【问题讨论】:

  • 如果您说出错误来自代码的哪一部分,将会有所帮助。
  • 请注意:模型通常以单数命名作为命名约定
  • @apokryfos,当我删除 {{ $quote->author->name }} 时,程序运行。
  • 非常感谢@RasmusRosengren。
  • 用{{ $quote-&gt;Authors()-&gt;name }}替换{{ $quote-&gt;authors-&gt;name }}怎么样

标签: php laravel


【解决方案1】:

经过一番修补后,我仍然无法找出问题所在,但我可以使用下面的代码来解决问题

App/Author.php(这里我把文件名从Authors改成了Author(单数)来匹配类名)

namespace App;

use Illuminate\Database\Eloquent\Model;

class Author extends Model {
    // Here I changed the method name to have all lower case
    public function quotes() {
        return $this->hasMany('App\Quote');
    }
}

App/Quote.php(文件名更改与Author.php相同)

namespace App;

use Illuminate\Database\Eloquent\Model;

class Quote extends Model {
    // Method name changed to lower case and singular since it belongs to only ONE other model
    public function author() {
        return $this->belongsTo('App\Author');
    }
}

App/Http/Controllers/QuoteController.php(QuoteController 由 Quote(s)Controller 更改)

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Quote;
use App\Author;

// Class name changed accordingly
class QuoteController extends Controller 
{
    public function index() {
        // Changed with('authors') to with('author'), singular
        $quotes = Quote::with('author')->orderBy('created_at', 'desc')->paginate(25);
        // Change the view here
        return view('home', [
            'quotes' => $quotes
        ]);
    }
}

resources/views/home.blade.php

<ul class="list-group">
    @forelse ($quotes as $quote)   
        <li class="list-group-item">{{ $quote->quotes }} <br>
            <!-- Changed $quote->authors->name to $quote->author->name -->
            <span class="label label-primary">{{ $quote->author->name }}</span>
        </li>
    @empty
        <p>No quotes</p>
    @endforelse
</ul>

【讨论】:

    【解决方案2】:

    我无法确切说明导致错误的原因,但这里有几个关于您发布的代码的 cmets。

    我会改变你的报价模型如下:

    namespace App;
    
    use Illuminate\Database\Eloquent\Model;
    
    class Quotes extends Model {
        public function author() {
            return $this->belongsTo('App\Authors');
        }
    }
    

    我对 Quote 模型进行了 2 处更改。首先,Laravel 5.2 通常遵循PSR-2 Coding Style Guide,通常你应该这样做。所以我改变了 author() 方法的大小写。另外,由于关系是单数的,我用单数复数重命名了方法。

    复数 Authors() 关系方法也可能导致 Laravel 的关系处理出现问题。 Laravel 将“_id”连接到方法名称以确定外键字段的名称,在您的情况下,Laravel 将尝试根据 Quotes 表中 authors_id 字段中的值获取父作者。显然这个字段不存在。更正方法名称的复数形式应该可以解决这个潜在问题。

    最后,对于 Blade 模板,我尝试这样实现它:

    <ul class="list-group">
    @forelse ($quotes as $quote)   
        <li class="list-group-item">{{ $quote->quotes }} <br>
            <span class="label label-primary">
                @if (quote->author())
                    {{ $quote->author()->name }}
                @else
                    The author of this quote is missing!!!!
                @endif
            </span>
        </li>
    @empty
        <p>No quotes</p>
    @endforelse
    

    您还可以尝试使用laravel-debugbar 之类的工具来轻松查看 ORM 发出的查询。这可以让您发现查询中的问题,您可以追溯到代码中的问题。

    【讨论】:

      猜你喜欢
      • 2016-12-23
      • 2018-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-25
      • 2015-09-22
      相关资源
      最近更新 更多