laravel如何访问具有表的编号名称的列?

2022-08-30 19:56:12

我制作了一个以数字22作为列名的表。如何访问此列?

enter image description here

内容:

enter image description here

我试过了

$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

无工作。

编辑为实现的答案:也得到空。

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

输出:

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

编辑:根据Paras的评论

enter image description here

Edit2:使问题简单明了:

迁移:

<?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()
    {
        //
    }

}

型:

<?php
class Test extends Eloquent
{
}

控制器:

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

和日志:

[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:  [] []

答案 1

您可以使用以下语法,如 PHP 文档中的变量变量主题所示:

$obj->{'22'};

...

也可以使用大括号来清楚地分隔属性名称。当访问包含数组的属性中的值时,当属性名称由多个部分组成时,或者当属性名称包含其他方式无效的字符(例如,来自json_decode()或SimpleXML)时,它们最有用。


答案 2

试试这个:

$obj->getAttributeValue("22");

如果错误不起作用,请发布错误