[英]How to fetch data from multiple tables while exporting in laravel
我有两张桌子
一个附件请求可以在附件详细信息表中有多个条目。 我正在尝试导出数据,但我获得的数据仅来自附件请求表。
这是我的控制器代码——
public function export()
{
return Excel::download(new AccessoryRequestExport, 'accessory.xlsx');
}
这是我的 AccessoryRequestExport 代码——
<?php
namespace App\Exports;
use App\AccessoryRequest;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\FromArray;
class AccessoryRequestExport implements FromCollection, WithHeadings
{
public function collection()
{
return AccessoryRequest::with('details')->get();
}
public function headings() : array
{
//Put Here Header Name That you want in your excel sheet
return [
'id',
'user_id',
'store_id',
'request_date',
'status',
'created_at',
'updated_at',
'accessory_request_id',
'vendor_id',
'barcode',
'description',
'qty',
'status'
];
}
}
I have listed the relation as "details" which is defined in my AccessoryRequest Model as written below.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class AccessoryRequest extends Model
{
protected $fillable = ['user_id', 'store_id', 'request_date', 'status'];
public function user() {
return $this->belongsTo('App\User', 'user_id');
}
public function store() {
return $this->belongsTo('App\Store', 'store_id');
}
public function details() {
return $this->hasMany('App\AccessoryRequestDetail');
}
public function vendor() {
return $this->belongsTo('App\AccessoryVendor', 'vendor_id');
}
}
请在这件事上给予我帮助 - -
我现在没有这个 Excel 库,但也许您需要使用“详细信息”。 从关系中获取值的标题中的语法。 我的意思是“details.vendor_id”、“details.barcode”之类的。 对于用户,您需要使用AccessoryRequest::with('details', 'user')->get()
然后,如果 'details.' 语法有效,您可以使用“user.user_name”标题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.