Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel在json响应上性能低下_Php_Json_Laravel_Performance - Fatal编程技术网

Php Laravel在json响应上性能低下

Php Laravel在json响应上性能低下,php,json,laravel,performance,Php,Json,Laravel,Performance,在我的Laravel应用程序中,数据通过ajax加载到数据表中。演出很差。所以我创建了一个测试脚本来测量加载时间 public function index() { $mt1 = microtime(true); $data = $this->repo->all(); $resource = ProjectResource::collection($data); $response = respo

在我的Laravel应用程序中,数据通过ajax加载到数据表中。演出很差。所以我创建了一个测试脚本来测量加载时间

    public function index()
    {
        $mt1 = microtime(true);

        $data = $this->repo->all();

        $resource = ProjectResource::collection($data);

        $response  = response()->json($resource);

        $mt2 = microtime(true);

        dd($mt2 - $mt1);

    }
数据库中有200行

该模型有4个关系

上面的脚本渲染数据需要>6秒

如果我取消注释行
$response=response()->json($resource),加载时间<0.2s

有什么可能加快JSON响应的呈现时间

美元数据模型:

附件:

trait ProjectAttribute
{
    public function getActionAttribute()
    {
        return $this->editButton().$this->deleteButton();
    }

    public function editButton()
    {
        if (Auth()->user()->can('update salesOrder')) {
            return '<button data-toggle="tooltip" data-placement="top" title="'.__('buttons.general.crud.edit').'" class="btn btn-info btn-sm editProject mr-1" ><i class="fas fa-pen"></i></button>';
        }
        return "";
    }

    public function deleteButton()
    {
        if (Auth()->user()->can('delete salesOrder')) {
            return '<button data-toggle="tooltip" data-placement="top" title="'.__('buttons.general.crud.delete').'" class="btn btn-danger btn-sm deleteProject" ><i class="fas fa-times"></i></button>';
        }
        return "";
    }

    public function getActiveLabelAttribute()
    {
        if ($this->active) {
            return "<span class='badge badge-info'>Aktiv</span>";
        }
        return "<span class='badge badge-secondary'>Inaktiv</span>";
    }
}
trait项目属性
{
公共函数getActionAttribute()
{
返回$this->editButton()。$this->deleteButton();
}
公共函数editButton()
{
if(Auth()->user()->can('updatesalesforder')){
返回“”;
}
返回“”;
}
公共函数deleteButton()
{
if(Auth()->user()->can('delete salesforder')){
返回“”;
}
返回“”;
}
公共函数getActiveLabelAttribute()
{
如果($this->active){
返回“Aktiv”;
}
返回“Inaktiv”;
}
}
模型:

<?php

namespace App\Models\Project\Project;

use Altek\Accountant\Contracts\Recordable;
use Altek\Accountant\Recordable as RecordableTrait;
use App\Models\Traits\Uuid;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Project extends Model implements Recordable
{
    use ProjectAttribute, ProjectMethod, ProjectRelationship, ProjectScope, Uuid, RecordableTrait, SoftDeletes;


    protected $fillable = [
        'name',
        'description',
        'sales_order_id',
        'project_leader_id',
        'project_type_id',
        'project_status_id'
    ];

    protected $appends = [
        'action',
        'activeLabel',
        'salesOrderName',
        'projectLeaderName',
        'creatorName',
        'statusName',
        'statusLabel',
        'typeLabel'
    ];

    protected $with = ['projectLeader', 'salesOrder', 'projectType', 'projectStatus'];
}

有几种方法可以分析请求,以获取有关服务器响应所需时间的信息

  • -在开发中的Laravel Homestead或生产中的Laravel Forge中默认使用的
  • -也可用于开发(我个人最喜欢的)

这些将为您提供更多关于代码中可能需要花费时间才能完成的部分的信息,如果您搜索Laravel或PHP Profiler以找到适合您需要的内容,还有更多信息。

Check out,这是一个提高Laravel应用程序性能和安全性的工具。它扫描您的代码和服务器配置,提供关于提高性能和安全性的可行建议。

您是否渴望加载关系?是,关系已加载。我刚才在问题中添加了$data模型。您的
$appends
数组中有什么?我在上面的问题中添加了appends。还使用检测未加载关系的N+1查询问题
<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class Project extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'id'                =>      $this->id,
            'name'              =>      $this->name,
            'description'       =>      $this->description,
            'sales_order_name'  =>      $this->salesOrder->name,
            'sales_order'       =>      $this->salesOrder,
            'project_leader'    =>      $this->projectLeader,
            'project_leader_name'=>     $this->projectLeader->full_name,
            'creator'            =>     $this->creator,
            'creator_name'      =>      $this->creator->full_name,
            'type'              =>      $this->projectType,
            'type_name'         =>      $this->projectType->name,
            'type_label'        =>      $this->projectType->typeLabel,
            'status'            =>      $this->projectStatus,
            'status_name'       =>      $this->projectStatus->name,
            'status_label'      =>      $this->projectStatus->statusLabel,
            'created_at'        =>      $this->created_at->format('Y-m-d'),
            'action'            =>      $this->action
        ];
    }
}