猿问

如何避免在不改变php的情况下超过Laravel的60秒的最大执行时间

我的代码有一个问题,我将导出到超过100,000个数据的CSV,这里我已经使用了2次,第一个是变量,其中这个变量在另一个类中采用函数,另一个是我使用时,如果我使用a加载1000个数据,则可以导出数据。是否可以在不更改和仅使用的情况下加载数据?如果可以,我该如何优化我的代码?chunkgetDataAssetRepositoryforeachlimitmax_execute_timephp.inichunk


在这种情况下,我使用的是PostgreSQL。


这是代码AssetRepository.php


class AssetRepository

{

    public $query = null;

    private $trashed = false;


    public static function query()

    {

        $repo = new AssetRepository();

        $repo->query = DB::table('assets as a')

        ->select(

            DB::raw("distinct a.id"),

            "a.id",

            "a.duration as duration",

            DB::raw("COALESCE( NULLIF(a.qr_code,'') , 'QR Code Not Set' ) as qr_code"),

            "a.material_no",

            DB::raw("COALESCE( NULLIF(a.serial_no,'') , 'Serial No Not Set' ) as serial_no"),

            "a.sbu_id",

            "a.pop_id",

            "a.building_id",

            "a.type_id",

            "asset_to_sid.cust_id",

            "a.category_id",

            "a.brand_id",

            "a.model_id",

            "a.id as id",

            "b.name as model",

            "b2.name as brand",

            "p.name as pop",

            "p2.name as sbu",

            "q.name as building",

            "a.updated_at",

            "a.created_at",

            "a.deleted_at",

            'a.eos',

            'a.eol',

            "s.name as sts",

            "c.name as category",

            "a.app_code",

            "a.name",

            "a.status_approval as status_approval",

            "a.approval_notes",

            "a.approval_activities",

            "a.habis_masa_garansi as habis_masa_garansi",

            "permission_approval.action as action_approval",

            DB::raw("CONCAT(u.first_name, ' ', u.last_name) as username"),

            DB::raw("CONCAT(u2.first_name, ' ', u2.last_name) as username2"),

            DB::raw("CONCAT(u3.first_name, ' ', u3.last_name) as approved_by"),

        )

    

HUWWW
浏览 327回答 4
4回答

偶然的你

您可以调用以从执行的其余部分中删除时间限制,也可以在循环的每次迭代中调用(例如)以将计时器重置 n 秒。set_time_limit(0)set_time_limit(n)https://www.php.net/manual/en/function.set-time-limit.php

Smart猫小萌

ini_set('max_execution_time', 180); //3 minutes

绝地无双

最好以异步方式执行这些长时间运行的任务。在Laravel中,您可以使用队列。当队列在 CLI 上运行时,您可以为此配置不同的队列。如果您希望保持执行时间不变,那么您应该尝试将正在执行的任务拆分为多个部分。如果每个部分不超过1分钟,那么您就可以开始了。max_execution_time

幕布斯6054654

也许这个查询返回了这么多元素,PHP大部分时间都花在了将对象包裹在它们周围。如果要查看在查询本身上花费了多少时间,可以直接在 控制台 () 上运行它,或者在代码中使用CollectionPostgreSQL Serverphp artisan tinkerDB::listenpublic function exportAll(Request $request){&nbsp; &nbsp; // PHP >= 7.4.0&nbsp; &nbsp; DB::listen(fn($query) => dump($query->sql, $query->bindings, $query->time));&nbsp; &nbsp; // PHP < 7.4.0&nbsp; &nbsp; DB::listen(function ($query) { dump($query->sql, $query->bindings, $query->time); });&nbsp; &nbsp; ...}如果包装是问题所在,请尝试使用 .它自 .您可以通过调用而不是 来使用它。CollectionLazyCollectionLaravel 6.0$data->cursor()$data->get()A基本上是一个对象,您可以迭代并使用一些方法。它们允许您处理数据,而无需为X行量构建大量行的开销。LazyCollectionCollectionCollection有关惰性集合的更多信息我将重新发布您的函数,并进行一些我认为会对性能产生积极影响的更改。exportAllpublic function exportAll(Request $request){&nbsp; &nbsp; $data = AssetRepository::query(); //From AssetRepository Function&nbsp; &nbsp; $headers = array(&nbsp; &nbsp; &nbsp; &nbsp; 'Content-Type'&nbsp; &nbsp; &nbsp; &nbsp; => 'text/csv',&nbsp; &nbsp; &nbsp; &nbsp; 'Cache-Control'&nbsp; &nbsp; &nbsp; &nbsp;=> 'must-revalidate, post-check=0, pre-check=0',&nbsp; &nbsp; &nbsp; &nbsp; 'Content-Disposition' => 'attachment; filename=export.csv',&nbsp; &nbsp; &nbsp; &nbsp; 'Expires'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=> '0',&nbsp; &nbsp; &nbsp; &nbsp; 'Pragma'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; => 'public',&nbsp; &nbsp; );&nbsp; &nbsp; $response = new StreamedResponse(function () use ($data) {&nbsp; &nbsp; &nbsp; &nbsp; $handle = fopen('php://output', 'w');&nbsp; &nbsp; &nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Use a LazyCollection instead&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* $getData = $data->get();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; &nbsp; &nbsp; $getData = $data->cursor();&nbsp; &nbsp; &nbsp; &nbsp; $remark = Remark::all(['id','label','type']);&nbsp; &nbsp; &nbsp; &nbsp; $remarkAsset = RemarkAsset::all(['asset_id','value','remark_id']);&nbsp; &nbsp; &nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Since we are using a LazyCollection,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* we can't treat $getData as an array directly.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* $getHeader = array_keys((array)$getData[0]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; &nbsp; &nbsp; $getHeader = array_keys((array)$getData->get(0));&nbsp; &nbsp; &nbsp; &nbsp; $newArray = array();&nbsp; &nbsp; &nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* This can be achieved with array_combine&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* $setHeader = array();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* foreach ($getHeader as $header) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; $setHeader[$header] = $header;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; &nbsp; &nbsp; $setHeader = array_combine($getHeader, $getHeader);&nbsp; &nbsp; &nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* $remarkHeader is unnecesary. You can just call $remark->toArray() instead.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Also, what you're trying to do with the following foreach can be done with&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* a combination of array_merge and array_combine&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* $remarkHeader = []; //result&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* foreach ($remark as $headerRemark) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;$remarkHeader[] = array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'id'&nbsp; &nbsp; => $headerRemark['id'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'label' => $headerRemark['label'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'type'&nbsp; => $headerRemark['type']&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;$setHeader[$headerRemark['type']] = $headerRemark['type'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; &nbsp; &nbsp; $setHeader = array_merge(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $setHeader,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array_combine(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $remark->pluck('type')->toArray(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $remark->pluck('type')->toArray()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Again, $remarkAssets is unnecessary. All you're doing with this loop&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* is the same as calling $remarkAsset->toArray()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* $remarkAssets = [];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* foreach ($remarkAsset as $assetRemark) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;$remarkAssets[] = (array)array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'asset_id' => $assetRemark['asset_id'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'value' => $assetRemark['value'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'remark_id' => $assetRemark['remark_id']&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; array_push($newArray, (object)$setHeader);&nbsp; &nbsp; &nbsp; &nbsp; // $coountData = count($getData) / 4;&nbsp; &nbsp; &nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* $getData is already a Collection. Here, you're telling PHP to rebuild it&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* for no reason. For large collections, this adds a lot of overhead.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* You can already call the chunk method on $getData anyways.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* You could do $chunk = $getData->chunk(500) for example.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* It's not even necessary to make a new variable for it since you won't use&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* $chunk again after this.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* $chunk = collect($getData);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* $chunk->chunk(500);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Also, according to the docs you're not using chunk properly.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* https://laravel.com/docs/6.x/collections#method-chunk&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* You're supposed to loop twice because the chunk method doesn't alter the collection.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* If you run&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; $chunk->chunk(500)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; foreach($chunk as $data) { ... }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* You're still looping over the entire Collection.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Since your code is not made to work with chunks, I'll leave it like that&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* foreach ($chunk as $data) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; &nbsp; &nbsp; foreach ($getData as $data) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* This seems to return an array of the keys of $remarkAssets&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* where 'asset_id' is equal to $data->id.&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* You can achieve this through Collection methods on $remarkAsset instead.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* $theKey = array_keys(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;array_combine(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;array_keys($remarkAssets),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;array_column($remarkAssets, 'asset_id')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;$data->id&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Since there is no real need to return an array, I'll leave $theKey as a collection.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $theKey = $remarkAsset->where('asset_id', $data->id)->keys();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Since $remarkHeader doesn't exist in this context, we use $remark instead&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* foreach ($remarkHeader as $head) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Since $theKey is a collection, the count is obtained&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* through the count() Collection method. Also, since you don't&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* ever use $countKey again, you could inline it instead.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;$countKey = count($theKey);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;if ($countKey > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ($remark as $head) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($theKey->count() > 0) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $valueRemark = '';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ($theKey as $key) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Since $remark is a collection and $head an object&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* the following if statement needs to be rewritten&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* if ($remarkAssets[$key]['remark_id'] == $head['id']) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;$valueRemark = $remarkAssets[$key]['value'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($remark->get($key)->remark_id == $head->id) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $valueRemark = $remark->get($key)->value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /**&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* $data being a stdClass, you can just set the property instead of&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* going through the trouble of casting it as an array, setting a value&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* and then re-casting it as an object.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;$data = (array)$data;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;$data[$head['type']] = $valueRemark;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;$data = (object)$data;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;$data = (array)$data;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;$data[$head['type']] = '';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;$data = (object)$data;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $data->{$head['type']} = $valueRemark;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $data->{$head['type']} = '';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array_push($newArray, $data);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $chunkArray = collect($newArray);&nbsp; &nbsp; &nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* As explained earlier, your use of chunk() doesn't do anything.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* We can then safely remove this line.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* $chunkArray->chunk(500);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; &nbsp; &nbsp; foreach ($chunkArray as $datas) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (is_object($datas))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $datas = (array)$datas;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fputcsv($handle, $datas);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; fclose($handle);&nbsp; &nbsp; }, 200, $headers);&nbsp; &nbsp; return $response->send();}没有所有评论public function exportAll(Request $request){&nbsp; &nbsp; $data = AssetRepository::query(); //From AssetRepository Function&nbsp; &nbsp; $headers = array(&nbsp; &nbsp; &nbsp; &nbsp; 'Content-Type'&nbsp; &nbsp; &nbsp; &nbsp; => 'text/csv',&nbsp; &nbsp; &nbsp; &nbsp; 'Cache-Control'&nbsp; &nbsp; &nbsp; &nbsp;=> 'must-revalidate, post-check=0, pre-check=0',&nbsp; &nbsp; &nbsp; &nbsp; 'Content-Disposition' => 'attachment; filename=export.csv',&nbsp; &nbsp; &nbsp; &nbsp; 'Expires'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=> '0',&nbsp; &nbsp; &nbsp; &nbsp; 'Pragma'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; => 'public',&nbsp; &nbsp; );&nbsp; &nbsp; $response = new StreamedResponse(function () use ($data) {&nbsp; &nbsp; &nbsp; &nbsp; $handle = fopen('php://output', 'w');&nbsp; &nbsp; &nbsp; &nbsp; $getData = $data->cursor();&nbsp; &nbsp; &nbsp; &nbsp; $remark = Remark::all(['id','label','type']);&nbsp; &nbsp; &nbsp; &nbsp; $remarkAsset = RemarkAsset::all(['asset_id','value','remark_id']);&nbsp; &nbsp; &nbsp; &nbsp; $getHeader = array_keys((array)$getData->get(0));&nbsp; &nbsp; &nbsp; &nbsp; $newArray = array();&nbsp; &nbsp; &nbsp; &nbsp; $setHeader = array_combine($getHeader, $getHeader);&nbsp; &nbsp; &nbsp; &nbsp; $setHeader = array_merge(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $setHeader,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array_combine(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $remark->pluck('type')->toArray(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $remark->pluck('type')->toArray()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; array_push($newArray, (object)$setHeader);&nbsp; &nbsp; &nbsp; &nbsp; foreach ($getData as $data) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $theKey = $remarkAsset->where('asset_id', $data->id)->keys();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ($remark as $head) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($theKey->count() > 0) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $valueRemark = '';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ($theKey as $key) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($remark->get($key)->remark_id == $head->id) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $valueRemark = $remark->get($key)->value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $data->{$head['type']} = $valueRemark;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $data->{$head['type']} = '';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array_push($newArray, $data);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $chunkArray = collect($newArray);&nbsp; &nbsp; &nbsp; &nbsp; foreach ($chunkArray as $datas) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (is_object($datas))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $datas = (array)$datas;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fputcsv($handle, $datas);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; fclose($handle);&nbsp; &nbsp; }, 200, $headers);&nbsp; &nbsp; return $response->send();}您还可以使用懒惰的集合来备注和备注资产模型,就像这样$remark = Remark::select('id','label','type')->cursor();$remarkAsset = RemarkAsset::select('asset_id','value','remark_id')->cursor();
随时随地看视频慕课网APP
我要回答