laravel未定义变量::

时间:2015-11-27 21:32:14

标签: laravel-5.1

我得到这个错误我不知道为什么 部分ID和书籍之间的界线

未定义的变量:section(查看:C:\ xampp \ htdocs \ lib \ resources \ views \ books \ create_book.blade.php)

链接<a href="{{url('admin_book/createbook',$section->id)}}"class="btn btn-success">New Book</a>

我的create_book.blade.php

{!! Form::open(['url'=>'admin_book/store','method'=>'POST','files'=>'true']) !!}
{!! Form::hidden('section_id',$section->id) !!}
<div class="form-group ">
{!! Form::label('Book Title', 'Enter the Title of Book:') !!}
{!! Form::text("book_title",'',['class'=>'form-control']) !!}
</div>
 <div class="form-group ">
{!! Form::label('Book Edition', 'Enter the Edition of Book:') !!}
{!! Form::text("book_edition",'',['class'=>'form-control']) !!}
</div>
 <div class="form-group ">
{!! Form::label('Book Description', 'Enter the Description of Book:') !!}
{!! Form::textarea("book_description",'',['class'=>'form-control']) !!}
 </div>
 <div class="form-group">
{!! Form::label('upload', 'Upload an Image:') !!}
{!! Form::file('image','',['class'=>'form-control']) !!}
 </div>
 <br>
 <div class="form-group">
{!! Form::submit('Create',['class'=>'btn btn-info btn-block']) !!}
 </div>
{!! Form::close() !!}

和我的booksControllers

public function create()
    {
        return view('books.create_book');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $book_title = $request ->input('book_title');
        $book_edition = $request ->input('book_edition');
        $book_description = $request ->input('book_description');
        $file = $request ->file('image');
        $destinationPath = 'images';
        $filename = $file ->getClientOriginalName();
        $file ->move($destinationPath,$filename);

        $section_id = $request -> section_id;


        $new_book = new Book;
        $new_book ->book_title = $book_title;
        $new_book ->book_edition = $book_edition;
        $new_book ->book_description = $book_description;
        $new_book ->image_name = $filename;

        $new_book ->section_id = $section_id;

        $new_book ->save();
        return redirect('admin_book/'.$section_id);
    }

和我的路线

Route::get('admin_book/createbook','BooksController@create');

2 个答案:

答案 0 :(得分:0)

正如蒂姆·刘易斯所指出的那样,在创建视图时,你没有传递$section变量。

您的创建方法应如下所示:

public function create()
    {
        //Logic that gets the section goes here
        //Stored in the $section variable

        return view('books.create_book', ['section' => $section]);
    }

这将解决您的错误,因为Undefined variable: section告诉您,在您的视图中,名为section的变量不存在。只需通过即可。

答案 1 :(得分:0)

您没有将$section变量传递给您的视图。您必须从数据库中检索变量并将其传递给视图,如下所示:

public function create() {
    //Retrieve from database
    $section = Section::all(); 
    //Pass the collection to the view
    return view('books.create_book')->with('section', $section);
}