简体   繁体   中英

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>{{ $producto->nombre }}</h3> <p class="justify"> {{ $producto->descripcion }} </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 

I think you have declared properties in your model. Something like:

class Productos extends Model {
    public $id;
    public $id_categoria;
    // etc.
}

You do not need to do that in Laravel. There is some magic with attributes, so just remove all this properties and let Laravel build the object for you. You will be able to call your attributes from your view after calling all() . Simplified example:

public function showAll(){
    $ps = Productos::all();
    foreach($ps as $p) {
         dump($producto->nombre);
         dump($producto->descripcion); // <-- it displays what you wanted.
    }
}

If you were using public properties in your model ( public $nombre , etc.) the magic system of Laravel attributes is broken, remove it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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