OctoberCMS - 具有关系manyToMany的模型查询 [英] OctoberCMS - Model query with relation manyToMany

查看:202
本文介绍了OctoberCMS - 具有关系manyToMany的模型查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我再次要求社区的一些帮助...
方案:



我有两个表,一个数据透视表和标签给出一些上下文):

 表1(Evento):
-id
- 。 ...

表2(Etiquetas):
-id
-etiqueta

数据透视表(Eventos_Etiquetas):
-evento_id pk)
-etiqueta_id(pk)

关系设置为:

  public $ belongsToMany = [
'eventos'=> ['JML\Gkb\Models\Evento','table'=> 'jml_gkb_eventos_etiquetas']
];

  public $ belongsToMany = [
'etiquetas'=> ['JML\Gkb\Models\Etiqueta','table'=> 'jml_gkb_eventos_etiquetas']
];

现在,我想实现:



-get所有有任何单独标签的事件,而不涉及输入(或)的顺序。



- 将所有标签的所有事件都与输入顺序相关联(和)。



正如你所想象的那样,我很沮丧,因为我是新来的十月/ Laravel查询构建器(而不是sql)。 p>

我目前为止做了什么:

  if(Session get('tipo')=='etiqueta'){
$ pesquisa = preg_split('/ \s + /',$ temp,-1,PREG_SPLIT_NO_EMPTY);
if(Session :: get('modo')== 0){
$ this ['records'] = Evento :: with('etiquetas') - > whereHas('etiquetas'函数($ query)use($ pesquisa){
foreach($ pesquisa as $ palavra){
$ query-> where('etiqueta','like',%$ palavra% ;
}
}) - > orderBy('id','DESC') - > paginate(25);
}
if(Session :: get('modo')== 1){
$ this ['records'] = Evento :: with('etiquetas') - > whereHas ('etiquetas',function($ query)use($ pesquisa){
foreach($ pesquisa as $ palavra){
$ query-> orWhere('etiqueta','like',% $ palavra%);
}
}) - > orderBy('id','DESC') - > paginate(25);
}
}

用户输入由$ temp变量传递,分开到$ pesquisa数组变量的单词。 'modo'定义用户是否假装AND(0)或OR(1)进行搜索。基于这个选择,构建了一个查询来尝试使用$ palasra变量来获取结果作为$ pesquisa的任何单词。



结果如下:



在modo == 0中,我只能得到一个用户输入标签的事件,如果它有多个单词(第一个字上不存在的任何字母)



在modo == 1中,它获取所有事件。



在这两种情况下,我都会没有任何标签(etiqueta)的任何事件 - 正确的行为。



我尝试过其他一些方法,但无济于事...这一个看到我最合乎逻辑的尝试...有人可以指出我正确的方向吗?



TIA



JL

解决方案

经过一些尝试,我有一半的问题解决了。我想要获得任何Eventiqu的部分,在用户输入上没有关于它们的顺序的任何Etiqueta都可以正常工作。



简历

  if(Session :: get('tipo')=='etiqueta'){
$ pesquisa = preg_split ('/ \s + /',$ temp,-1,PREG_SPLIT_NO_EMPTY);
$ cadeiapesquisa = implode('|',$ pesquisa);
/ ***** NOT WORKING YET ***** /
if(Session :: get('modo')== 0){
$ this ['records'] = Evento :: with('etiquetas') - > whereHas('etiquetas',function($ query)use($ pesquisa){
foreach($ pesquisa as $ palavra){
$ query- > where('etiqueta','like','%$ palavra%));
}
}) - > orderBy('id','DESC') - > paginate );
}
/ ******这是工作精细! ******* /
if(Session :: get('modo')== 1){
if(count($ pesquisa)> 0&& ==
$ this ['records'] = Evento :: with('etiquetas') - > whereHas('etiquetas',function($ query)use($ cadeiapesquisa){

$ query-> where('etiqueta','regexp',$ cadeiapesquisa);

}) - > orderBy('id','DESC') - >分页(25);
} else {
Evento :: paginate(25);
}
}
}

但我会到那里:)



TIA



JL





问题解决...由于我所做的测试,迄今为止所产生的代码的狙击手是如下:


$ b $ {pre> if(Session :: get('tipo')=='etiqueta'){
$ pesquisa = preg_split('/ \s + /',$ temp,-1,PREG_SPLIT_NO_EMPTY);
$ cadeiapesquisa = implode('|',$ pesquisa);
$ contagem = count($ pesquisa);
if(Session :: get('modo')== 0){
if(strlen($ cadeiapesquisa)> 0){
$ this ['records'] = Evento: :with('etiquetas') - > whereHas('etiquetas',function($ query)use($ cadeiapesquisa,$ contagem){

$ query-> where('etiqueta',' regexp',$ cadeiapesquisa) - > groupBy('evento_id') - 具有(DB :: raw(COUNT('etiqueta_id')),'> =',$ contagem);

}) - > paginate(25);
} else {
$ this ['records'] = Evento :: paginate(25);
}
}
if(Session :: get('modo')== 1){
if(strlen($ cadeiapesquisa)> 0){
$ this ['records'] = Evento :: with('etiquetas') - > whereHas('etiquetas',function($ query)use($ cadeiapesquisa){

$ query->其中('etiqueta','regexp',$ cadeiapesquisa);

}) - > paginate(25);
} else {
$ this ['records'] = Evento :: paginate(25);
}
}
}

JL


Once again i'm asking some help from the community... Scenario:

I have two tables with one pivot table (something like posts and tags to give some context):

    table 1 (Evento):
        -id
        -....

    table 2 (Etiquetas):
        -id
        -etiqueta

    pivot table (Eventos_Etiquetas):
        -evento_id (pk)
        -etiqueta_id (pk)

The relations are set as:

public $belongsToMany = [
    'eventos' => ['JML\Gkb\Models\Evento', 'table' => 'jml_gkb_eventos_etiquetas']
];

and

public $belongsToMany = [
    'etiquetas' => ['JML\Gkb\Models\Etiqueta', 'table' => 'jml_gkb_eventos_etiquetas']
];

Now, what i want to achieve:

-get all events that have any individual tag without regarding the order of input (or).

-get all events that have all tags without regarding the order of input (and).

As you can imagine i'm strugling with this as i'm new to October/Laravel query builder (and not so to sql).

What i've done so far:

if (Session::get('tipo') == 'etiqueta'){
    $pesquisa = preg_split('/\s+/', $temp, -1, PREG_SPLIT_NO_EMPTY);
    if (Session::get('modo') == 0){
        $this['records'] = Evento::with('etiquetas')->whereHas('etiquetas', function($query) use ($pesquisa){
            foreach ($pesquisa as $palavra){
                $query->where('etiqueta', 'like', "%$palavra%");
            }
        })->orderBy('id', 'DESC')->paginate(25);
    }
    if (Session::get('modo') == 1){
        $this['records'] = Evento::with('etiquetas')->whereHas('etiquetas', function($query) use ($pesquisa){
            foreach ($pesquisa as $palavra){
                $query->orWhere('etiqueta', 'like', "%$palavra%");
            }
        })->orderBy('id', 'DESC')->paginate(25);
    }
}

The user input is passed by $temp variable and it's splited to words to the $pesquisa array variable. 'modo' defines if the user pretend a search by AND (0) or by OR (1). Based on that choice a query is built to try to get the results using $palavra variable as any word of $pesquisa.

The result of this:

In modo == 0, i only can get the events of one tag of user input, if it have more than one word (any letter that don't exist on first word) don't get any result.

In modo == 1 it gets all events.

In both cases i don't get any event that don't have any tag (etiqueta) - correct behaviour.

I've tried some other ways but with no avail... This one looks to me the most logical of the tries... Can someone point me on the correct direction ?

TIA

JL

解决方案

After some tries i have half of the problem solved. The part where i want to get any "Evento" that has any of the "Etiqueta" on user input not regarding the order of them is working fine finally.

Resume bellow

    if (Session::get('tipo') == 'etiqueta'){
    $pesquisa = preg_split('/\s+/', $temp, -1, PREG_SPLIT_NO_EMPTY);
    $cadeiapesquisa = implode('|', $pesquisa);
 /***** NOT WORKING YET *****/
    if (Session::get('modo') == 0){
        $this['records'] = Evento::with('etiquetas')->whereHas('etiquetas', function($query) use ($pesquisa){
            foreach ($pesquisa as $palavra){
                $query->where('etiqueta', 'like', "%$palavra%");
            }
        })->orderBy('id', 'DESC')->paginate(25);
    }
/****** THIS IS WORKING FINE ! *******/
    if (Session::get('modo') == 1){
        if ( count ($pesquisa) > 0 && !($temp == null)){
            $this['records'] = Evento::with('etiquetas')->whereHas('etiquetas', function($query) use ($cadeiapesquisa){

                $query->where('etiqueta', 'regexp', "$cadeiapesquisa");

            })->orderBy('id', 'DESC')->paginate(25);
        } else {
            Evento::paginate(25);
        }
    }
}

The first parte is fighting, but i will get there. :)

TIA

JL

[EDIT]

Problem solved... The resulting snipet of code working so far with the tests i've done is bellow:

    if (Session::get('tipo') == 'etiqueta'){
    $pesquisa = preg_split('/\s+/', $temp, -1, PREG_SPLIT_NO_EMPTY);
    $cadeiapesquisa = implode('|', $pesquisa);
    $contagem = count($pesquisa);
    if (Session::get('modo') == 0){
        if ( strlen($cadeiapesquisa) > 0 ){
            $this['records'] = Evento::with('etiquetas')->whereHas('etiquetas', function($query) use ($cadeiapesquisa, $contagem){

                $query->where('etiqueta', 'regexp', "$cadeiapesquisa")->groupBy('evento_id')->having(DB::raw("COUNT('etiqueta_id')"), '>=', $contagem );

            })->paginate(25);
        } else {
            $this['records'] = Evento::paginate(25);
        }
    }
    if (Session::get('modo') == 1){
        if ( strlen($cadeiapesquisa) > 0 ){
            $this['records'] = Evento::with('etiquetas')->whereHas('etiquetas', function($query) use ($cadeiapesquisa){

                $query->where('etiqueta', 'regexp', "$cadeiapesquisa");

            })->paginate(25);
        } else {
            $this['records'] = Evento::paginate(25);
        }
    }
}

JL

这篇关于OctoberCMS - 具有关系manyToMany的模型查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆