laravel 如何访问带有表编号名称的列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41711470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
laravel how to access column with number name of a table?
提问by Kris Roofe
I make a table with number 22 as the column name. How to access this column?
我制作了一个以数字 22 作为列名的表格。如何访问此列?
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:
Edit2:使问题简单明了:
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: [] []
采纳答案by DvdEnde
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:
最好的方法是不要使用 Integer 作为字段名。这是不好的做法。但如果需要,您应该使用原始方法访问数据库:
public function show()
{
$tests = DB::table('test')
->select("22 as twentytwo")
->get();
foreach($tests as $test){
Log::info($test->twentytwo);
}
}
回答by Marty
You can use the following syntax, as found in the variable variables topicin the PHP documentation:
您可以使用以下语法,如PHP 文档中的变量变量主题所示:
$obj->{'22'};
...
Curly braces may also be used, to clearly delimit the property name. They are most useful when accessing values within a property that contains an array, when the property name is made of mulitple parts, or when the property name contains characters that are not otherwise valid(e.g. from json_decode() or SimpleXML).
...
也可以使用花括号来明确界定属性名称。当访问包含数组的属性中的值时,当属性名称由多个部分组成时,或者当属性名称包含无效的字符(例如来自 json_decode() 或 SimpleXML)时,它们最有用。
回答by Paras
Try this:
尝试这个:
$obj->getAttributeValue("22");
Please post the error if it doesn't work
如果它不起作用,请发布错误
回答by Onix
if you always know the name is 22, you can do this.
如果您始终知道名称是 22,则可以执行此操作。
$myfield = 22;
dd($obj->$myfield);
I tested it and it returns the value in the 22 field correctly.
我对其进行了测试,它正确返回了 22 字段中的值。
回答by Thanh Nguyen
Try this:
尝试这个:
$col = '22';
$res = $obj->{$col};
var_dump($res);
回答by Abdulkareem Mohammed
$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
回答by Jeremy Giberson
You can use model attribute $maps to give your troublesome column a different name. Try
您可以使用模型属性 $maps 为麻烦的列指定不同的名称。尝试
$maps = ['22' => 'twentytwo'];
$hidden = ['22'];
$appends = ['twentytwo'];
Then with your model instance
然后使用您的模型实例
echo $model->twentytwo;
回答by Angelin Calu
The question is similar to this:
问题与此类似:
Hide number field from Eloquent model in Laravel
在 Laravel 的 Eloquent 模型中隐藏数字字段
Presently this is not possible in Laravel as seen in this line of code located in vendor\symfony\var-dumper\Symfony\Component\VarDumper\Cloner\VarCloner.php at line 74
.
目前,这在 Laravel 中是不可能的,如位于vendor\symfony\var-dumper\Symfony\Component\VarDumper\Cloner\VarCloner.php at line 74
.
if ($zval['zval_isref'] = $queue[$i][$k] === $cookie) {
$zval['zval_hash'] = $v instanceof Stub ? spl_object_hash($v) : null;
}
Here is the hack.
这是黑客。
if ($zval['zval_isref'] = (isset($queue[$i][$k])) ? ($queue[$i][$k] === $cookie) : false) {
$zval['zval_hash'] = $v instanceof Stub ? spl_object_hash($v) : null;
}
Issue has been discussed here:
问题已在这里讨论:
回答by nvisser
Possible duplicate of how can I use exists column with laravel model
The same answer applies here; you can use $model->getAttribute('22')
to get the value of a model attribute.
同样的答案在这里也适用;您可以使用它$model->getAttribute('22')
来获取模型属性的值。
回答by iamj
Try to use where:
Tablename::where('22','=','value')->first();
尝试使用 where:
Tablename::where('22','=','value')->first();