L.Lehmann
L.Lehmann

Reputation: 9

store in table, Laravel

I have a problem to store data in a table. And after hours I still couldnt figure out, what the problem is. I would be so happy for some help!

WishlistController.php:

public function store($book_id)
    {
        $user_id=Auth::id();
        $wishlist=new Wishlist;
        $wishlist->book_id=$book_id;
        $wishlist->user_id= $user_id;
        $wishlist->save();
        return redirect()->route('wishlistCRUD.show' , $book_id->id)
            ->with('success', 'Buch gewünscht');

The Model:

class Wishlist extends Model
{
    public $table = 'wishlist';
    public $fillable = ['book_id','user_id',];

the view.blade:

{!! Form::open(array('route' => 'wishlistCRUD.store', 'method'=>'POST')) !!}
  <form action="someaction" method="POST">
      <input type="hidden" name="book_id" value="{{$book->id}}"/>
  </form>    
<a class="btn btn-primary" href="{{ route('wishlistCRUD.store',$book->id) }}">wünschen</a>
{!! Form::close() !!}

the Route:

Route::post('wishlistCRUD.store', 'WishlistController@store');

When I check the table, nothing new is added. Its frustrating :-(

Upvotes: 1

Views: 952

Answers (4)

Babak Asadzadeh
Babak Asadzadeh

Reputation: 1239

Just change post to get in this line:

    Route::post('wishlistCRUD.store', 'wishlistCRUD@store');

Upvotes: 1

Laerte
Laerte

Reputation: 7073

Try to do this. In your Controller, you don't need $book_id as a parameter because you can get it from the $request:

public function store(Request $request)
{
    $user_id = Auth::id();
    $book_id = $request->book_id;

    $wishlist = new Wishlist;
    $wishlist->book_id = $book_id;
    $wishlist->user_id = $user_id;
    $wishlist->save();

    return redirect()->route('wishlistCRUD.show' , $book_id)
            ->with('success', 'Buch gewünscht');
}

Don't forget to add use Illuminate\Http\Request;

And then in your blade, you don't need to set a route in an anchor, because it is already defined in the form. And you don't need two forms, because Form::open already does that:

{!! Form::open(array('route' => 'wishlistCRUD.store', 'method'=>'POST')) !!}
  {{ csrf_field() }}
      <input type="hidden" name="book_id" value="{{$book->id}}"/>
  {!! Form::submit('wünschen') !!}
{!! Form::close() !!}

This should work.

Upvotes: 0

Santee
Santee

Reputation: 448

In blade, change the <a class="btn btn-primary" href="{{ route('wishlistCRUD.store',$book->id) }}">wünschen</a>

to a <button class="btn btn-primary" type="submit">wünschen</button>

Delete the tag form, it's not necesary. You declared it with the {!! form:open... !!}

You are trying to access to route('wishlistCRUD.store',$book->id) by GET with the <a></a>

Try this: the view.blade:

{!! Form::open(['url' => 'wishlistCRUD/store', 'method' => 'POST']) !!} 
    <input type="hidden" name="book_id" value="{{$book->id}}"/> 
    <button class="btn btn-primary" type="submit">wünschen</button> 
{!! Form::close() !!}

the Route:

Route::post('wishlistCRUD/store', 'WishlistController@store');

Upvotes: 0

Saurabh Mistry
Saurabh Mistry

Reputation: 13669

You have created POST method in your web.php for adding book to wish list, in your blade template you need to submit hidden form on click of link.

in your web.php

Route::get('wishlistCRUD/book/{book_id}','WishlistController@get_book_by_id')->name('get_book_by_id');

    Route::post('wishlistCRUD.store', 'WishlistController@store')->name('store');

In your blade template

<a class="btn btn-primary" href="javascript:void(0)" onclick="event.preventDefault();document.getElementById('addToWishlist').submit();">wünschen</a>

<form style="display:none" id="addToWishlist" method="POST" action="{{ route('store')}}">{{csrf_field()}} <input type="hidden" name="bookid" value="{{$book->id}}" /> </form>

in your controller

public function store(Request $request){
        $bookID= $request->input('bookid');
        $wishlist=new Wishlist();
        $wishlist->book_id= $bookID
        $wishlist->user_id= Auth::user()->id;
        $wishlist->save();
        return redirect()->route('get_book_by_id', ['book_id' => $bookID]);
}


public function get_book_by_id(Request $request,$book_id){
     // find book by ID;

     $book=Book::find($book_id);

     // book found 
     if($book){

       return view('book')->with('book',$book);

     }else{

      // book not found , redirect to 404 page or home page
       return redirect('/');
     }
}

Upvotes: 0

Related Questions