Documentation says:
$user = User::find($user_id);
$user->delete();
This doesnt work, however ProductColor::find($color_id) working. $color->delete() Doest return anything, DELETE FROM query doesn't even execute (as seen in debug bar).
But I can delete record with:
ProductColor::destroy($color_id);
Must be something I overlooked earlier, I'm new to Laravel.
I'm using store also, and it working as expected
public function store(Request $request)
{
$color = new ProductColor();
$color->name = $request->color_name;
$color->order = $request->color_order;
$saved = $color->save();
if ($saved) {
return back()->with('message:success', 'Ok');
} else {
return back()->with('message:error', 'Error');
}
}
To sum up
This WORKS
public function destroy($color_id)
{
$deleted = ProductColor::destroy($color_id);
if ($deleted) {
return back()->with('message:success', 'Deleted');
} else {
return back()->with('message:error', 'Error');
}
}
This NOT
public function destroy($color_id)
{
$color = ProductColor::find($color_id);
$color->delete();
}
My Model
<?php
namespace Modules\Shop\Entities;
use Illuminate\Database\Eloquent\Model;
use Modules\Languages\Entities\Language;
class ProductColor extends Model
{
protected $fillable = [];
protected $table = 'product_colors';
}
In Eloquent, you can delete database records conveniently with the delete method from the parent Model class. The link:delete command, already implemented within the base version of the demo application, deletes links based on a valid link id.
app/Http/routes.phpRoute::get('delete-records','StudDeleteController@index'); Route::get('delete/{id}','StudDeleteController@destroy'); Step 6 −The output will appear as shown in the following image. Step 7 − Click on delete link to delete that record from database.
Delete method of query builder returns the number of affected rows. So, if no records deleted - 0. If 10 - 10.
Sorry, I've figured out the problem. My mistake to post this question.
What I tried to do:
$color = new ProductColor();
$color->find($color_id);
$color->delete();
Should be:
$color = ProductColor::find( $color_id );
$color->delete();
My problem was that I was scared about the IDE complaining about using non-static method 'find'
Your code is fine - the docs show exactly what you are doing.
If there is no error, and the color is not deleted as expected, then $color_id is not being passed as expected. Try using findOrFail, or add some other check that you found the expected model.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With