laravel 5.5属性[libros]在此集合实例上不存在

时间:2018-10-02 08:03:38

标签: laravel-5.5

我不断收到一个错误消息,指出此集合实例上不存在属性[libros]。我究竟做错了什么?检索我的libros的所有数据的正确方法是什么?

我尝试了网站上给出的几个答案,例如: -Property [stats]在此集合实例上不存在。 -Laravel-此集合实例上不存在属性[名称]。 -“此集合实例上不存在属性[registration_types]。 -Property [comment]在此集合实例上不存在。 一无所有,我已经能够获得我的结果。 这些是我的模特 Almacen Model Libro Model Almacen模型

<?php

namespace App\Models\Administracion\Almacenes;

use Illuminate\Database\Eloquent\Model;
use App\Models\Administracion\Almacenes\Libro;


class Almacen extends Model
{

    protected $table = 'almacenes';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'clave','nombre', 'calle','colonia','cp','telefono','estado',
    ];

    //Relaciones

    //Un almacén tiene muchos libros
    public function libros()
    {
        // return $this->belongsToMany('App\Models\Administracion\Almacenes\Libro','almacen_libro')
        return $this->belongsToMany(Libro::class,'almacen_libro', 'almacen_id', 'libro_id')
                    ->withPivot('stock')
                    ->withTimestamps()
                    ->using('App\Models\Administracion\Almacenes\AlmacenLibro');
    }

}

Libro模型

<?php

namespace App\Models\Administracion\Almacenes;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;

class Libro extends Model
{

    protected $table = 'libros';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'clave', 'nombre', 'descripcion','imagen','nivelLibro_id'
    ];

    //Relaciones

    //Muchos libros hay en muchos almacenes
    public function almacenes()
    {
        return $this->belongsToMany('App\Models\Administracion\Almacenes\Almacen', 'almacen_libro', 'almacen_id', 'libro_id')
                    ->withPivot('stock')
                    ->withTimestamps()
                    ->using('App\Models\Administracion\Almacenes\AlmacenLibro');
    }
}

这是我控制器中的代码 Almacen Controller

<?php

namespace App\Http\Controllers\Administracion\Almacenes;

use App\Models\Administracion\Almacenes\Almacen;
use App\User;
use App\Models\Administracion\Almacenes\AlmacenUser;
use App\Models\Administracion\Almacenes\AlmacenLibro;
use Illuminate\Http\Request;
use App\Http\Requests\Administracion\Almacenes\AlmacenFormRequest;
use App\Http\Controllers\Controller;
use DB;
use Barryvdh\DomPDF\Facade as PDF;
use Maatwebsite\Excel\Facades\Excel;
use App\Exports\AlmacenesExport;
use App\Models\Administracion\Almacenes\Libro;
use App\Models\Administracion\Almacenes\NivelLibro;
// use App\AlmacenPrueba;
// use App\LibroPrueba;

class AlmacenesController extends Controller
{

    public function pdfDetalle()
    {
        //Selecciono el almacén a mostrar para la cabecera

        $almacenes = Almacen::with('libros')->get();
        // $almacenes = Almacen::has('libros')->get();
        // $almacenes = Almacen::find(1)->libros();

        dd($almacenes);
        // $libros = $almacenes->libros->toArray();


        $pdf = PDF::loadView('administracion.almacenes.almacenes.almacenesDetallePdf', [
            'almacenes'=>$almacenes,
            // 'libros'=>$libros
            ]);
        return $pdf->download('DetallesAlmacenes.pdf');
    }
}

这是我的视野。 AlmacenesDetalleVista Almacen Vista

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<style>
.page-break {
    page-break-after: always;
}
</style>

<div class="page-break"></div>
<title>Detalle del almacén</title>
</head>

<body>

@foreach ($almacenes->libros as $libro)
  <h1>Detalle del almacén</h1>
    <table>
        <tr>
            <th>Clave</th>
            <th>Descripción</th>
            <th>Calle</th>

        </tr>
        <tr>

            <td> {{$libro->clave }} </td>
            <td> {{$libro->descripcion }} </td>
        </tr>

    </table> 
@endforeach
<div class="page-break"></div>
</body>
</html>

我想要的是从我的商店以及每家商店中获取的每本书籍中的数据。 当我对仓库变量执行dd方法时,我得到了。 dd method

Collection {#824 ▼
  #items: array:5 [▼
    0 => Almacen {#689 ▼
      #table: "almacenes"
      #fillable: array:7 [▶]
      #connection: "mysql"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:10 [▼
        "id" => 1
        "clave" => "Deleg"
        "nombre" => "Delegación"
        "calle" => "Francisco I Madero"
        "colonia" => "Sindurio"
        "cp" => "58500"
        "telefono" => "44332586"
        "estado" => 1
        "created_at" => "2018-09-13 04:05:56"
        "updated_at" => "2018-09-13 04:05:56"
      ]
      #original: array:10 [▶]
      #changes: []
      #casts: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: array:1 [▼
        "libros" => Collection {#822 ▼
          #items: array:3 [▼
            0 => Libro {#803 ▼
              #table: "libros"
              #fillable: array:5 [▶]
              #connection: "mysql"
              #primaryKey: "id"
              #keyType: "int"
              +incrementing: true
              #with: []
              #withCount: []
              #perPage: 15
              +exists: true
              +wasRecentlyCreated: false
              #attributes: array:8 [▼
                "id" => 1
                "clave" => "B1ELP"
                "nombre" => "La Palabra"
                "descripcion" => "Libro que enseña a leer y a escribir a los adultos"
                "imagen" => ""
                "nivelLibro_id" => 1
                "created_at" => "2018-09-13 04:05:55"
                "updated_at" => "2018-09-21 03:45:03"
              ]
              #original: array:13 [▶]
              #changes: []
              #casts: []
              #dates: []
              #dateFormat: null
              #appends: []
              #dispatchesEvents: []
              #observables: []
              #relations: array:1 [▶]
              #touches: []
              +timestamps: true
              #hidden: []
              #visible: []
              #guarded: array:1 [▶]
            }
            1 => Libro {#804 ▶}
            2 => Libro {#805 ▶}
          ]
        }
      ]
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #guarded: array:1 [▶]
    }
    1 => Almacen {#690 ▶}
    2 => Almacen {#691 ▶}
    3 => Almacen {#692 ▶}
    4 => Almacen {#693 ▶}
  ]
}

如何从书本中获取数据? 感谢您的帮助和解答

1 个答案:

答案 0 :(得分:0)

我已经能够解决问题。非常感谢您。 解决方案是执行两次foreach。 视图如下:

<!doctype html>
<html>
<head>
<meta charset="utf-8">

<title>Detalle del almacén</title>
</head>

<body>

@foreach ($almacenes as $almacen)
  <h1>Detalle del almacén</h1>
    <table>
        <tr>
            <th>Clave</th>
            <th>Nombre</th>
            <th>Calle</th>

        </tr>
        <tr>

            <td> {{$almacen->clave }} </td>
            <td> {{$almacen->nombre }} </td>               
            <td> {{$almacen->calle}} </td>
        </tr>

        <tr>
            <th>Clave</th>
            <th>Título</th>
            <th>Nivel</th>
            <th>Stock</th>
        </tr>
        @foreach ($almacen->libros as $libro)
        <tr>
            <td> {{ $libro->clave }} </td>
            <td> {{ $libro->nombre }} </td>
            <td> {{ $libro->nivel->nombre }} </td>
            <td> {{ $libro->pivot->stock }} </td>
        </tr>
        @endforeach
    </table> 

@endforeach

</body>
</html>