我用数字22做了一个表格作为列名.如何访问此专栏?

enter image description here

content:

enter image description here

I've tryed thest

$obj = Tablename::find(1)->first();
$obj->22;
$obj->'22';   //'syntax error, unexpected ''22''
$obj->"22";
$obj->`22`;
$obj[22];
$arr = $obj->toArray();
var_dump($arr); //  array(15) { ["id"]=> string(2) "25" ["out_trade_no"]=> string(14) "14847080930025" ["22"]=> string(0) "2"
$arr[22];       // 'ErrorException' with message 'Undefined offset: 22'
$arr['22'];     // 'ErrorException' with message 'Undefined offset: 22'
$arr["22"];     // 'ErrorException' with message 'Undefined offset: 22'
$arr[`22`];     // 'ErrorException' with message 'Undefined index: ' in
$arr[{'22'}];   //  'syntax error, unexpected '{', expecting ']'' in

none works.

edited as the answer implemented: also get null.

var_dump($orders[0]);
var_dump($orders[0]->id);
var_dump($orders[0]->{'22'});
$col = '22';
$res = $orders[0]->{$col};
var_dump($res);

output:

object(Order)#537(21){
    [
        "connection": protected
    ]=>NULL[
        "table": protected
    ]=>NULL[
        "primaryKey": protected
    ]=>string(2)"id"[
        "perPage": protected
    ]=>int(15)[
        "incrementing"
    ]=>bool(true)[
        "timestamps"
    ]=>bool(true)[
        "attributes": protected
    ]=>array(15){
        [
            "id"
        ]=>string(2)"25"[
            "out_trade_no"
        ]=>string(14)"14847080930025"[
            "22"
        ]=>string(1)"2"[
            "user_id"
        ]=>string(2)"49"[
            "product_name"
        ]=>string(4)"test"[
            "amount"
        ]=>string(1)"3"[
            "fee"
        ]=>string(4)"0.03"[
            "address_id"
        ]=>string(1)"3"[
            "trade_status"
        ]=>string(13)"TRADE_SUCCESS"[
            "express_name"
        ]=>string(0)""[
            "express_no"
        ]=>string(0)""[
            "buyer_email"
        ]=>string(0)""[
            "modify_at"
        ]=>string(19)"2017-01-18 10:54:53"[
            "created_at"
        ]=>string(19)"2017-01-18 10:54:53"[
            "updated_at"
        ]=>string(19)"2017-01-18 10:55:26"
    }[
        "original": protected
    ]=>array(15){
        [
            "id"
        ]=>string(2)"25"[
            "out_trade_no"
        ]=>string(14)"14847080930025"[
            "22"
        ]=>string(1)"2"[
            "user_id"
        ]=>string(2)"49"[
            "product_name"
        ]=>string(4)"test"[
            "amount"
        ]=>string(1)"3"[
            "fee"
        ]=>string(4)"0.03"[
            "address_id"
        ]=>string(1)"3"[
            "trade_status"
        ]=>string(13)"TRADE_SUCCESS"[
            "express_name"
        ]=>string(0)""[
            "express_no"
        ]=>string(0)""[
            "buyer_email"
        ]=>string(0)""[
            "modify_at"
        ]=>string(19)"2017-01-18 10:54:53"[
            "created_at"
        ]=>string(19)"2017-01-18 10:54:53"[
            "updated_at"
        ]=>string(19)"2017-01-18 10:55:26"
    }[
        "relations": protected
    ]=>array(0){

    }[
        "hidden": protected
    ]=>array(0){

    }[
        "visible": protected
    ]=>array(0){

    }[
        "appends": protected
    ]=>array(0){

    }[
        "fillable": protected
    ]=>array(0){

    }[
        "guarded": protected
    ]=>array(1){
        [
            0
        ]=>string(1)"*"
    }[
        "dates": protected
    ]=>array(0){

    }[
        "touches": protected
    ]=>array(0){

    }[
        "observables": protected
    ]=>array(0){

    }[
        "with": protected
    ]=>array(0){

    }[
        "morphClass": protected
    ]=>NULL[
        "exists"
    ]=>bool(true)[
        "softDelete": protected
    ]=>bool(false)
}string(2)"25"NULLNULL

Edit: acording to Paras's comment

enter image description here

Edit2: to make question simple and clear:

migration:

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class Test extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('tests', function($table)
        {
            $table->increments('id');
            $table->integer('22');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //
    }

}

Model:

<?php
class Test extends Eloquent
{
}

Controller:

public function show()
{
    $tests = Test::all();
    foreach($tests as $test)
    {
        Log::info($test->id);
        Log::info($test->{'22'});
        Log::info($test->{"22"});
        Log::info($test->getAttribute("22"));
    }
}

数据表:

enter image description here

and the log:

[2017-02-25 09:16:48] production.INFO: 1 [] []
[2017-02-25 09:16:48] production.INFO:  [] []
[2017-02-25 09:16:48] production.INFO:  [] []
[2017-02-25 09:16:48] production.INFO:  [] []
[2017-02-25 09:16:48] production.INFO: 2 [] []
[2017-02-25 09:16:48] production.INFO:  [] []
[2017-02-25 09:16:48] production.INFO:  [] []
[2017-02-25 09:16:48] production.INFO:  [] []

推荐答案

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);
    }
}

Laravel相关问答推荐

Laravel带S3存储器

AJAX响应中未定义的全名

在 Laravel 中清除 Routes&config:cache 后未定义的常量

验证错误后的 Laravel 自定义重定向

Laravel:子文件夹中的 lang 文件

Laravel中具有两个外键字段的数据库一对多

我怎样才能了解更多关于我的 Laravel 排队作业(job)失败的原因?

Cookie::forget 不工作 laravel 5.1

在 Laravel 中显示已注册的路由

如何使用中间件将标头添加到响应中?

干预图像:直接从带有原始文件名和分机的网址保存图像?

如何将 Facebook PHP SDK 与 Laravel 5.4 集成?

使用 Laravel 创建新项目会引发异常

laravel 5.1 在没有 VM 重启的情况下看不到作业(job)文件的更改

在 vue.js 中使用 Laravel 的Gate/Authorization

如何以 JSON 格式发送 Laravel 错误响应

Laravel 5如何获取路由动作名称?

如何使用 Laravel 迁移在 Mysql 列中添加注释

如何在没有日志(log)、没有信息的情况下调试 Laravel 错误 500

Eloquent的关系 - attach附加(但不保存)到 Has Many