Laravel无法从表单中检索特定记录

时间:2017-04-21 06:54:02

标签: laravel laravel-5 laravel-5.3

我正在从数据库表中输出一个列表' books'像这样:

function edit()
{
    $books = Book::all();
    return view('layouts/editbooks', ['books' => $books]);
}

然后像这样显示:

@extends('layouts.master')

@section('title')

@section('content')
<h1>Edit Book</h1>
<form action="{{url('editbook')}}" method="GET">
    {{ csrf_field() }}
     @foreach ($books as $book)
        <div>
            <label>{{$book->title}}</label>
            <input type='radio' value='{{$book->id}}' name='books[]'/>
        </div>
    @endforeach
    <input type="submit" class="btn btn-warning form-control" value="Edit">
</form>

@endsection

然后,我希望用户通过选择单选按钮来选择要编辑的记录,并在单击提交按钮后,将用户重定向到包含书籍详细信息的新页面。

因此,我试图从单选按钮获取图书ID,然后如果id匹配,则显示该记录中的所有内容:

function editing(Request $request)
{
    $edit = $request->books;
    return view('layouts/editing', ['edit' => $edit]);
}
function updateEdit()
{
    $books = DB::table('books')->where('id', $edit)->first();
}

这将显示在视图中:

@extends('layouts.master')

@section('title')

@section('content')
<h1>Delete Book</h1>
<form action="{{url('removebook')}}" method="POST">
    {{ csrf_field() }}
        <div>
        <input name="name" type="textbox" value="{{ old('name', $edit['name']) }}"/>
        </div>
    <input type="submit" name="submitBtn" value="Delete Book">
</form>

@endsection

但是我收到一条错误消息:

  

未定义索引:名称(查看:C:\ xampp \ htdocs \ laraveladvweb \ resources \ views \ layouts \ editing.blade.php)

导致问题的原因是什么?

1 个答案:

答案 0 :(得分:0)

不是$ edit只是这本书的ID吗?

试试这个

function editing(Request $request)
{
    $edit = $request->books;
    $book = DB::table('books')->where('id', $edit)->first();
    return view('layouts/editing', ['edit' => $book]);
}

@extends('layouts.master')
@section('title')

@section('content')
<h1>Delete Book</h1>
<form action="{{url('removebook')}}" method="POST">
    {{ csrf_field() }}
        <div>
        <input name="name" type="textbox" value="{{ old('name', $edit['name']) }}"/>
        </div>
    <input type="submit" name="submitBtn" value="Delete Book">
</form>

@endsection