jeudi 27 avril 2017

Eloquent "all()" method [Laravel 5.4]

Eloquent is not returning me the data correctly (that's what I think).

I'm getting null variables here. What should I do?

When i do dd(Productos::all()); it shows me what is showed below (the info I want lies in 'original' and 'attributes'):

Collection {#178 ▼
  #items: array:4 [▼
    0 => Productos {#179 ▼
      +id: null
      +id_categoria: null
      +nombre: null
      +precio: null
      +descripcion: null
      +created_at: null
      +udated_at: null
      #fillable: array:4 [▼
        0 => "id_categoria"
        1 => "nombre"
        2 => "precio"
        3 => "descripcion"
      ]
      #connection: null
      #table: null
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:7 [▼
        "id" => 1
        "id_categoria" => 1
        "nombre" => "Invitacion 1"
        "precio" => "75.000"
        "descripcion" => """
          Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum ut ultrices quam. Phasellus eget magna in justo cursus posuere. Curabitur porta, purus eu co ▶
          \n
          """
        "created_at" => "2017-04-27 21:19:41"
        "updated_at" => "2017-04-27 21:19:41"
      ]
      #original: array:7 [▼
        "id" => 1
        "id_categoria" => 1
        "nombre" => "Invitacion 1"
        "precio" => "75.000"
        "descripcion" => """
          Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum ut ultrices quam. Phasellus eget magna in justo cursus posuere. Curabitur porta, purus eu co ▶
          \n
          """
        "created_at" => "2017-04-27 21:19:41"
        "updated_at" => "2017-04-27 21:19:41"
      ]
      #casts: []
      #dates: []
      #dateFormat: null
      #appends: []
      #events: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #guarded: array:1 [▼
        0 => "*"
      ]
    }
    1 => Productos {#180 ▼
      +id: null
      +id_categoria: null
      +nombre: null
      +precio: null
      +descripcion: null
      +created_at: null
      +udated_at: null
      #fillable: array:4 [▶]
      #connection: null
      #table: null
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:7 [▼
        "id" => 2
        "id_categoria" => 2
        "nombre" => "Invitacion 2"
        "precio" => "75.000"
        "descripcion" => """
          Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum ut ultrices quam. Phasellus eget magna in justo cursus posuere. Curabitur porta, purus eu co ▶
          \n
          """
        "created_at" => "2017-04-27 21:20:09"
        "updated_at" => "2017-04-27 21:20:09"
      ]
      #original: array:7 [▼
        "id" => 2
        "id_categoria" => 2
        "nombre" => "Invitacion 2"
        "precio" => "75.000"
        "descripcion" => """
          Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum ut ultrices quam. Phasellus eget magna in justo cursus posuere. Curabitur porta, purus eu co ▶
          \n
          """
        "created_at" => "2017-04-27 21:20:09"
        "updated_at" => "2017-04-27 21:20:09"
      ]
      #casts: []
      #dates: []
      #dateFormat: null
      #appends: []
      #events: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #guarded: array:1 [▼
        0 => "*"
      ]
    }
  ]
}

Controller:

public function showAll(){
    return view('productos.index',[
                'productos' => Productos::all()
                ]);
}

View:

@foreach($productos as $producto)
 <div class="row">
  <div class="col-sm-6 col-md-4">
    <div class="thumbnail clearfix">
      <img class="image-product" src="image.jpg" resposive alt="...">
      <div class="caption">
        <h3></h3>
        <p class="justify">
            
        </p>
        <p><a href="#" class="btn btn-primary pull-right" role="button"><i class="fa fa-cart-plus"></i> Añadir al carrito</a>
        </div>
      </div>
    </div>
  </div>
@endforeach


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire