samedi 23 juin 2018

Laravel eloquent relationship does not retrieving results

I am trying to make table relationships with laravel(5.6) eloquent. And I'm not able to retrieve relational table results from model. Below I mentioned the relation.

relationship : Item has one category

This is my item model

class InventoryItems extends Model
{
    /**
     * table row delete
     */
    use SoftDeletes;

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'inventory_items';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $guarded = ['id'];

    protected $fillable = ['name','type_id','category_id','sub_category_id','description','cost_price','sale_price','image_path','image','store_id','status'];


    public function category()
    {
        return $this->belongsTo('App\ItemCategories', 'id', 'category_id');
    }
}

This is my category model

class ItemCategories extends Model
{
    /**
     * table row delete
     */
    use SoftDeletes;

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'item_categories';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $guarded = ['id'];

    protected $fillable = ['name'];
}

This is my controller

 public function index(Request $request)
    {
        $items = InventoryItems::all();

        dd($items);

    }

This is result

Collection {#722 ▼
  #items: array:2 [▼
    0 => InventoryItems {#719 ▼
      #table: "inventory_items"
      #guarded: array:1 [▶]
      #fillable: array:11 [▼
        0 => "name"
        1 => "type_id"
        2 => "category_id"
        3 => "sub_category_id"
        4 => "description"
        5 => "cost_price"
        6 => "sale_price"
        7 => "image_path"
        8 => "image"
        9 => "store_id"
        10 => "status"
      ]
      #connection: "mysql"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:15 [▼
        "id" => 1
        "name" => "Dell Keyboard"
        "type_id" => 1
        "category_id" => 1
        "sub_category_id" => 1
        "description" => "<p>test key</p>"
        "cost_price" => 100.0
        "sale_price" => 500.0
        "image_path" => null
        "image" => null
        "store_id" => 1
        "status" => 1
        "created_at" => "2018-06-02 14:31:32"
        "updated_at" => "2018-06-02 14:31:32"
        "deleted_at" => null
      ]
      #original: array:15 [▶]
      #changes: []
      #casts: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #forceDeleting: false
    }

I'm new to eloquent relations and I referred laravel documentation and some other tutorials but it did not workout. Must be a silly mistake thought.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire