Home > OS >  The data that except in the store function does not go to the db
The data that except in the store function does not go to the db

Time:06-02

i'm learning laravel. I have to do a simple store function to create a new post. The form appears to work correctly but the data does not reach the DB.

There's my store function:

public function store(Request $request)

{
    $request->validate($this->validationRules);
    $formData = $request->all();

    $puzzle = new Puzzle();
    $puzzle->fill($formData);
    $puzzle->save();
    // $newPuzzle = Puzzle::create($formData);
    // return redirect()->route('puzzles.show', $newPuzzle->id);
}

My model:

class Puzzle extends Model

{

public $timestamps = false;
protected $fillable = [
    'title',
    'pieces',
    'image',
    'description',
    'brand',
    'price',
    'available',
    'quantity',
];

} My form:

<form method="POST" action="{{route('puzzles.store')}}">
            @csrf
            <div >
              <label for="title" >Title</label>
              <input type="text"  id="title" name="title" value="{{ old('title') }}">
            </div>

            <div >
                <label for="pieces" >Pieces</label>
                <input type="number"  id="pieces" name="pieces" value="{{ old('pieces')}}">
            </div>

            <div >
              <label for="description" >Description</label>
              <input type="text"  id="description" name="description" value="{{ old('description')}}">
            </div>

            <div >
                <label  for="available">Available</label>
                <input type="checkbox"  id="available" name="available" checked>
            </div>

            <div >
                <label for="quantity" >Quantity</label>
                <input type="number" min="1" step="any"  id="quantity" name="quantity">
            </div>

            <div >
                <label for="price" >Price</label>
                <input type="number" min="3.0" step="0.1"  id="price" name="price">
            </div>
            <button type="submit" >Submit</button>
        </form>

what am I doing wrong? Thank you

CodePudding user response:

Make changes in your store function as per below

public function store(Request $request)
{
    $request->validate($this->validationRules);
    
    $puzzle = Puzzle::create($request->only(
        'title', 'pieces', 'description', 'available', 'quantity', 'price'
    ));
    
    return redirect()->route('puzzles.show', $puzzle->id);
}

in .env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_databse_name
DB_USERNAME=your_databse_user_name
DB_PASSWORD=your_databse_password
  • Related