2017-04-21 156 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

相关问题