我正在學習laravel。我必須做一個簡單的商店功能來創建一個新帖子。表單似乎作業正常,但資料未到達資料庫。
這是我的商店功能:
公共功能存盤(請求 $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);
}
我的模型:
類拼圖擴展模型
{
public $timestamps = false;
protected $fillable = [
'title',
'pieces',
'image',
'description',
'brand',
'price',
'available',
'quantity',
];
我的表格:
<form method="POST" action="{{route('puzzles.store')}}">
@csrf
<div class="mb-3">
<label for="title" class="form-label">Title</label>
<input type="text" class="form-control" id="title" name="title" value="{{ old('title') }}">
</div>
<div hljs-number">3">
<label for="pieces" hljs-string">">Pieces</label>
<input type="number" hljs-string">" id="pieces" name="pieces" value="{{ old('pieces')}}">
</div>
<div hljs-number">3">
<label for="description" hljs-string">">Description</label>
<input type="text" hljs-string">" id="description" name="description" value="{{ old('description')}}">
</div>
<div hljs-number">3 form-check">
<label hljs-string">" for="available">Available</label>
<input type="checkbox" hljs-string">" id="available" name="available" checked>
</div>
<div hljs-number">4">
<label for="quantity" hljs-string">">Quantity</label>
<input type="number" min="1" step="any" hljs-string">" id="quantity" name="quantity">
</div>
<div hljs-number">4">
<label for="price" hljs-string">">Price</label>
<input type="number" min="3.0" step="0.1" hljs-string">" id="price" name="price">
</div>
<button type="submit" hljs-string">">Submit</button>
</form>
我究竟做錯了什么?謝謝
uj5u.com熱心網友回復:
根據以下更改您的商店功能
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);
}
在.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
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/484677.html