将数据库数组插入laravel

时间:2018-10-11 06:26:32

标签: html database laravel

我已经读了很多laravel教程来做到这一点,但是没有一个对我有用。

这是我的表格

<div class="row increment  control-group">
  {{-- Request Item --}}
  <div class="form-group col-md-4">
    <input type="text" class="form-control" name="request_item[]" placeholder="Item" />
  </div>
  {{-- Request Description --}}
  <div class="form-group col-md-4">
    <input type="text" class="form-control" name="request_description[]" placeholder="Item Description" />
  </div>
  <div class="form-group col-md-4">
    <button class="btn btn-success" id="btn-item" type="button">Add</button>
  </div>
</div>

这是我的控制人

$request->validate([
            'request_no' => 'required|max:255',
			'request_date' => 'required|date', //unique:(tablename)
            'request_item' => 'required|max:255',
            'request_description' => 'max:255',
            'request_by' => 'required|max:255',
            'request_status' => 'required|max:255',
            'request_scan' => 'mimes:pdf',
            'created_by' => 'max:255',
            'updated_by' => 'max:255',
		]);
        
        //For file uploading
		$name="";
		if($request->hasfile('filename')){
			$file = $request->file('filename');
			$name = time()."_".$file->getClientOriginalName();
			$file->move(public_path(). '/images/', $name);
		}
        
        $itemArray = Input::get('request_item');
        $count = count($itemArray);

        
        for($i = 0; $i < $count; $i++){
            $request= new \Trisco\IT_Request;
            $request->request_no=$request->get('request_no');
            $request->request_date=$request->get('request_date');
            $request->request_item=$itemArray[$i];
            $request->reqeust_description='';
            $request->request_by=$request->get('request_by');
            $request->request_status=$request->get('request_status');
            $passport->request_scan = $name; //file
            $request->added_by=$request->get('username');
            $request->updated_by=$request->get('username');
            $request->save();
        }

request_item和request_description均为字符串。

有人可以帮助我吗?

谢谢。

2 个答案:

答案 0 :(得分:1)

如何在数组上循环?

$itemArray = Input::get('request_item');
foreach ($itemArray as $value) {
  $request->request_item=$value;
  //your code here
  //request->save()
}

答案 1 :(得分:0)

怎么回事?
还要检查拼写-

$request->reqeust_description='';

尝试这个>>

$request->request_item= implode(',', $request['request_item']);