laravel如何访问列与数字表的名称? [英] laravel how to access column with number name of a table?

查看:332
本文介绍了laravel如何访问列与数字表的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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





内容:





我试过了

  $ 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`];
$ arr [{'22'}]中的消息未定义索引:的ErrorException; //'语法错误,
中的意外{',expecting']''

/ p>

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

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

输出:

 code> object(Order)#537(21){
[
connection:protected
] => NULL [
table:protected
] => NULL [
primaryKey:protected
] => string(2)id[
perPage:protected
] => int(15)[
incrementing
] => bool(true)[
timestamps
] =>属性: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
] =& 1)3[
trade_status
] => string(13)TRADE_SUCCESS[
express_name
] => string [
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
] =& (1)3[
trade_status
] => string(13)TRADE_SUCCESS[
express_name
] => string [
express_no
] => string(0)[
buyer_email
] => string(0)[
modify_at
] => string(19)2017-01-18 10:54:53[
created_at
] => string(19) 01-18 10:54:53[
updated_at
] => string(19)2017-01-18 10:55:26
} [
bb=> array(0){

} [
hidden:protected
] {

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

} :protected
] => array(0){

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

} [
guarded:protected
] => array(1){
[
0
] => string )*
} [
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 $ b} string(2)25NULLNULL

编辑:acording to



Edit2:
使问题简单明了:



迁移:

 <?php 

使用Illuminate \Database\Schema\Blueprint;
使用Illuminate\Database\Migrations\Migration;

class Test extends Migration {

/ **
*运行迁移。
*
* @return void
* /
public function up()
{
Schema :: create('tests',function )
{
$ table-> increment('id');
$ table-> integer('22');
}
}

/ **
*撤消迁移。
*
* @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));
}
}

资料表:





和日志:

  [2017-02-25 09:16:48] production.INFO:1 [] [] 
[2017-02-25 09:16:48]生产。 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:[] [ ]


解决方案

最好的方法是不要使用Integer作为字段名。这是坏习惯。
但是如果你需要,你应该用原始方法访问数据库:

  public function show b {
$ tests = DB :: table('test')
- > select(22 as twentytwo)
- > get
foreach($ tests as $ test){
Log :: info($ test-> twentytwo);
}
}


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

content:

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

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

data table:

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如何访问列与数字表的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆