我正在测试我的应用程序的速度,并且我有多个hasOne关系。例如,订单有一种状态(订单待定,订单已发运等)
序模型
<?php
namespace App\Models;
use App\Models\OrderStatus;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Order extends Model
{
protected $primaryKey = 'order_id';
publi
我正在成功地查询跟随和它创建130个查询,我想优化它和减少查询的数量,我已经设置了模型和控制器如下。
后模
class Post extends Eloquent {
public function Categories () {
return $this->belongsToMany('Category', 'category_post');
}
}
范畴模
class Category extends Eloquent {
public function posts () {
return $th
在我的Laravel应用程序中,我有一个页面,在该应用程序中,我正在获取类别,并在该类别中急切地加载事件。效果很好。我现在要做的是获取类别,但这次,根据用户选择的区域/位置获取事件。这些都是我工作的模型;
1.区域类别
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Region extends Model
{
/**
* Get events within region
*
* @return \Illuminate\Database\Eloq
我正试图为我们正在开发的新API建立雄辩的基础。我在一个模型中使用关系。
有些关系很复杂,并不适合快速链式查询生成器语句。例如,我试图返回度量,其中一些度量是复杂的。例如,计算用户生成的总点击量(它不仅仅是一个简单的COUNT(*))。下面是我现在的代码:
<?php
namespace App\Models\Eloquent;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
class Af
有没有办法将这个查询转换成Laravel eloquent格式?
SELECT TEMP.A, COUNT(*) AS Total FROM
(SELECT A FROM TABLE WHERE B='something' GROUP BY C)
AS TEMP GROUP BY TEMP.A
我在这里尝试做的是根据C在某些where子句中的不同记录来获取A的计数。
subselect语句也是一个很有说服力的对象,它由用户在筛选器上的输入构建而成,以获得C输出的独特记录。我尝试这样做,假设subselect eloquent对象是$query
$query2-&
我需要用Eloquent QueryBuilder复制这个查询: select * from table_name
where type = 'custom_type'
and not
(
(topable_type = 'Car' and topable_id = 1)
or
(topable_type = 'Car' and topable_id = 2)
) 问题出在and not部件上。据我所知,Laravel不支持"whereNot“功能。 编辑:代码尝试: $deleteQuery = Item::where('
我希望在Eloquent中有这样的查询: SELECT * FROM table WHERE status = 1 AND (type = 2 OR type = 3 OR type = 4) 我一直无法在Eloquent中找到一种简单的方法来做到这一点。如果我使用 Table::where('status', 1)->orWhere('type', 2)->orWhere('type', 3)... 这可以转化为: SELECT * FROM table WHERE status = 1 OR type = 2 OR type =
我使用的是雄辩的查询生成器,这就是特定查询产生的结果:
select * from `articles` where (select count(*) from `authors` where `authors`.`article_id` = `articles`.`id` and `author` like ?) >= 1 or `title` LIKE ? and `approved` = ? or (select count(*) from `tags` where `tags`.`article_id` = `articles`.`id` and `tag` like ?) &g
我有一个items表,它有一个sequence列,它的值应该是子查询的结果: INSERT INTO items (company_id, sequence, name)
VALUES (1, (
SELECT
COALESCE(max(i.sequence), 0) + 1
FROM
items AS i
WHERE
company_id = 1), 'item 1 of company 1') 但在
好的,我需要在下面用eloquent或Laravel创建一个查询。我只是不确定我该如何创建这样的东西:
select * from
(select * from Bulk
where Login = 'moga' and ApptDate='2015-02-25'
order by FormatTime asc) as A
group by Name
这个查询将在数据库中作为一个原始查询运行得很好,我只是不确定如何使用query helper或eloquent来完成这个查询。
我有这三张桌子和它们的栏:
users
id first_name ...
reports
id user_id title ...
entries
id report_id value
这个查询提供了我想要的内容,它是特定用户的所有条目:
SELECT entries.value from entries JOIN
reports ON (entries.report_id = reports.id) where reports.user_id = 2
我怎么能用Laravel的雄辩的ORM呢?
编辑:
我有这样的模型:
cla
我正在尝试优化我的代码,在其中我生成了过去七天的统计数据。
目前,我对每天记录的数据使用eloquent & querying,这导致我在一个循环中进行7个单独的查询。
示例:
// loop for number of days
for($i = 0; $i < $days; $i++){
// some logic to set comparitive unix times
$oldest = $newest - $dayDuration;
// count number of objects between oldest time and newe
我一直在尝试使用子查询来获得结果。我需要执行两个子查询,但是到目前为止我还没有得到结果。
以下查询将产生所需的结果:
SELECT x.responsible_cook_id, x.d
FROM (
SELECT
responsible_cook_id,
count(*) d
FROM orders
GROUP BY responsible_cook_id
ORDER BY count(*) ASC
) as x
WHERE x.responsible_cook_id IN (
SELECT ID
FROM
我正在尝试解决如何使用用户输入动态创建一个有说服力的查询,根据输入的长度更改查询。使用直接的MySQL很简单...只需根据用户输入连接查询字符串。使用Eloquent是不可能的,除非您a)使用‘原始’sql查询,这违背了使用Eloquent的目的,或者b)使用一些非常不健康的东西,如eval()函数。
如何遍历未知长度的用户输入并创建一个雄辩的查询,该查询使用“OR”和“and”的组合在多个字段中搜索单词
考虑一下下面的伪代码,它实际上不能工作。
$search = "cat sat on the mat";
$searchWords = explode(' '
我试图在Laravel 4中创建一个更高级的查询,该查询由AND和OR where子句组成,格式如下:
(('a','=','1') OR ('b','=','s') OR ('c','=','3')) AND (('d','=','1') OR ('e','=','s') OR ('f','=','3'))
我正在为一个项目制作一个游戏库系统,并试图实现一个搜索功能,这将显示他们正在搜索的用户游戏。
我有两个模型叫做游戏和GameInformation。这是由于许多游戏具有相同的信息,即相同的游戏。迁移文件如下所示:
GameInformation
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateGameInformationTable extend
我想用实际的数据库插入和查询来测试api,所以我尝试在测试前后使用setUpBeforeClass和tearDownAfterClass方法。
<?php
use App\Models\User;
class UserTest extends TestCase {
public static function setUpBeforeClass()
{
User::create([ 'name' => 'Tom' ]);
}
public function testStore()
{
$this->call('PO
我已经这样定义了雄辩的模型:
class Order extends Eloquent {
public function station() {
return $this->belongsTo('Station');
}
}
class Station extends Eloquent {
public function client() {
return $this->belongsTo('Client');
}
public function orders() {
我有这种类型的查询,我想将其转换为Laravel eloquent查询,而不必进行行查询或DB::select。这就是行查询的外观
Where (user_id = '$from' OR to = '$from') AND (user_id = '$to' OR to = '$to')
我如何在eloquent中做到这一点?到目前为止,我刚刚完成了
return $query->where('user_id', $from)->orWhere('to', $from)->get()
我试图将实际的SQL查询输出到屏幕上。我添加了以下路线:
// Display all SQL executed in Eloquent
Event::listen('illuminate.query', function($query)
{
var_dump($query);
});
这主要是起作用的,但有些价值观却成了问号:
select DATE_FORMAT(DATE(`created_at`),'%b %d') as `date`, created_at, COUNT(*) as `count`
from `contacts`
where