2015-11-27 180 views
1

我得到這個錯誤,我不無爲什麼 部分ID和書籍之間的界限laravel未定義的變量:

未定義的變量:節(查看:C:\ XAMPP \ htdocs中\ LIB \資源\意見\書\ 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','[email protected]'); 
+0

您沒有將'$ section'傳遞給您的視圖:'return view('books.create_book');'您需要有一個' - > with([「section」=> $ section]);'可以在你的視圖中使用它。 –

+0

我得到這個錯誤未定義的屬性:Illuminate \ Database \ Eloquent \ Collection :: $ id(View:C:\ xampp \ htdocs \ lib \ resources \ views \ books \ create_book.blade.php)當我把這個$ section =第::所有();返回視圖('books.create_book') - > with([「section」=> $ section]); – fido

+0

你正在返回所有'Sections',它們在一個數組中......所以'$ section [0] - > id',不要'$ section-> id'。 –

回答

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); 
} 
+0

我得到這個錯誤未定義的屬性:Illuminate \ Database \ Eloquent \ Collection :: $ id(View:C:\ xampp \ htdocs \ lib \ resources \ views \ books \ create_book.blade.php)當我把這個$ section = Section :: all();返回視圖('books.create_book') - > with([「section」=> $ section]); – fido

+0

我不認爲用([「section」=> $ section]);'是正確的。嘗試像我的答案,沒有數組:'('部分',$部分);''。 – Alex

+0

非常感謝 – fido

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變量不存在。簡單地通過它,它會。

+0

我得到這個錯誤未定義的屬性:Illuminate \ Database \ Eloquent \ Collection :: $ id(View:C:\ xampp \ htdocs \ lib \ resources \ views \ books \ create_book.blade.php)當我把這個$ section = Section :: all(); return view('books.create_book') - > with([「section」=> $ section]); – fido

+0

非常感謝你很好的回答 – fido