Laravel 如何获取单行并发布以查看
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21682871/
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 get single row and post to view
提问by DolDurma
in MySql database i have systemSetting table and this have any data.
在 MySql 数据库中,我有 systemSetting 表,其中有任何数据。
in this table i have this fields :
在这张表中,我有以下字段:
id - site_copyright - date
i want to fill form with this single row.
我想用这一行填写表格。
{{ Form::model($siteSettings) }}
{{ Form::label('site_copyright' , 'copy right') }}
{{ Form::text('site_copyright', null , array('id'=>'site_copyright' )) }}
{{ Form::label('site_copyright' , 'copy right') }}
{{ Form::text('site_copyright', null , array('id'=>'site_copyright' )) }}
{{ Form::close() }}
UPDATE:in CONTROLLER of that i have this:
更新:在 CONTROLLER 中,我有这个:
public function getIndex()
{
$siteSettings = SystemSetting::all();
return View::make('back_end.layouts.manageHeaders')->with('siteSettings', $siteSettings);
}
in Result, form could not parse data to input
fields.
在结果中,表单无法将数据解析为input
字段。
i get error for this :
我得到这个错误:
$siteSettings->site_copyright
回答by Simo A.
This is a duplicate of many questions, such as this: Laravel display table record using query
这是很多问题的重复,例如:Laravel display table record using query
The short answer is: use first()
method to get a single entry.
简短的回答是:使用first()
方法获取单个条目。
回答by revo
You're using form model binding, so change your controller to:
您正在使用表单模型绑定,因此将控制器更改为:
public function getIndex()
{
$siteSettings = new SystemSetting;
return View::make('back_end.layouts.manageHeaders', compact('siteSettings'));
}
回答by ImBhavin95
Query work on 5.1 or more
查询 5.1 或更高版本的工作
return DB::table('users')->where('id', $id)->value('field_name');
//if lower version of laravel like 4.2
DB::table('users')->where('id', $id)->pluck('field_name');