拉拉维尔 查询值中变量范围的问题丢失.超短代码片段



大家好,美好的一天

这个问题很棘手(对我来说)。我将参数值从控制器传递给模型,模型应该使用此值(保存在变量中),但它丢失了它。我可以回应它确实在查询中具有它,但随着它的进展而丢失。我已经将问题定位到最具体的地方,因此,如果知道答案,则很容易解决它。

最后一行右侧$dato的值显示变量未定义,而上面的行$dato回显正确的值:

<?php
class Treatment extends Eloquent {

    public function sendData($dato){
         echo "en el model dato es: $dato";    <==  IT HAS IT OK HERE 
     $resultado = DB::table('treatments')      <==  IT LOSES ITS VALUE HERE
    ->whereIn('departamento', function($query)
    {
        $query->select(DB::raw('spec_description'))
              ->from('specialties')
              ->whereRaw('id_specialty', '=', $dato); <= SO IT DOES NOT GET IT HERE
    })
    ->get();
        return $resultado;
    }

}

新答案,试试这个,

<?php
class Treatment extends Eloquent {

    public function sendData($dato){
         echo "en el model dato es: $dato";    
     $resultado = DB::table('treatments')
    ->whereIn('departamento', function($query) use($dato)
    {
        $query->select(DB::raw('spec_description'))
              ->from('specialties')
              ->whereRaw('id_specialty = ?',array( $dato));
    })
    ->get();
        return $resultado;
    }
}

我希望这应该有效...

未测试,试试这个,

<?php
class Treatment extends Eloquent {

    public function sendData($dato){
         echo "en el model dato es: $dato";    //  IT HAS IT OK HERE 
     $resultado = DB::table('treatments')
    ->whereIn('departamento', function($query)
    {
        global $dato;
        $query->select(DB::raw('spec_description'))
              ->from('specialties')
              ->whereRaw('id_specialty', '=', $dato); // IT LOSES IT HERE
    })
    ->get();
        return $resultado;
    }

}

最新更新