2017-09-27 103 views
1

我目前正試圖將數據從數據表中保存回數據庫。我正在處理數據表中的表單字段,並希望更改輸入值並保存回數據庫。理想情況下,我希望能夠保存到數據庫中的另一個表,如果該id不存在,但現在我只是試圖進行更改並將其恢復到原始表。 我正在使用laravel 5.4和datatables插件。 我一直在查找各種職位laracasts,堆棧溢出和數據表。我已經將我的數據保存在我的控制器中,並繼續獲取對象。我試過在模型中更改受保護的$ cast,以查看開發工具中的值是什麼,並且它沒有幫助。我也在我的控制器中嘗試了多種保存方法,但無濟於事。請參閱下面的響應和代碼:Laravel 5.4和Datatables - Ajax - 保存/發佈數據 - 服務器端

截圖1 enter image description here

截圖2 enter image description here

途徑:

Route::get('/plunker', ['uses'=>'[email protected]']); 
Route::get('/plunker/ttotgetposts', ['as'=>'plunker.ttotgetposts','uses'=>'[email protected]']); 
Route::post('/plunker', '[email protected]')->name('saveOrUpdate'); 

型號:

namespace App; 

use Illuminate\Database\Eloquent\Model; 
use \DB; 

class Inducted extends Model 
{ 
    protected $casts = [ 
     'id' => 'string', 
     'book_name' => 'string', 
     'date_inducted' => 'string', 
    ]; 
    protected $table = 'books'; 
    protected $fillable = ['book_id', 'book_name', 'date_inducted', 'updated_at', 'created_at']; 

} 

查看:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
    <meta http-equiv="X-UA-Compatible" content="ie=edge"> 
    <title>Save test</title> 


    <link rel="stylesheet" href="/css/bootstrap-3.min.css"> 
    <link rel="stylesheet" type="text/css" href="/css/jquery-ui.css"> 

    <link rel="stylesheet" type="text/css" href="/css/led.css"> 
    <link rel="stylesheet" type="text/css" href="/css/jquery-ui.structure.min.css"> 
    <link rel="stylesheet" type="text/css" href="/css/jquery-ui.theme.min.css"> 
    <link rel="stylesheet" type="text/css" href="/css/dataTables.jqueryui.min.css"> 
    <link rel="stylesheet" type="text/css" href="/css/ssstyle.css"> 
    <script src="/js/jquery-1.12.4.js"></script> 
    <script src="/js/jquery.dataTables.min.js"></script> 
    <script src="/js/dataTables.jqueryui.min.js"></script> 
    <script src="/js/jquery-ui.min.js"></script> 
    <script src="/js/jquery-ui.accordion.multiple.js"></script> 
    <script src="/js/demo.js"></script> 

</head> 
<body> 

<h3>ttot Save Test</h3> 
<div> 
    <div class="containershe"> 
     <table id="books" class="ttotsave" style="width:100%"> 
      <thead> 
      <tr><th>ID</th> 
       <th>Name</th> 
       <th>Inducted</th> 
       {{--<th>Edit</th>--}} 
       {{--<th>Delete</th>--}} 
      </tr> 
      </thead> 
     </table> 
    </div> 
    <button data-token="{{ csrf_token() }}" type="submit">Submit</button> 

    <script type="text/javascript"> 
     $(document).ready(function() { 
      oTable = $('#books').DataTable({ 
       "processing": true, 
       "serverSide": true, 
       "scrollY": "500px", 
       "scrollX": true, 
       "scrollCollapse": true, 
       "paging": true, 
       "ajax": "{{ route('tabletotable.ttotgetposts') }}", 
       "dataSrc" : "", 
       "columns": [ 
        {'data': 'id', render: function (data, type, row) 
        { 
         if (data) { 
          var id = $('<div />').text(data).html(); 
         } 
         else { 
          var id = ''; 
         } 

         return '<input type="text" id="id" name="id[' + $('<div />').text(row.id).html() + ']" value=" ' + id + ' ">'; 
        } }, 
        {'data': 'book_name', render: function (data, type, row) 
        { 
         if (data) { 
          var name = $('<div />').text(data).html(); 
         } 
         else { 
          var name = ''; 
         } 

         return '<input type="text" id="name" name="name[' + $('<div />').text(row.id).html() + ']" value=" ' + name + ' ">'; 
        } }, 
        {'data': 'date_inducted', render: function (data, type, row) 
        { 
         if (data) { 
          var date = $('<div />').text(data).html(); 
         } 
         else { 
          var date = ''; 
         } 

         return '<input type="text" id="inducted" name="inducted[' + $('<div />').text(row.id).html() + ']" value=" ' + date + ' ">'; 
        } }, 
       ] 
      }); 


      $('button').click(function() { 
       var data = oTable.$('input, select').serialize(); 

       $.ajax({ 
        headers: { 
         'X-CSRF-TOKEN': $('button').data('token'), 
        }, 
        type: "POST", 
        url: 'tabletotable', 


        dataSrc : 'data', 
        dataType: 'JSON', 
        data: data, 

        async: 'false', 


        success: function() 
        { 
         alert('saved'); 
         console.log('my message' + response + data); 
        } 
       }); 
      }); 

     }); 
    </script> 
</div> 

</body> 
</html> 

控制器:

<?php 

namespace App\Http\Controllers; 

use App\Books; 
use App\Inducted; 
use Illuminate\Foundation\Bus\DispatchesJobs; 
use Illuminate\Routing\Controller as BaseController; 
use Illuminate\Foundation\Validation\ValidatesRequests; 
use Illuminate\Foundation\Auth\Access\AuthorizesRequests; 
use Illuminate\Http\Request; 

use DataTables; 
use DB; 

class Controller extends BaseController 
{ 
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests; 

    public function plunker() 
    { 
     return view('plunker'); 
    } 

    public function ttotGetPosts() 
    { 
     $users = DB::table('books')->select('*'); 
     return DataTables::of($users) 
      ->make(true); 
    } 

    public function saveOrUpdate(Request $request){ 
     dd($request->all()); 
     DB::table('books')->where('id','=', $request->get('id')) 
      ->update(['book_name' => $request->get('name'), 
       'date_inducted' => $request->get('inducted')]); 
     return redirect('/index')->with('status','Successfully updated !'); 
    } 
} 

任何更多的信息,你需要的只是讓我知道

親切的問候

回答

0

試試這個

public function saveOrUpdate(Request $request){ 
    // dd($request->all()); 
    $ids = $request->get('id'); 
    $names = $request->get('name'); 
    $inducteds = $request->get('inducted') 

    foreach($ids as $id) { 
     DB::table('books')->where('id','=', $id) 
     ->update([ 
      'book_name' => $names[$id], 
      'date_inducted' => $inducteds[$id] 
     ]); 
    } 

    return redirect('/index')->with('status','Successfully updated !'); 
} 
+0

該死的.......你的血腥傳奇。我一直在研究這些好幾天:)我只開發了4個月,並且仍然開始着手解決問題。謝謝堆 – Jason

相關問題