dongshao1156
2017-05-25 02:10 阅读 78
已采纳

如何在LARAVEL 5.2中将数据存储到数据库

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

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    doukuang8166 doukuang8166 2017-05-25 02:40

    Replace store as below:

    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();
    }
    
    点赞 评论 复制链接分享
  • duandian8110 duandian8110 2017-05-25 05:39

    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

    点赞 评论 复制链接分享

相关推荐