我想2组不同的ID保存到一个表,book_id和reader_id,但我正在逐渐:Laravel列未找到:1054未知列“0”“字段列表”
列未找到:在 '字段列表' 1054未知列 '0'
下方
线说:
在连接 - > runQueryCallback( '插入
book_reader
(0
)值(),()?' ,数组('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
DB ::表(“...”) - >插入别“T的工作,为多个插入 其实,你传递两个数组,一个与所有N book_id和一个与所有N reader_id。 。你必须通过N数组,每个数组包含两个字段,一个用于book_id,一个用于reader_id :) – Jiedara
'$ request-> input('readers')'和'$ request-> input('books') '是数组? –
是的,他们是数组 – Przemek