laravel how to access column with number name of a table?

后端 未结 12 1265
闹比i
闹比i 2020-12-18 19:11

I make a table with number 22 as the column name. How to access this column?

content:

I\'ve tryed thest

$obj = Tablename::f         


        
相关标签:
12条回答
  • 2020-12-18 19:47

    Try this:

    $obj->getAttributeValue("22");
    

    Please post the error if it doesn't work

    0 讨论(0)
  • 2020-12-18 19:49

    Best way is NOT to use Integer as a fieldname. It is bad praxis. But if you need, you should access the database with raw method:

    public function show()
    {
         $tests = DB::table('test')
            ->select("22 as twentytwo")
            ->get();
        foreach($tests as $test){
            Log::info($test->twentytwo);
        }
    }
    
    0 讨论(0)
  • 2020-12-18 19:52
    $arr= Tablename::where('id', 1)->lists('22', 'id')->toArray();
    $result = $arr[1];
    
    As 1 is the $id var. I tried it in my localhost and it works
    
    0 讨论(0)
  • 2020-12-18 19:54

    Try this:

    $col = '22';
    $res = $obj->{$col};
    var_dump($res);
    
    0 讨论(0)
  • 2020-12-18 19:54

    Try use pluck()

    $plucked = $collection->pluck('22');
    
    $plucked->all();
    
    0 讨论(0)
  • 2020-12-18 19:55

    Try to use where: Tablename::where('22','=','value')->first();

    0 讨论(0)
提交回复
热议问题