You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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:

1. Fix the Incomplete Table Name in Your Model

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
}
2. Add Mass Assignment Protection (Critical!)

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
}
3. Debug the Save Operation to See Exact Errors

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.

4. Fix the Redirect Route Parameter Bug

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');
}
5. Verify Your Database Table Structure

Double-check your wishlist table to make sure:

  • It has book_id and user_id fields (matching the data type of id in your users and books tables—usually unsigned bigint)
  • If you're using foreign keys, the corresponding records exist in users and books tables (e.g., the user_id from Auth::id() actually exists, and $book_id is a valid book ID)
  • The table includes created_at and updated_at timestamp 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 03:54:11