未找到Laravel列:1054'字段列表'中的未知列'0'

时间:2017-04-21 09:16:42

标签: mysql laravel laravel-5

我正在尝试将2个不同的ID保存到表,book_id和reader_id,但我得到了:

  

找不到列:1054'字段列表'中的未知列'0'

下面的一行说:

  

在Connection-> runQueryCallback('insert into book_reader0)values(?),(?)',array('2','1'),object(Closure) )

看起来像数组正在获取值。

这是我的控制器:

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

1 个答案:

答案 0 :(得分: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.'));
}