2017-04-21 78 views
0

我想2組不同的ID保存到一個表,book_id和reader_id,但我正在逐漸:Laravel列未找到:1054未知列「0」「字段列表」

列未找到:在 '字段列表' 1054未知列 '0'

下方

線說:

在連接 - > runQueryCallback( '插入book_reader0)值(),()?' ,數組('2','1'),對象(Closu重新))

看起來像數組獲取值。

這裏是我的控制器:

function assignbook(Request $request) 
{ 
    $reader_id = $request->input('readers'); 
    $book_id = $request->input('books'); 
    DB::table('book_reader')->insert(array('book_id' => $book_id, 'reader_id' => $reader_id)); 
    return redirect()->back()->with('status', trans('Book has been successfully assigned to the reader.')); 
} 

,並查看是否需要這種耐心:

@extends('layouts.master') 

@section('title') 

@section('content') 
    <form action="{{url('assignbook')}}" method="POST"> 
    {{ csrf_field() }} 
    <h1>Readers details</h1> 
    @foreach ($readers as $reader) 
<ul> 


    <label>{{$reader->name}}</label> 
    <input type='checkbox' value='{{$reader->id}}' name='readers[]'/> 


</ul> 
     @endforeach 
    <h1>Book details</h1> 
    @foreach ($books as $book) 
<ul> 
    <label>{{$book->title}}</label> 
    <input type='checkbox' value='{{$book->id}}' name='books[]'/> 

</ul> 
@endforeach 
<input type="submit" class="btn btn-primary form-control" name="submitBtn" value="Assign Book"> 
    </form> 
@endsection 
+0

DB ::表(「...」) - >插入別「T的工作,爲多個插入 其實,你傳遞兩個數組,一個與所有N book_id和一個與所有N reader_id。 。你必須通過N數組,每個數組包含兩個字段,一個用於book_id,一個用於reader_id :) – Jiedara

+0

'$ request-> input('readers')'和'$ request-> input('books') '是數組? –

+0

是的,他們是數組 – Przemek

回答

2

嘗試這樣, reader_id和book_id是數組,所以你需要插入這樣。

function assignbook(Request $request) 
{ 
    $reader_id = $request->input('readers'); 
    $book_id = $request->input('books'); 
    for($i=0; $i<count($reader_id); $i++) 
    { 
    DB::table('book_reader')->insert(array('book_id' => $book_id[$i], 'reader_id' => $reader_id[$i])); 
    } 
    return redirect()->back()->with('status', trans('Book has been successfully assigned to the reader.')); 
} 
+1

你超過我這一個:) 這是在[文檔](https://laravel.com/docs/5.4/queries#inserts)使用的陣列形成 – Jiedara

+0

工程非常感謝對於 – Przemek

+0

@Przemek你歡迎... – Komal

相關問題