samedi 22 septembre 2018

Find current Model ID (has many relationship)

I have a laravel app that allows users to add reviews to a book.

My Routes are:

Route::get('/books/{book}/addreview', 'ReviewController@create'); 
Route::post('books/{book}/review', 'ReviewController@store');

My Review controller looks like:

public function create(Book $book) {
    return view('/reviews/create');
}

public function store(Book $book){
     $this->validate(request(),[
        'title' => 'required',
        'body' => 'required'
     ]);

     Review::create([
        'title'=>request('title'),
        'body'=>request('body'),
        'user_id'=>auth()->id(),
        'book_id'=>$book->id
     ]);
    // return redirect('/books');

}

However, I get 'Undefined variable: book' - So I'm not finding the current Book ID. How could I find the current Book ID and pass it to the view?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire