在编辑和创建 blade 后,我的索引突然有了 "Trying to get property of non-object"
My index suddenly having "Trying to get property of non-object" after doing edit and create blade
谢谢你们上次帮助我。我现在遇到的问题是关于我的观点 blade。在我创建、编辑和创建 blade 之前,此 index.blade 已完全正常工作。在我完成另外两个 blade 之后,我注意到 index.blade 现在正在 Trying to get 属性 of non-object
我想做的是在产品中显示类别 table。
产品Table
编号,名称,categories_id
类别Table
身份证、姓名
产品型号
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Products extends Model
{
use SoftDeletes;
protected $fillable = [
'product_code',
'name',
'categories_id',
'wh1_limit_warning',
'wh2_limit_warning',
'price',
'selling_price',
'user_id'
];
protected $dates = ['deleted_at'];
public function category() {
// return $this->belongsTo(Categories::class);
return $this->belongsTo('App\Categories', 'categories_id', 'id');
}
}
类别模型
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Categories extends Model
{
protected $fillable = ['name'];
public function Products()
{
// return $this->hasMany(Products::class);
return $this->hasMany('App\Products', 'categories_id');
}
}
索引Blade
<div class="panel-body">
<div class="result-set">
<table class="table table-bordered table-striped table-hover text-center" id="data-table">
<thead>
<tr>
<th class="col-md-1 text-center">Code</th>
<th class="col-md-2 text-center">Name</th>
<th class="col-md-2 text-center">Category</th>
<th class="col-md-1 text-center"><small>Warehouse 1<br/>Limit Warning</small></th>
<th class="col-md-1 text-center"><small>Warehouse 2<br/>Limit Warning<small></th>
<th class="col-md-1 text-center">Price</th>
<th class="col-md-1 text-center">Selling Price</th>
@can('update_product', 'delete_product')
<th class="text-center col-md-1 text-center">Actions</th>
@endcan
</tr>
</thead>
<tbody>
@foreach($products as $product)
<tr>
<td>{{ $product->product_code }}</td>
<td>{{ $product->name }}</td>
<td><b>{{ $product->category->name }}</b></td>
<td>{{ $product->wh1_limit_warning }}</td>
<td>{{ $product->wh2_limit_warning }}</td>
<td>{{ $product->price }}</td>
<td>{{ $product->selling_price }}</td>
@can('update_product')
<td>
<button data-toggle="tooltip" title="Update product" onclick="window.location='{{ route('products.edit', $product) }}'" name="edit" class="action-button edit">
<i class="fas fa-edit"></i>
</button>
<form action="{{ route('products.delete', $product )}}" method="post">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<button data-toggle="tooltip" title="Delete product" type="submit" class="action-button delete" onclick="return confirm('Are you sure you want to delete this?');"><i class="fas fa-trash-alt"></i></button>
</form>
</td>
@endcan
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
产品控制器
<?php
namespace App\Http\Controllers;
use App\Products;
use App\Categories;
use Illuminate\Http\Request;
class ProductsController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$products = Products::with('category')->get();
return view('products.index')->with('products',$products);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$products = Products::with('category')->get();
$categories = Categories::get()->pluck('name','id');
return view('products.create',compact('categories'))->with('products', $products);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$products = Products::create($request->only(
'product_code',
'name',
'categories_id',
'wh1_limit_warning',
'wh2_limit_warning',
'price',
'selling_price',
'user_id'
));
$this->validate($request, [
'product_code' => '',
'name' => '',
'categories_id' => 'required|integer',
'wh1_limit_warning' => 'required|integer',
'wh2_limit_warning' => 'required|integer',
'price' => 'required|integer',
'selling_price' => 'required|integer',
'user_id' => 'required|integer',
]);
flash('New product added!');
return redirect(route('products.index'));
}
/**
* Display the specified resource.
*
* @param \App\Products $products
* @return \Illuminate\Http\Response
*/
public function show(Products $products)
{
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Products $products
* @return \Illuminate\Http\Response
*/
public function edit(Products $product)
{
$products = Products::all();
$categories = Categories::all('name','id');
return view('products.edit',compact('product','categories'));
//return view('products.edit',compact('product','categories'))->with('products', $products);
// return view('products.edit', compact('product'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Products $products
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Products $product)
{
$product->update($request->only(
'product_code',
'name',
'categories_id',
'wh1_limit_warning',
'wh2_limit_warning',
'price',
'selling_price',
'user_id'
));
return redirect()->route('products.index');
}
/**
* Remove the specified resource from storage.
*
* @param \App\Products $products
* @return \Illuminate\Http\Response
*/
public function destroy(Products $product)
{
$product->delete();
return redirect(route('products.index'));
}
}
我看不出哪里出错了,因为我没有更改控制器的 public 函数 index() 中的任何内容。同样,EDIT 和 Create 正在运行我能够在下拉列表中显示类别而没有任何问题,只有这个 index.blade。
Please see this screenshot of the issue
在此先感谢您!
我猜你的查询 return 是一个数组而不是一个对象。尝试倾倒您的 return 以确保。
如果它是一个数组,您可以尝试在 index.blade.php:
中使用 []
而不是 ->
<td><b>{{ $product->category['name'] }}</b></td>
似乎会有一些产品没有任何类别。因此该产品不会与任何类别相关联,并且不会有任何类别对象,这就是它出现此错误的原因。
您可以在显示产品包含或不包含类别的类别名称之前添加一个检查。
<td><b>{{ $product->category ? $product->category->name : "" }}</b></td>
谢谢你们上次帮助我。我现在遇到的问题是关于我的观点 blade。在我创建、编辑和创建 blade 之前,此 index.blade 已完全正常工作。在我完成另外两个 blade 之后,我注意到 index.blade 现在正在 Trying to get 属性 of non-object
我想做的是在产品中显示类别 table。
产品Table
编号,名称,categories_id
类别Table
身份证、姓名
产品型号
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Products extends Model
{
use SoftDeletes;
protected $fillable = [
'product_code',
'name',
'categories_id',
'wh1_limit_warning',
'wh2_limit_warning',
'price',
'selling_price',
'user_id'
];
protected $dates = ['deleted_at'];
public function category() {
// return $this->belongsTo(Categories::class);
return $this->belongsTo('App\Categories', 'categories_id', 'id');
}
}
类别模型
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Categories extends Model
{
protected $fillable = ['name'];
public function Products()
{
// return $this->hasMany(Products::class);
return $this->hasMany('App\Products', 'categories_id');
}
}
索引Blade
<div class="panel-body">
<div class="result-set">
<table class="table table-bordered table-striped table-hover text-center" id="data-table">
<thead>
<tr>
<th class="col-md-1 text-center">Code</th>
<th class="col-md-2 text-center">Name</th>
<th class="col-md-2 text-center">Category</th>
<th class="col-md-1 text-center"><small>Warehouse 1<br/>Limit Warning</small></th>
<th class="col-md-1 text-center"><small>Warehouse 2<br/>Limit Warning<small></th>
<th class="col-md-1 text-center">Price</th>
<th class="col-md-1 text-center">Selling Price</th>
@can('update_product', 'delete_product')
<th class="text-center col-md-1 text-center">Actions</th>
@endcan
</tr>
</thead>
<tbody>
@foreach($products as $product)
<tr>
<td>{{ $product->product_code }}</td>
<td>{{ $product->name }}</td>
<td><b>{{ $product->category->name }}</b></td>
<td>{{ $product->wh1_limit_warning }}</td>
<td>{{ $product->wh2_limit_warning }}</td>
<td>{{ $product->price }}</td>
<td>{{ $product->selling_price }}</td>
@can('update_product')
<td>
<button data-toggle="tooltip" title="Update product" onclick="window.location='{{ route('products.edit', $product) }}'" name="edit" class="action-button edit">
<i class="fas fa-edit"></i>
</button>
<form action="{{ route('products.delete', $product )}}" method="post">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<button data-toggle="tooltip" title="Delete product" type="submit" class="action-button delete" onclick="return confirm('Are you sure you want to delete this?');"><i class="fas fa-trash-alt"></i></button>
</form>
</td>
@endcan
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
产品控制器
<?php
namespace App\Http\Controllers;
use App\Products;
use App\Categories;
use Illuminate\Http\Request;
class ProductsController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$products = Products::with('category')->get();
return view('products.index')->with('products',$products);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$products = Products::with('category')->get();
$categories = Categories::get()->pluck('name','id');
return view('products.create',compact('categories'))->with('products', $products);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$products = Products::create($request->only(
'product_code',
'name',
'categories_id',
'wh1_limit_warning',
'wh2_limit_warning',
'price',
'selling_price',
'user_id'
));
$this->validate($request, [
'product_code' => '',
'name' => '',
'categories_id' => 'required|integer',
'wh1_limit_warning' => 'required|integer',
'wh2_limit_warning' => 'required|integer',
'price' => 'required|integer',
'selling_price' => 'required|integer',
'user_id' => 'required|integer',
]);
flash('New product added!');
return redirect(route('products.index'));
}
/**
* Display the specified resource.
*
* @param \App\Products $products
* @return \Illuminate\Http\Response
*/
public function show(Products $products)
{
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Products $products
* @return \Illuminate\Http\Response
*/
public function edit(Products $product)
{
$products = Products::all();
$categories = Categories::all('name','id');
return view('products.edit',compact('product','categories'));
//return view('products.edit',compact('product','categories'))->with('products', $products);
// return view('products.edit', compact('product'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Products $products
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Products $product)
{
$product->update($request->only(
'product_code',
'name',
'categories_id',
'wh1_limit_warning',
'wh2_limit_warning',
'price',
'selling_price',
'user_id'
));
return redirect()->route('products.index');
}
/**
* Remove the specified resource from storage.
*
* @param \App\Products $products
* @return \Illuminate\Http\Response
*/
public function destroy(Products $product)
{
$product->delete();
return redirect(route('products.index'));
}
}
我看不出哪里出错了,因为我没有更改控制器的 public 函数 index() 中的任何内容。同样,EDIT 和 Create 正在运行我能够在下拉列表中显示类别而没有任何问题,只有这个 index.blade。
Please see this screenshot of the issue
在此先感谢您!
我猜你的查询 return 是一个数组而不是一个对象。尝试倾倒您的 return 以确保。
如果它是一个数组,您可以尝试在 index.blade.php:
中使用[]
而不是 ->
<td><b>{{ $product->category['name'] }}</b></td>
似乎会有一些产品没有任何类别。因此该产品不会与任何类别相关联,并且不会有任何类别对象,这就是它出现此错误的原因。
您可以在显示产品包含或不包含类别的类别名称之前添加一个检查。
<td><b>{{ $product->category ? $product->category->name : "" }}</b></td>