laravel 5.5 属性 [libros] 在此集合实例上不存在
laravel 5.5 Property [libros] does not exist on this collection instance
我一直收到一条错误消息,说 属性 [libros] 在这个集合实例上不存在。我究竟做错了什么?检索我的 libros 的所有数据的正确方法是什么?
我尝试了网站上给出的几个答案,例如:
-属性 [stats] 在此集合实例上不存在。
-Laravel - 属性 [name] 在此集合实例上不存在。
- "Property [registration_types] does not exist on this collection instance."
-属性 [comment] 在此集合实例上不存在。
通过 none,我已经能够获得我的结果。
这些是我的模特
Almacen Model
Libro Model
阿尔马森模型
<?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
阿尔马森维斯塔
<!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 ▶}
]
}
如何从我的书中获取数据?
感谢您的帮助和回答
我已经能够解决问题了。无论如何都非常感谢你。
解决方案是执行双 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>
我一直收到一条错误消息,说 属性 [libros] 在这个集合实例上不存在。我究竟做错了什么?检索我的 libros 的所有数据的正确方法是什么?
我尝试了网站上给出的几个答案,例如: -属性 [stats] 在此集合实例上不存在。 -Laravel - 属性 [name] 在此集合实例上不存在。 - "Property [registration_types] does not exist on this collection instance." -属性 [comment] 在此集合实例上不存在。 通过 none,我已经能够获得我的结果。 这些是我的模特 Almacen Model Libro Model 阿尔马森模型
<?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 阿尔马森维斯塔
<!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 ▶}
]
}
如何从我的书中获取数据? 感谢您的帮助和回答
我已经能够解决问题了。无论如何都非常感谢你。 解决方案是执行双 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>