小编Dev*_*evK的帖子

Laravel 5.3 - 如何记录页面上的所有查询?

我和我的团队正在开展一个相当大的项目.随处可见的查询 - 在控制器中,在视图中的视图作曲家(延迟加载)以及可能在其他一些服务中.现在越来越难以跟踪这一切,目前页面加载速度相当慢.

我将把\ DB :: enableQueryLog()和\ DB :: getQueryLog()放在哪里记录所有查询并转储它们?基本上我正在寻找在任何查询发生之前发生的代码中的某个位置(放置enableQueryLog())并且我正在寻找在视图呈现之后发生的位置(转储getQueryLog()).

有什么好办法可以解决这个问题?

提前致谢.

php laravel laravel-5.3

18
推荐指数
2
解决办法
2万
查看次数

Laravel 5.2视图编辑器被多次执行

我正在处理的项目中存在一些(主要)性能问题,并且在记录了所有执行的查询后,我意识到其中许多执行多次,我无法找到问题的根源.

多次执行的所有查询都在我的视图编写器提供程序中.

这就是我的视图作曲家的样子:

public function boot()
    {
        view()->composer('partials.sidebar', function ($view) {
            $currentCategory = $this->getCurrentCategory();
            $sidebarCategories = SidebarCategory::category($currentCategory)
                ->order()
                ->get();
            $view
                ->with('sidebarCategories', $sidebarCategories);
        });

        view()->composer('partials.footer', function ($view) {
            $footerLinks = FooterCategory::with('links.translations')->order()->get();
            $footerColumn1 = $footerLinks->filter(function ($value, $key) {
                return $value->column == 1;
            });
            $footerColumn2 = $footerLinks->filter(function ($value, $key) {
                return $value->column == 2;
            });
            $footerColumn3 = $footerLinks->filter(function ($value, $key) {
                return $value->column == 3;
            });
            $footerColumn4 = $footerLinks->filter(function ($value, $key) {
                return $value->column == 4;
            });

            $view
                ->with(compact('footerColumn1', 'footerColumn2', 'footerColumn3', 'footerColumn4'));
        }); …
Run Code Online (Sandbox Code Playgroud)

php laravel laravel-5

9
推荐指数
1
解决办法
755
查看次数

Laravel 5.2查询范围内的多态关系

我有一对多的多态关系,完全像文档中描述的那样,除了我在投票表中有投票而不是照片和整数投票列.像那样:

-votes
--id
--vote
--voteable_id
--voteable_type
Run Code Online (Sandbox Code Playgroud)

投票模型具有可投票的方法:

public function voteable()
{
    return $this->morphTo();
}
Run Code Online (Sandbox Code Playgroud)

但是对于我希望附加投票的模型(本例中的页面),我创建了一个VoteableTrait,它具有votes()morphMany方法和我试图开始工作的范围.范围将添加属于某个模型的所有投票的总和,并按总和对父模型进行排序.我试过的范围:

public function scopeWithVotesTrait($query)
{
    return $query->with(['votes' => function($q){
        $q->select(\DB::raw("SUM(vote) AS votes"), 'voteable_id')->first();
    }]);
}
Run Code Online (Sandbox Code Playgroud)

但是,由于Laravel执行2个不同的查询,一个用于父模型,一个用于投票,因此我无法订购.Page :: withVotesTrait() - > first()的结果将投票放在一个数组中:

{
"id": 1,
    "votes": [
    {
        "votes": "-1",
        "voteable_id": 1
    }
    ]
}
Run Code Online (Sandbox Code Playgroud)

我的另一个想法是执行连接而不是 - > with(),如下所示:

public function scopeWithVotesTrait($query, $model, $table)
{
    return $query->join('votes', function($join) use($model, $table)
    {
        $join->on('votes.voteable_id', '=', $table . '.id')
            ->where('votes.voteable_type', '=', $model);
    })->select('*', \DB::raw('sum(vote) as votes')) …
Run Code Online (Sandbox Code Playgroud)

php laravel eloquent laravel-5.2

7
推荐指数
1
解决办法
1747
查看次数

MySQL 5.7 - 增加 JSON 中的数字

是否可以批量增加特定的 JSON 属性?

例如,我的 JSON 列被称为metadata,如下所示:

{"counter": 0, ...}
Run Code Online (Sandbox Code Playgroud)

我可以更新多行,以便通过单个查询使计数器递增 1 吗?

编辑:

由于这被标记为过于宽泛,让我举一个确切的例子。

表上的数据x,其上唯一的列是metadata,它有 3 行:

{"counter": 0}, {"counter": 1},{"counter": 0, "something": "somethign"}

期望的结果是一个查询将这 3 个更新为:

{"counter": 1}, {"counter": 2},{"counter": 1, "something": "somethign"}

不用说,我不是 MySQL 专家,我也没有设法从 MySQL 文档中找到适合我的查询。如有帮助,将不胜感激。

mysql sql

6
推荐指数
1
解决办法
2096
查看次数

Golang - 为什么这个函数会改变输入参数?

我正在学习Golang,我来自PHP背景.我有时难以理解一些核心功能.

具体来说,现在我建立一个游戏式炉排和我创建了一个CardStack类型,有一些方便的方法在卡片堆栈一个可以使用(读:玩家的手牌,弃牌堆...),例如DrawCards(...),AppendCards(...)...

我遇到的问题是函数func (c* CardStack) DrawCards(cards []deck.Card) ([]deck.Card, error) {...}改变了参数cards []deck.Card,我无法弄清楚为什么或如何避免这种情况.

这是我的CardStack:

type CardStack struct {
    cards []deck.Card
}
Run Code Online (Sandbox Code Playgroud)

这是我的DrawCards方法:

func (c *CardStack) DrawCards(cards []deck.Card) ([]deck.Card, error) {
    return c.getCardsSlice(cards, true)
}

// Returns cards that are missing
func (c *CardStack) getCardsSlice(cards []deck.Card, rm bool) ([]deck.Card, error) {
    var err error
    var returnc = []deck.Card{}
    for _, card := range cards {
        fmt.Println("BEFORE c.findCard(cards): ")
        deck.PrintCards(cards) // In …
Run Code Online (Sandbox Code Playgroud)

go

3
推荐指数
1
解决办法
337
查看次数

使用相关模型的Count加载laravel eloquent模型

鉴于我有两个雄辩的模型:预订和客户。

当我将所有预订与相应客户一起列出时,我还想显示相应客户的总预订量(此预订的数量 + 所有其他预订)。

示例输出:

  • 预订 1:客户 A(总共有 20 个预订)
  • 预订2:客户B(总共有10个预订)
  • 预订3:客户C(VIP:总共有100个预订)

为了避免 n+1 问题(显示此内容时每个预订额外查询一个),我想bookingsCount为客户急切加载。

关系是:

预订: public function customer() { return $this->belongsTo(Customer::class) }

顾客: public function bookings() { return $this->hasMany(Booking::class) }

使用预先加载查询预订的示例

工作,但没有急切地加载bookingsCount:

Booking::whereNotCancelled()->with('customer')->get();
Run Code Online (Sandbox Code Playgroud)

不工作:

Booking::whereNotCancelled()->with('customer')->withCount('customer.bookings')->get();
Run Code Online (Sandbox Code Playgroud)

我了解到,您不能withCount在相关模型的字段上使用,但您可以创建一个hasManyThrough关系并调用withCount该关系,例如Booking::whereNotCancelled()->withCount('customerBookings');请参阅此处接受的答案)。

但是:这不起作用。我想,这是因为一个预订属于一个客户,而一个客户有许多预订。

这是类 Booking 的 hasManyThrough 关系

public function customerBookings()
{
    // return the bookings of this booking's customer
    return $this->hasManyThrough(Booking::class, Customer::class);
}
Run Code Online (Sandbox Code Playgroud)

这是 hasManyThrough 的失败测试

/**
 * @test …
Run Code Online (Sandbox Code Playgroud)

php eager-loading laravel eloquent

3
推荐指数
1
解决办法
2252
查看次数

如何在事件侦听器中获取经过身份验证的用户-Laravel

我正在尝试确定在事件侦听器中获取经过身份验证的用户信息的正确方法。

我总是可以使用\Auth:user()似乎有效的方法,但是如果我将侦听器排队,我认为它不会起作用。

我是否需要将经过身份验证的用户作为每个事件的属性添加到事件类中,还是有其他方法可以执行?

php laravel laravel-5.6

2
推荐指数
1
解决办法
661
查看次数

laravel 5 组路由

我想创建这样的网址

http://example.com/url1/product/blabla http://example.com/url2/product/aiueo

我无法做到这一点

Route::prefix('{page}')->name('the_url')->group(function () {
    Route::get('/', 'HomeController@page')
        ->name('index');

})->where('page', '(url1|url2)');
Run Code Online (Sandbox Code Playgroud)

或者我应该where(){page}前缀内的每个方法中使用?

php laravel laravel-5

2
推荐指数
1
解决办法
2199
查看次数

使用 Laravel 5.6 api 验证 JSON Post

在 Laravel 5.6 上验证 API 的 JSON 输入时遇到了困难。我一直在尝试如何在 Laravel 中获取和验证应用程序/json 数据的解决方案 但仍然没有解决。

配套类:

<?php
namespace App\Http\Controllers\API;

class ResponseObject
{
    const status_ok = "OK";
    const status_fail = "FAIL";
    const code_ok = 200;
    const code_failed = 400;
    const code_unauthorized = 403;
    const code_not_found = 404;
    const code_error = 500;

    public $status;
    public $code;
    public $messages = array();
    public $result = array();
}
Run Code Online (Sandbox Code Playgroud)

和控制器:

namespace App\Http\Controllers\API;

use App\Http\Resources\MyItemsResource;
use App\MyItem;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use \Illuminate\Http\Response;
use \Illuminate\Support\Facades\Response as FacadeResponse; …
Run Code Online (Sandbox Code Playgroud)

php laravel laravel-5.6

1
推荐指数
1
解决办法
5193
查看次数