简体   繁体   English

Laravel空对象hasMany关系

[英]Laravel null object hasMany relationship

SOLVED i should not have called out my attributes in the models. 解决了我不应该在模型中标注我的属性。 For some reason this was keeping it from working. 由于某种原因,这使它无法正常工作。

I have the following structure of two models, Listing and Image. 我有两个模型的以下结构,清单和图像。 Table Images has FK listing_id that references id on table Listing. 表格图像具有FK listing_id ,该id引用表格清单上的id

My Eloquent Model for Listing: 我的雄辩清单模型:

namespace App\Models;

use Illuminate\Database\Eloquent\Model as Model;

class Listing extends Model {

    protected $table = 'listings';
    protected $connection = 'mysql';

    public $id;
    public $name;
    public $value;
    public $itemDescr;
    public $user_id;
    public $category_id;

    protected $fillable = [
        'id',
        'name',
        'value',
        'itemDescr',
        'user_id',
        'category_id'
    ];

    /**
     * Return images
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function images()
    {
        return $this->hasMany('App\Models\Image');
    }

}

My Eloquent Model for Image: 我雄辩的图像模型:

namespace App\Models;
use Illuminate\Database\Eloquent\Model as Model;

class Image extends Model
{
    protected $table = 'images';
    protected $connection = 'mysql';

    public $id;
    public $imageType;
    public $title;
    public $filename;
    public $path;
    public $author_id;
    public $listing_id;
    public $user_id;
    public $size;
    public $width;
    public $height;


    protected $fillable = [
        'imageType',
        'title',
        'filename',
        'path',
        'author_id',
        'listing_id',
        'user_id',
        'size',
        'width',
        'height'
    ];

    /**
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function listings()
    {
        return $this->belongsTo('App\Models\Listing', 'listing_id');
    }

}

My controller: 我的控制器:

/**
 * Return the full list of listings
 *
 * @param Request $request
 *
 * @return string
 */

    public function itemList(Request $request)
    {
        $listings = Listing::with('images')->get();

        return $listings;

    }

I cannot seem to return my listing WITH the image objects -- they are always null. 我似乎无法返回带有图像对象的列表-它们始终为null。 Here is a sample response: 这是一个示例响应:

{ id: 6, name: "rare listing item", value: "100", itemDescr: "this is a descr", user_id: 1, category_id: 6, created_at: "2016-05-30 13:47:33", updated_at: "2016-05-30 13:47:33", images: [ ] } {id:6,名称:“稀有商品”,值:“ 100”,itemDescr:“这是一个说明”,user_id:1,category_id:6,created_at:“ 2016-05-30 13:47:33” ,updated_at:“ 2016-05-30 13:47:33”,图片:[]}

The database shows two images with listing_id of 6 for this particular item. 数据库显示此特定项目的两个图像,它们的listing_id为6。 I cannot figure out what could be wrong, I've tried nearly all suggestions. 我无法弄清楚可能出了什么问题,我几乎尝试了所有建议。 Any ideas? 有任何想法吗?

In Laravel table columns and their values is held in $attributes property, Laravel uses __get() magic method to get the $attributes data. 在Laravel表列中,它们的值保存在$attributes属性中,Laravel使用__get()魔术方法获取$attributes数据。 By defining public $listing_id as property on your model it just like telling Laravel: "Hey this is the listing_id and not the one that held $attributes property so please return this instead". 通过在模型上将public $listing_id定义为属性,就像告诉Laravel:“嘿,这是listing_id而不是持有$attributes属性,请改为返回它”。

When you define the relationship: 定义关系时:

return $this->belongsTo('App\Models\Listing', 'listing_id', 'id');

The public $listing_id property will be returned which is the value is null . 将返回public $listing_id属性,该属性的值为null Remove those public property definition and it should be works. 删除那些公共财产定义,它应该是可行的。

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

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