我有运行良好的Laravel查询生成器代码段:
$records = DB::table('users')
->select(
DB::raw('users.*, activations.id AS activation,
(SELECT roles.name FROM roles
INNER JOIN role_users
ON roles.id = role_users.role_id
WHERE users.id = role_users.user_id LIMIT 1)
AS role')
)
->leftJoin('activations', 'users.id', '=', 'activations.user_id')
->where('users.id', '<>', 1)
->orderBy('last_name')
->orderBy('first_name')
->paginate(10);
Run Code Online (Sandbox Code Playgroud)
有没有一种方法可以避免使用原始查询并获得相同的结果?换句话说,我该如何以更具“查询生成器”的风格编写此代码?我还可以将其转换为口才查询吗?
谢谢
因此,我使用Vue 2.0和Laravel 5.3创建应用程序。
我已经使用Laravel提供的内置分页功能,通过Vue实现了自己的可排序表。
一切工作正常-除了,我试图离开加入“媒体”多态表,以便可以在表中显示图像。
要启用排序,我不得不使用查询生成器,因为您不能使用Eloquent(AFAIK)对关系进行排序。
我的关系定义如下:
$inventories = $inventories->leftjoin('users as inventory_user', 'inventories.user_id', '=', 'inventory_user.id');
$inventories = $inventories->leftjoin('categories as category', 'inventories.category_id', '=', 'category.id');
$inventories = $inventories->leftjoin('inventories as parent', 'inventories.parent_id', '=', 'parent.id');
Run Code Online (Sandbox Code Playgroud)
哪个工作很棒。但是,在不重复(复制)任何行的情况下,我如何精确地加入多态关系?
我有这个:
$inventories = $inventories->leftJoin('media', function($q) {
$q->on('media.model_id', '=', 'inventories.id');
$q->where('media.model_type', '=', 'App\Models\Inventory');
});
Run Code Online (Sandbox Code Playgroud)
哪个显示媒体(如果它在多态表中有关系)。但是,例如,如果我有1个特定库存物品的5张图像(媒体),则我的查询会针对存在的任何媒体重复库存,在我的情况下,它会重复5次。
这是我的选择查询:
$inventories = $inventories->select(DB::raw("
inventories.id as inventory_id,
inventories.name as inventory_name,
inventories.order_column as inventory_order_column,
category.id as category_id,
category.name as category_name,
parent.name as parent_name,
parent.id as parent_id,
inventories.updated_at as inventory_updated_at,
media.name
"));
Run Code Online (Sandbox Code Playgroud)
我想我需要使用groupBy,但是我不确定在哪里定义它。如果我做
$inventories = $inventories->leftJoin('media', …Run Code Online (Sandbox Code Playgroud) Laravel 版本:5.5
PHP版本:7
你好,我想执行这个查询:
select (case
when(title like 'my-keyword') then 1
when(description like 'my-keyword') then 2
) as ordering from products where id > 10;
Run Code Online (Sandbox Code Playgroud)
当我通过查询生成器执行此操作时:
$products = DB::table('products')->select(DB::raw('(case
when(title like "?") then 1
when(description like "?") then 2
) as ordering'))->where('id', '>', 10)->setBindings(['my-keyword', 'my-keyword'])->paginage(10);
Run Code Online (Sandbox Code Playgroud)
这将获得计数,正如我们所知,这将删除所有选择部分并将其替换为 count(*) 作为聚合,因此如果我在此查询生成器上使用 setBindings 并传递 ['my-keyword', 'my-keyword']此聚合查询将更改为:
select count(*) as aggregate from products where id > my-keyword;
Run Code Online (Sandbox Code Playgroud)
因此,这将导致在此查询和其他类似查询的替代方案上使用分页的问题!
为了解决这个问题,我更改了/..../Query/Builder.php中的一些代码:
$total = $this->getCountForPagination($columns);
Run Code Online (Sandbox Code Playgroud)
对此:
$all = $this->get();
$total = $all->count();
Run Code Online (Sandbox Code Playgroud)
对于这种情况,我知道这是错误的,但现在它有效!
我该怎么做才能以正确的方式解决这个问题?
任何人都可以帮我写下面的SQL语句的数据库查询版本.我需要一些关于select语句和分区连接的帮助.
到目前为止,我已设法做到这一点.
$query = DB::table(raw('SapakInAdminOrder a'))
->select(raw('a.*'))
->leftJoin(raw('currency cu'), 'a.currency', '=', 'cu.id')
->leftJoin(raw('moodboards m'), 'a.orderMoodboardID', '=', 'm.id')
->join(raw('clients b'), 'a.clientID', '=', 'b.id')
->leftJoin(raw('moodboards mc'), 'b.moodboardID', 'mc.id')
->join(raw('sapakim c'), 'b.sapakID', '=', 'c.id')
->leftJoin(raw('sapakim sm'), 'c.managerid', '=', 'sm.id')
->leftJoin(raw('products p'), 'a.productKey', '=', 'p.id')
->where(function ($query) {
$query->whereNull('a.isDeleted');
$query->orWhere('a.isDeleted', '!=', 1);
});
Run Code Online (Sandbox Code Playgroud)
但我需要实现这一目标.
select * from (select ROW_NUMBER() OVER(ORDER BY case when (indesign.status=4 or indesign.statusdate is null) then getdate()+2 else indesign.statusdate end ASC) AS RowNum,a.*
FROM sapakInAdminOrder a
left join currency cu on …Run Code Online (Sandbox Code Playgroud) 我有一个可以免费访问的课程表,或者管理员需要单击某些内容才能让用户查看课程。
该course表如下所示:
| id | title | invite_only |
|----|----------------|-------------|
| 1 | free course | 0 |
| 2 | private course | 1 |
Run Code Online (Sandbox Code Playgroud)
除此之外,我有一个course_user表,最初用户请求访问,然后管理员可以批准或拒绝访问:
| id | user_id | course_id | approved | declined |
|----|---------|-----------|----------|----------|
| 1 | 3 | 2 | 1 | 0 |
| 2 | 4 | 1 | 0 | 1 |
| 3 | 4 | 2 | 0 | 0 |
Run Code Online (Sandbox Code Playgroud)
我想索引用户有权访问的所有课程:
class …Run Code Online (Sandbox Code Playgroud) Laravel 版本 7.0
我有Team模型和User模型,team_has_users表格。
team_has_users表有team_id, user_id,role列。
一名用户可以属于一个具有不同角色的团队。
例如,一个用户可以作为客户和员工属于一个团队。
在Team模型中,我设置了这样的关系。
public function users(){
return $this->belongsToMany(User::class, 'team_has_user', 'team_id', 'user_id')
->withPivot('role');
}
Run Code Online (Sandbox Code Playgroud)
当我将用户加入团队时,效果很好。
$item->users()->attach($request->clients, ['role'=>'client']);
$item->users()->attach($request->employees, ['role'=>'employee']);
Run Code Online (Sandbox Code Playgroud)
但是,当我要同步它们时,我却做不到。
我尝试搜索并找到了类似的,syncwithoutDetaching但它似乎不适合我的情况。
team_has_users表可以是这样的。
team_id user_id role
1 1 client
1 1 employee
1 2 client
1 1 other
...
Run Code Online (Sandbox Code Playgroud)
谁能帮我?
谢谢你!
php laravel eloquent laravel-query-builder laravel-relations
根据 Laravel 文档,可以通过将 Closure 作为第一个参数传递给 orWhere 方法来对“or”条件进行分组:
$users = DB::table('users')
->where('votes', '>', 100)
->orWhere(function($query) {
$query->where('name', 'Abigail')
->where('votes', '>', 50);
})
->get();
Run Code Online (Sandbox Code Playgroud)
我想要的是在查询中使用一个变量,它看起来像:
$q = $request->get('query');
$users = DB::table('users')
->where('votes', '>', 100)
->orWhere(function($query) {
$query->where('name', $q)
->where('votes', '>', 50);
})
->get();
Run Code Online (Sandbox Code Playgroud)
我尝试将其作为第二个参数传递,例如:
$q = $request->get('query');
$users = DB::table('users')
->where('votes', '>', 100)
->orWhere($q, function($query, $q) {
$query->where('name', $q)
->where('votes', '>', 50);
})
->get();
Run Code Online (Sandbox Code Playgroud)
但它不起作用,有什么帮助吗?
这是我尝试分页的方式:
$posts = Post::all()->sortByDesc("created_at")->pagination(1);
Run Code Online (Sandbox Code Playgroud)
但我收到这个错误:
方法 Illuminate\Database\Eloquent\Collection::pagination 不存在。
model-view-controller pagination laravel laravel-collection laravel-query-builder
我在 SQL 中有这样的产品表:
productID Type Amount
1046 1 4
1046 1 5
1046 2 10
1047 1 5
1047 2 3
Run Code Online (Sandbox Code Playgroud)
如何进行查询,输出如下:
productID Type TotalTypeAmount TotalPerProductID
1046 1 9 19
1046 2 10 19
1047 1 5 8
1047 2 3 8
Run Code Online (Sandbox Code Playgroud) laravel-5.7/mysql\nRun Code Online (Sandbox Code Playgroud)\n\n在我的数据库中,我有一个如下所示的 json 格式字段:
\n\n字段名称:特征:
\n\n[\n {"id": 1, "url": null, "name": "A"}, \n {"id": 2, "url": null, "name": "B"}\n]\nRun Code Online (Sandbox Code Playgroud)\n\n同样在它的模型中,我写了这个
\n\n protected $casts = [\n \'features\' => \'array\'\n ];\nRun Code Online (Sandbox Code Playgroud)\n\n现在我创建一个数组:
\n\n$features = array();\n\ntemp = array();\ntemp[\'id\'] = 1;\ntemp[\'url\'] = null;\ntemp[\'name\'] = A;\n$features[] = temp;\n\ntemp = array();\ntemp[\'id\'] = 2;\ntemp[\'url\'] = null;\ntemp[\'name\'] = B;\n$features[] = temp;\nRun Code Online (Sandbox Code Playgroud)\n\n如何$features array 与features field 数据库中的进行比较?
\xd9\x91我检查了这些:
\n\n$fff = \\App\\Cart::whereFeatures($features)->get()->first();\nRun Code Online (Sandbox Code Playgroud)\n\n或者
\n\n$fff = …Run Code Online (Sandbox Code Playgroud) laravel ×8
php ×4
eloquent ×2
laravel-5 ×2
sql ×2
laravel-5.3 ×1
mysql ×1
pagination ×1
sql-server ×1
vue.js ×1