Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.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
Laravel模型使用postgresql`subpath`函数为ltrees获取子路径_Postgresql_Eloquent_Laravel 5.6 - Fatal编程技术网

Laravel模型使用postgresql`subpath`函数为ltrees获取子路径

Laravel模型使用postgresql`subpath`函数为ltrees获取子路径,postgresql,eloquent,laravel-5.6,Postgresql,Eloquent,Laravel 5.6,我在名为breadcrumbs的postgresql服务器上有下表: id序列号 路径VARCHAR(300) 使用以下示例数据: id | path --- | --- 1 | animes.like.hentai 2 | animes.dislike.hentai 3 | animes.like.shonen 4 | animes.like.action 并使用以下雄辩的模型进行建模: <?php declare(strict_types=1); namespace App\M

我在名为
breadcrumbs
的postgresql服务器上有下表:


id序列号
路径VARCHAR(300)

使用以下示例数据:

id | path
--- | ---
1 | animes.like.hentai
2 | animes.dislike.hentai
3 | animes.like.shonen
4 | animes.like.action
并使用以下雄辩的模型进行建模:

<?php declare(strict_types=1);

namespace App\Model;
use Illuminate\Database\Eloquent\Model as BaseModel;

class Breadcrumbs extends BaseModel
{
   protected $table = 'breadcrumbs';

   // 
}
到目前为止,我尝试使用以下代码:

Breadcrumbs::where("subpath(path, 0, 1)","hentai")->all();
但我得到了以下错误:

Illuminate/Database/QueryException with message 'SQLSTATE[42703]: Undefined column: 7 ERROR:  column "subpath(path, 0, 1)" does not exist
LINE 1: select * from "breadcrumbs" where "subpath(path, 0, 1)"...
                                               ^ (SQL: select * from "breadcrumbs" where "subpath(path, 0, 1)" = hentai '
这意味着
where()
方法的第一个参数是自动引用的,假设它是一个列而不是一个函数

因此,我如何扩展模型的查询生成器或按原样使用上述代码来执行正确的选择?

如图所示,同样的原则也适用于此处,而不是:

Breadcrumbs::where("subpath(path, 0, 1)","hentai")->all();
使用:

如文档中所示,您可以使用
DB:raw
来绕过将列名作为第一个参数的限制

Breadcrumbs::where("subpath(path, 0, 1)","hentai")->all();
Breadcrumbs::where(DB::raw("subpath(path, 0, 1)"),"hentai")->all();