问题标签 [laravel-eloquent]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
1095 浏览

laravel - Laravel 调试器显示重复查询

在此处输入图像描述

有没有人对此有任何想法。我不知道为什么它显示重复的查询。我搜索了很多,在stackoverflow上找到了一个答案,但没有得到正确的答案。如果有人遇到同样的错误,请告诉我。谢谢

这是控制器中的代码。与所有观点共享联盟。但是$league = $utility->checkDomainController();中只有一个查询

这是检查域控制器

0 投票
0 回答
28 浏览

laravel-5 - 相关模型的 Laravel 问题

我有三个表:用户、产品和购买

在 Laravel 中,我定义了这些模型:

这个想法是我可以找出哪个用户购买了哪些产品,以便(在我看来)我可以遍历所有产品并指出该用户已经购买了哪些产品。对于这些我会展示“观看这个产品”,对于所有其他(尚未购买的产品)我想展示“购买这个产品”

我目前正在使用以下 SQL 语句

然后我可以遍历product_ids,所有为空的都表示未购买。

但感觉不是最理想的。有人对此有更好的方法吗?

0 投票
1 回答
2215 浏览

php - 在 laravel 上将数百万行数据从一个数据库复制到另一个数据库

使用 Laravel Eloquent,我从旧 Mysql 数据库的一个表中复制 700 万行数据,并将这些行放在新 Mysql 数据库的不同表中。问题是执行此操作需要将近一天的时间,我需要为近 80M 行重新执行此操作。我一次使用 1000 个数据块。有什么方法可以更有效地做到这一点??这是我的代码:

0 投票
2 回答
976 浏览

laravel - Query a relationship that is NOT part of a scope in Laravel

For a model I have a complicated scope condition like so:

This complicated condition will select all the records in Foo that are considered active (the real scope is even more complicated than that).

I have another class like so:

Which means the Bar model has many Foo models.

Now if I wanted to get all the Bar models as well as all the active Foo models that belong to it, I can do the following:

However, how can I write a query that gives me all the Bar records that are NOT active.

Ideally I would want something like this:

0 投票
3 回答
4155 浏览

laravel - Laravel:转换为 JSON 时更改模型中碳日期的格式

目前,当我将模型转换为 JSON 时,所有 Carbon 日期字段都像这样转换:

我希望它使用Atom符号进行转换。这可以在碳中完成,如下所示:

$date日期在哪里Carbon

将模型转换为 JSON 时,如何使模型以 atom 格式转换日期?

我知道可以像这样附加数据:https ://laravel.com/docs/5.3/eloquent-serialization#appending-values-to-json

但这不会改变现有值的格式吗?

0 投票
1 回答
507 浏览

laravel - Laravel Datatables 显示来自相关表的数据

我正在使用数据表来显示数据。它工作得很好,但是当我想显示来自相关表(模型)的数据时,它们不会出现。

模型关键字.php

模型网站.php

“网站”数据库表:id、siteName、siteUrl
“关键字”数据库表:id、website_id、kwName

应该显示数据的 URL:projects/'.$website->id.'/edit(例如:projects/2/edit - (编辑是一个刀片))所以,在这个 Url 我想显示一个显示所有 kwName 的数据表对于某些 website_id(在本例中为 /2/)。

请帮助我应该使用什么以及如何执行此操作。

0 投票
0 回答
580 浏览

php - Laravel 5.1 - Sub query parameter bindings applied in wrong order?

I have the following update query which consists of a sub-query join (I had to use DB:Raw as I was unable to find any documentation to accomplish via any other method - unless someone can show me some other way to achieve it).

However, because the table/model being updated uses timestamps, a value for updated_at is also being added and messes up the order of the parameter bindings?

Below is what I end up with when I die n dump the query log:

As you can see inside the bindings array a value for updated_at is automatically added by eloquent at index 0 and so it's ending up being bound to the first parameter placeholder inside the query which is incorrect.

So instead of WHERE t1.product_id = 1 it's ending up as WHERE t1.product_id = "2017-02-04 09:41:22" and the same for all the other parameters - all in wrong order.

Why is the value for the updated_at column being added first to the bindings array - shouldn't it be added to the end of the array?

How can I fix this?

* UPDATE *

When I check the Illuminate\Database\Eloquent\Builder for the decrement method I see the following:

Arr::add does the following:

So to exclude it from being added by the addUpdatedAtColumn method I've had to include the column within the update query as follows as a workaround:

0 投票
1 回答
1628 浏览

laravel - 如何在 Laravel 5.4 中从一对多多态关系中获取单个记录

我有两个具有One to Many多态关系的模型,例如:

在地址模型中有一个名为的列is_primary,一个用户只能有一个主地址。现在在用户更新表单中,我只想从地址模型中获取主地址。

我知道我可以提取所有地址并检查哪个是主地址或这个:

但问题是表单字段没有得到值,我想访问主地址$user->address_line_1

我怎样才能实现这个目标?

0 投票
4 回答
6642 浏览

php - Eloquent - 多个 WHERE LIKE 条件

我目前正在尝试在我的数据库中搜索关键字。因此,我拆分了一个用逗号分隔的字符串,因此我得到了一个包含可变数量元素(一个关键字)的数组。

我知道我可以使用这样一个雄辩的结构:

用关键字查找产品samsung,galaxy,s7

现在我需要这个东西,但会自动为可变数量的搜索查询部分生成,所以对于数组中的每个关键字,我需要添加一个 ['keywords', 'LIKE', '...']...

我怎样才能用 Laravel Eloquent 做到这一点?

0 投票
1 回答
1016 浏览

php - 使用分页向 Laravel 模型的结果添加键值对

在我的控制器中,我正在调用我的发票模型,该模型正在进行分页选择。在我得到结果之后,我需要遍历每个结果并将 key => value 对添加到原始结果中。我在 config/database.php 中设置了我的 laravel 以将结果作为数组获取:

'fetch' => PDO::FETCH_ASSOC,

我的控制器中的代码是

我的发票模型是:

和我的付款模式

当我尝试将“总计”添加到发票数组时,出现以下错误

间接修改 Illuminate\Pagination\LengthAwarePaginator 的重载元素没有效果