count(): Parameter must be an array or an object that implements Countable When trying to delete a row I am getting this error message:
count(): Parameter must be an array or an object that implements Countable
products.blade.php
<form style="display:inline" method="POST" action="{{url( '/cpages/products/'. $product->prod_id )}}">
{{ csrf_field()}}
{{ method_field('DELETE') }}
<input type="submit" class="delete fix-inline-btn" value="Delete" />
</form>
routes/web.php
Route::delete('/cpages/products/{product}', 'ProductsController@softDelete');
ProductsController.php
public function softDelete(Products $product)
{
Products::where('prod_id', $product->prod_id)->delete();
Wishlists::where('prod_id', $product->prod_id)->delete();
session()->flash('flash', 'Product Dihapus');
return back();
}
Make sure you're getting the correct result for $product.
public function softDelete(Products $product)
{
dd($product);
// do you get the product?
}
You're using the values from it to delete the other things, so make sure you have a $product->prod_id.
I do get prod_id for example 1 or 2.
dd($product->prod_id);
This is Products.php model:
class Products extends Model
{
use SoftDeletes;
protected $primaryKey = 'prod_id';
protected $guarded = ['prod_id'];
protected $dates = ['deleted_at'];
public function productMetas()
{
return $this->hasMany(ProductsMeta::class, 'prod_id');
}
public function productMainImage()
{
return $this->hasOne(Images::class, 'img_id', 'prod_main_img');
}
public function reviews()
{
return $this->hasMany(Reviews::class, 'prod_id');
}
public static function get_featured()
{
return static::where('prod_featured', 1)->limit('4')->get();
}
public static function checkStockAvailability($prod_id, $amount)
{
$stock = static::select('prod_stock', 'prod_stockable')->where('prod_id', $prod_id)->first();
if ($stock->prod_stockable == 0) {
return true;
}
if ($stock->prod_stock < $amount) {
return false;
}
return true;
}
}
Please check the redirection after the soft deletion. I think that will be the issue.
There is nothing wrong with the back();
There is something wrong these:
Products::where('prod_id', $product->prod_id)->delete();
Wishlists::where('prod_id', $product->prod_id)->delete();
@AJITHLAL - Yeah I also first thought of that after Googling the error. However, then I thought that the odds are higher that the actual issue has something to do with the code.
Probably within the Wishlist::where()->delete() statement. @davy_yg can you comment that one out and see if it works. Otherwise, could you post your wishlist model?
Check WHERE the error occurs
I think its more likely that your error is in the original view when you return back and try to show or count something related to the product you just deleted
@SNAPEY - I said the same thing and he responded that is not the error.
Please check the redirection after the soft deletion. I think that will be the issue.
There is nothing wrong with the back();
There is something wrong these:
Products::where('prod_id', $product->prod_id)->delete();
Wishlists::where('prod_id', $product->prod_id)->delete();
I expect we won't hear anything else... thats how @davy_yg rolls. Loads of questions, no outcomes or thanks.
Please sign in or create an account to participate in this conversation.