php 如何在 LARAVEL 5.2 中将数据存储到数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44171190/
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
How to store data to database in LARAVEL 5.2
提问by Wisnu
Iam beginer on Laravel, i have problem when i want to store data to database. When name on view different with field name on database data didn't save on database but when input name on view same with field name on database data stored correctly
我是 Laravel 的初学者,当我想将数据存储到数据库时遇到问题。当视图上的名称与数据库数据上的字段名称不同时,没有保存在数据库中,但是当视图上的输入名称与数据库数据上的字段名称正确存储时
example this is view
示例这是视图
<div class="form-group">
<div class="row">
<div class="col-md-3">
<label>Job</label>
<input type="text" class="form-control" name="job" placeholder="Job">
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-md-3">
<label>Machine</label>
{{ Form::select('machine', $mesin_laminating->pluck('active', 'kode')->all(), null, ['class' => 'form-control select2'])}}
</div>
</div>
</div>
this is my controller
这是我的控制器
public function store(Request $request)
{
$this->validate($request, [
'job' => 'required',
'machine' => 'required'
]);
$input = $request->all();
SpkAdmin::create($input);
}
this is my model
这是我的模型
protected $table = 'tb_job';
protected $fillable = ['user_job', 'machine'];
this is my database
这是我的数据库
user_job | machine
------ | ------
Cell | Cell
How to scyn job on controller to store on user_job? Sorry for my english
如何在控制器上 scyn 作业以存储在 user_job 上?对不起我的英语不好
回答by Ganesh Ghalame
Replace store
as below:
替换store
如下:
public function store(Request $request)
{
$this->validate($request, [
'job' => 'required',
'machine' => 'required'
]);
$spkAdmin = new SpkAdmin();
//On left field name in DB and on right field name in Form/view
$spkAdmin->user_job = $request->input('job');
$spkAdmin->machine = $request->input('machine');
$spkAdmin->save();
}
回答by Gaurav Gupta
there is another way of adding data in database something like this
还有另一种在数据库中添加数据的方法是这样的
$data = Input::all();
$check = DB::table('tablename')->insertGetId(array(
'phone_no' => $data['number'],
'firstname' => $data['first_name'],
'lastname' => $data['last_name'],
'birth_month' => $data['birth_month'],
'birth_year' => $data['birth_year'],
'zipcode' => $data['zip_code'],
'device_token' => $data['device_token'],
'created_at' => Carbon::now()
));
hope this could solve your query
希望这可以解决您的疑问
回答by Ramnish Parmar
use App\SpkAdmin;
public function store(Request $request)
{
$this->validate($request->all(), [
'job' => 'required',
'machine' => 'required'
]);
$spkAdmin = SpkAdmin::create($request->all());
Or
$input = [];
$input = $request->all();
$input['user_job'] = $request->get('job');
$input['machine'] = $request->get('machine');
SpkAdmin::create($input);
}