Php 为什么我试图在拉威尔中得到非对象的属性
我有以下功能:Php 为什么我试图在拉威尔中得到非对象的属性,php,laravel,eloquent,Php,Laravel,Eloquent,我有以下功能: public function show($id){ $product = Product::findOrFail($id); $product_image = $product->image->file_name.".".$product->image->file_extension; return json_encode(array_merge($product->toArray(), ["image" => $p
public function show($id){
$product = Product::findOrFail($id);
$product_image = $product->image->file_name.".".$product->image->file_extension;
return json_encode(array_merge($product->toArray(), ["image" => $product_image]));
}
public function index() {
return json_encode(Product::all());
}
给定id,它将返回以下JSON:
{
"id": 1,
"parent_id": 0,
"category_id": 0,
"image": "1.png"
}
[
{
"id": 1,
"parent_id": 0,
"category_id": 0
},
{
"id": 2,
"parent_id": 0,
"category_id": 0
},
{
"id": 3,
"parent_id": 0,
"category_id": 0
},
{
"id": 4,
"parent_id": 0,
"category_id": 0
}
]
我有以下功能:
public function show($id){
$product = Product::findOrFail($id);
$product_image = $product->image->file_name.".".$product->image->file_extension;
return json_encode(array_merge($product->toArray(), ["image" => $product_image]));
}
public function index() {
return json_encode(Product::all());
}
将返回以下JSON:
{
"id": 1,
"parent_id": 0,
"category_id": 0,
"image": "1.png"
}
[
{
"id": 1,
"parent_id": 0,
"category_id": 0
},
{
"id": 2,
"parent_id": 0,
"category_id": 0
},
{
"id": 3,
"parent_id": 0,
"category_id": 0
},
{
"id": 4,
"parent_id": 0,
"category_id": 0
}
]
我的函数位于Laravel项目中API的控制器类中
ProductsController扩展控制器
产品
是一种雄辩的
模型
我试图实现的是使index函数返回带有image
属性的数组。以下是我的尝试:
public function index() {
$products = Product::all();
$productsWithImages = [];
for($i = 0; $i < count($products); $i++) {
$product = $products[$i];
$product_image = $product->image->file_name.".".$product->image->file_extension;
$productsWithImages[] = array_merge($product->toArray(), ["image" => $product_image]);
}
return json_encode(productsWithImages);
}
它指向下面一行:
$productsWithImages[] = array_merge($product->toArray(), ["image" => $product_image]);
产品
型号类别:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\File;
class Product extends Model
{
use SoftDeletes;
protected $fillable = ['parent_id', 'category_id'];
// this is for voyager
public function categoryId() {
return $this->belongsTo(ProductCategory::class);
}
// this is for voyager
public function parentId() {
return $this->belongsTo(self::class);
}
public function category() {
return $this->belongsTo(ProductCategory::class);
}
public function parent() {
return $this->belongsTo(Product::class);
}
public function files(){
return $this->hasOne(File::class, 'file_name');
}
public function image() {
return $this->files()->where('table_name', '=', 'products');
}
}
您的一些产品似乎没有附加图像
公共功能索引()
{
返回Product::all()->map(函数($Product){
如果($image=$product->image){
$image=$image->file\u name...$image->file\u扩展名;
}
返回$product->toArray()+压缩('image');
});
}
另外,您不必转换为JSON。Laravel会自动处理这些问题。您的一些产品似乎没有附加图像
公共功能索引()
{
返回Product::all()->map(函数($Product){
如果($image=$product->image){
$image=$image->file\u name...$image->file\u扩展名;
}
返回$product->toArray()+压缩('image');
});
}
另外,您不必转换为JSON。Laravel会自动处理这一问题。使用变异子和
$appends
属性的组合
将此添加到您的模型:
class Product extends Model
{
// ...
/**
* The accessors to append to the model's array form.
*
* @var array
*/
protected $appends = ['image_path'];
// ...
/**
* Get the full path for the product image.
*
* @return bool
*/
public function getImagePath()
{
if ($this->image) {
return $this->image->file_name . "." . $this->image->file_extension;
}
return ""; // or any other sensible default
}
// ...
}
您可以在这里的文档中阅读更多关于它的信息:。使用Mutators和
$appends
属性的组合
将此添加到您的模型:
class Product extends Model
{
// ...
/**
* The accessors to append to the model's array form.
*
* @var array
*/
protected $appends = ['image_path'];
// ...
/**
* Get the full path for the product image.
*
* @return bool
*/
public function getImagePath()
{
if ($this->image) {
return $this->image->file_name . "." . $this->image->file_extension;
}
return ""; // or any other sensible default
}
// ...
}
您可以在此处的文档中阅读更多信息:。更改后请尝试:
public function index() {
$products = Product::all();
$productsWithImages = [];
foreach($products as $product) {
$product_image = !(empty($product->image)) ? $product->image->file_name.".".$product->image->file_extension : '';
$productsWithImages[] = array_merge($product->toArray(), ["image" => $product_image]);
}
return json_encode(productsWithImages);
}
更改此项后重试:
public function index() {
$products = Product::all();
$productsWithImages = [];
foreach($products as $product) {
$product_image = !(empty($product->image)) ? $product->image->file_name.".".$product->image->file_extension : '';
$productsWithImages[] = array_merge($product->toArray(), ["image" => $product_image]);
}
return json_encode(productsWithImages);
}
同样的错误:
试图在$image=$product->image->file\u name.”行获取非对象的属性
您的一些产品在数据库中没有映像吗?@ceejayoz Oops,这是我的问题:)相同的错误:试图在$image=$product->image->file\u name.行中获取非对象的属性代码>你的一些产品在数据库中没有图像吗?@ceejayoz Oops,这是我的问题:)你能显示这个打印的输出吗($product)?@EXPERTOR我已经用所需的输出更新了我的问题。$product->image->file name这是错误。正如您在$product的输出中所看到的,没有图像名称,我将依赖于$product->image
的代码放在if($product->image){}
块中。您能显示这个打印的输出吗($product)?@Exprator我已经用所需的输出更新了我的问题。$product->image->file U name这是错误。正如您在$product的输出中看到的,没有图像名称,我将依赖于$product->image
的代码放在if($product->image){}
块中。这可能会导致相同的错误。我怀疑OP的某些产品没有图像。我更改为“回答”以首先检查图像。这可能会导致相同的错误。我怀疑OP有一些没有图片的产品。我改为回答,先检查图片。