自从我开始我的第一个 Laravel 项目以来,我一直在
Trying to get property 'column_name' of non-object
不停地遇到错误,而且大多数时候我都能够以一种或另一种方式修复它,但问题是,我不知道为什么会出现错误,或者我该如何防止它。例如,
// 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'));
每当我按下该下载链接时,我都会得到一个带有trying to get property 'column' of non-object (View: /usr/src/workapp/resources/views/pdf/pdfgen.blade.php)
. 为什么我收到此错误,而不是打开/保存对话框?
编辑:忘记添加,在尝试使用 $id 获取特定数据之前,我调用了整个表($data = Table::all();
)并@foreach
在刀片视图中使用 a 在表中打印它
编辑 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');
}
}