SQLSTATE[HY000]: General error: 1364 Field 'name' doesn't have a default value (SQL: insert into products
(featured_image
, product_attributes
, updated_at
, created_at
) values (7wtDzLjyBAIIGzQBSXVBwKqnuUPmh1OGLVpxLd2H.png, null, 2021-03-09 02:11:00, 2021-03-09 02:11:00))
Any solution to my facing problem? Whenever I create only the validated part everything is fine. But, on adding "Product::create(['featured_image' => $this->featuredImage->hashName()]); return with above error.
My Products Table
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->mediumText('description');
$table->float('price');
$table->string('featured_image')->nullable();
$table->unsignedBigInteger('shop_id')->nullable();
$table->foreign('shop_id')->references('id')->on('shops')->onDelete('cascade');
$table->timestamps();
});
Datatable.php
<?php
namespace App\Http\Livewire;
use App\Product;
use Livewire\Component;
use Livewire\WithPagination;
use Livewire\WithFileUploads;
class Datatable extends Component
{
use WithFileUploads;
use WithPagination;
protected $products;
public $featuredImage;
public $sortBy = 'created_at';
public $sortDirection = 'desc';
public $search = '';
public $perPage = 10;
public $editModal = false;
public $deleteModal = false;
public $editingProduct;
public $deletingProductId;
public function render()
{
$this->fetchProducts();
return view('livewire.datatable',[
'items' => $this->products
]);
}
protected function fetchProducts()
{
$products = Product::search($this->search)
->orderBy($this->sortBy, $this->sortDirection)
->paginate($this->perPage);
$this->products = $products;
}
public function updatingSearch()
{
$this->resetPage();
}
public function sortBy($field)
{
if ($this->sortDirection == 'asc') {
$this->sortDirection = 'desc';
} else {
$this->sortDirection = 'asc';
}
return $this->sortBy = $field;
}
public function editProduct($product)
{
$this->editingProduct = $product;
$this->editModal = true;
}
public function createProduct()
{
$this->reset();
$this->editModal = true;
}
public function saveProduct()
{
$validated = $this->validate([
'editingProduct.name' => 'required|min:3|max:20',
'editingProduct.price' => 'required|min:1',
'editingProduct.description' => 'required|min:5|max:30',
'featuredImage' => 'required|image|max:1024|mimes:jpg,png,jpeg'
]);
if(!empty($this->featuredImage)) {
$this->featuredImage->store('public/photos');
}
if(!empty($this->editingProduct['id'])) {
$product = Product::find($this->editingProduct['id']);
$product->update($validated['editingProduct']);
}else {
Product::create($validated['editingProduct']);
Product::create([
'featured_image' => $this->featuredImage->hashName()
]);
}
$this->fetchProducts();
$this->closeModal();
}
public function closeModal()
{
$this->editModal= $this->deleteModal = false;
}
public function confirmDelete($productId)
{
$this->deleteModal = true;
$this->deletingProductId = $productId;
}
public function deleteProduct()
{
if(!empty($this->deletingProductId)) {
Product::destroy($this->deletingProductId);
}
$this->deletingProductId = null;
$this->fetchProducts();
$this->closeModal();
}
}
datatable.blade.php
<div class="field">
<label class="label">Name</label>
<div class="control">
<input wire:model.lazy="editingProduct.name" {{-- value="{{$editingProduct['name'] ?? null}}"
--}} class="input" type="text" placeholder="Text input">
@error('editingProduct.name')
<p class="help is-danger">{{$message}}</p>
@enderror
</div>
</div>
<div class="field">
<label class="label">Description</label>
<div class="control">
<textarea class="textarea"placeholder="Textarea"
wire:model.lazy="editingProduct.description"></textarea>
@error('editingProduct.description')
<p class="help is-danger">{{$message}}</p>
@enderror
</div>
</div>
When I add ->nullable method to the field, there is no error but the data in table is null.