Laravel数据存储问题求助:无法将数据存入数据库表
Hey there, let's figure out why your Wishlist entries aren't saving to the database. I'll break down the issues in your code and walk through fixes step by step:
First up, your Wishlist model has a truncated table definition:
public $table = 'w...'
Laravel can't map your model to the correct database table if this is incomplete. Replace it with the full, exact name of your wishlist table (e.g., wishlists if you followed Laravel's naming conventions):
class Wishlist extends Model { public $table = 'wishlists'; // Replace with your actual table name }
Even though you're assigning fields manually, Laravel's Eloquent has built-in mass assignment protection that can block saves if fields aren't explicitly allowed. Add a $fillable array to your Wishlist model to whitelist the fields you're saving:
class Wishlist extends Model { public $table = 'wishlists'; protected $fillable = ['book_id', 'user_id']; // Allow these fields to be saved }
Right now, you're not checking if the save() call actually succeeds. Add error handling to get concrete feedback about why the save is failing:
public function store($book_id) { $user_id=Auth::id(); $wishlist=new Wishlist; $wishlist->book_id=$book_id; $wishlist->user_id= $user_id; // Check if save succeeds and debug errors if (!$wishlist->save()) { // For development only: dump errors to the screen dd($wishlist->getErrors()); // Or return a user-friendly error message // return back()->with('error', 'Failed to add book to wishlist: ' . $wishlist->getErrors()->first()); } // Fix the redirect parameter (see next point) return redirect()->route('wishlistCRUD.show' , $book_id) ->with('success', 'Buch gewünscht'); }
The getErrors() method will show you exactly what's wrong—like missing required fields, foreign key constraints failing, or invalid data types.
Your redirect has a mistake: $book_id->id. Since your method accepts $book_id as a parameter (it looks like it's an integer ID, not a Book model instance), calling ->id on an integer will throw an error. Just pass $book_id directly:
return redirect()->route('wishlistCRUD.show', $book_id) ->with('success', 'Buch gewünscht');
If $book_id is actually a Book model instance (not an ID), update your method signature to type-hint it:
public function store(Book $book) { $user_id=Auth::id(); $wishlist=new Wishlist; $wishlist->book_id=$book->id; // Now we can access ->id $wishlist->user_id= $user_id; $wishlist->save(); return redirect()->route('wishlistCRUD.show' , $book->id) ->with('success', 'Buch gewünscht'); }
Double-check your wishlist table to make sure:
- It has
book_idanduser_idfields (matching the data type ofidin yourusersandbookstables—usuallyunsigned bigint) - If you're using foreign keys, the corresponding records exist in
usersandbookstables (e.g., theuser_idfromAuth::id()actually exists, and$book_idis a valid book ID) - The table includes
created_atandupdated_attimestamp fields (unless you set$timestamps = false;in your model—if you don't have these fields, saves will fail silently)
Start with these fixes, and the debug step will help you catch any remaining issues you might have missed!
内容的提问来源于stack exchange,提问作者L.Lehmann

