简体   繁体   English

是什么引发“试图获取非对象的属性”错误

[英]What triggers the “Trying to get property of non-object” error

Since I started with my first Laravel project I've been having the error Trying to get property 'column_name' of non-object nonstop, and most of the time I've been able to fix it one way or another, but the problem is, I have no idea of why I get the error, or how can I prevent it. 自从我的第一个Laravel项目开始以来,我一直遇到错误, Trying to get property 'column_name' of non-object不间断的Trying to get property 'column_name' of non-object ,并且大多数时候我已经能够以一种或另一种方式修复它,但是问题是,我不知道为什么会收到错误,或者如何防止此错误。 For example, 例如,

// In my controller I have this domPDF function...
public function pdfgen(Request $request, $id) {
        $data = Table::find($id);
        View()->share('data', $data);

        if ($request -> has('download')) {
            $pdf = PDF::loadView('pdf/pdfgen');
            return $pdf ->download('pdfgen.pdf');
        }

        return view('pdf/pdfgen');
}
// In my blade file I have a table calling this column...
<td>{{ $data->column }}</td>

// and this link making the request in the pdfgen function... 
<a class="btn btn-primary" href="{{ route('pdfgen', ['download'=>'pdf']) }}">Download PDF</a>
// And in my routes of web.php...
Route::get('/pdfgen/{id}', array('as' => 'pdfgen', 'uses' => 'PDFController@pdfgen'));

and whenever I press that download link, I get a Whoops page with the trying to get property 'column' of non-object (View: /usr/src/workapp/resources/views/pdf/pdfgen.blade.php) . 每当我按下该下载链接时,我都会获得一个Whoops页面,其中包含trying to get property 'column' of non-object (View: /usr/src/workapp/resources/views/pdf/pdfgen.blade.php) Why am I getting this error and not the open/save dialog? 为什么会出现此错误,而不是“打开/保存”对话框?

EDIT: Forgot to add, before trying to get specific data with $id, I called the whole table ( $data = Table::all(); ) and used a @foreach in the blade view to print it in the table 编辑:忘记添加,在尝试使用$ id获取特定数据之前,我调用了整个表( $data = Table::all(); ),并在刀片视图中使用@foreach将其打印在表中

EDIT 2: Adding the model 编辑2:添加模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Table extends Model
{
    protected $table = 'table';
    protected $primaryKey = 'id';
    public $incrementing = false;

    protected $fillable = [
        // all columns are strings, even id
        'id',
        'column',
        'column2',
        'column3',
    ];

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

When you do find($id) and the record is not present, it returns null . 当您执行find($id)并且记录不存在时,它返回null So if you try to access any property ->column , it will throw error. 因此,如果您尝试访问任何属性->column ,它将引发错误。

You can either do findOrFail($id) or have condition if (is_null($data)) or on frontend have $data->column ?? 'defaultvalue ' 您可以执行findOrFail($id) if (is_null($data)) 前端具有$data->column ?? 'defaultvalue ' $data->column ?? 'defaultvalue '

Now, the second situation where your model is present but you still get error, do : 现在,存在您的模型但仍然出现错误的第二种情况,请执行以下操作:

$data = Table::find($id);
dd(array_keys($data->toArray()));

You should see the column you are trying to access if it is present. 您应该看到您尝试访问的column如果存在)。

Lastly, if it is not present in above step but is there in the table, then check for protected $hidden = [] setting of your model which essentially hides certain columns. 最后,如果它在上面的步骤中不存在,但在表中存在,则检查模型的protected $hidden = []设置,该设置本质上隐藏了某些列。

This happend because your $data might be null and your record with given $id might not exists in your database. 发生这种情况是因为您的$data可能为null并且具有给定$id记录可能不存在于数据库中。

Try to change your code like below: 尝试如下更改代码:

Table::findOrFail($id);

From now on if your table with given id doesn't exist, it'll automatically return 404 page. 从现在开始,如果您的具有给定ID的表不存在,它将自动返回404页。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM