代码之家  ›  专栏  ›  技术社区  ›  Kevin

如何使用id从数据库中获取数据?

  •  0
  • Kevin  · 技术社区  · 6 年前

    我正在从vue获取id值以模拟“区域”。但在模型中,我不能从db得到值。下面是我用来获取值的代码。

      class Zones extends Model{
        protected $primaryKey = 'zone_id';
        public function getZone($id){
             $zone = Zones::where('zone_id','=',$id)->first();
    
             printf($zone);
             exit(0);
        }
     }
    

    你能帮帮我这里出什么事了吗?

    3 回复  |  直到 6 年前
        1
  •  4
  •   Prashant Pimpale Dila Gurung    6 年前

    使用 print_r()

    class Zones extends Model {
    protected $primaryKey = 'zone_id';
    public function getZone($id){
         $zone = Zones::where('zone_id','=',$id)->first();
    
         print_r($zone);
         exit(0);
       }
    }
    
        2
  •  2
  •   bimal    6 年前
    printf($var) 
    

    它打印普通变量字符串、整数等

    print_r($var);
    

    它打印数组

    $zone是一个数组,因此需要使用print\r

        3
  •  0
  •   Aleksandrs    6 年前

    还有一个dump($your\u data),它只打印而不死亡。

    您还可以使用('zone\u id',$id)而不是('zone\u id','=',$id)。