Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel delete row with no 'id'

I'm using laravel to create a small app. This app uses a remote database. This database doesn't have an AI id. So when I try to delete a row from the datbase (which I selected with the 'where::' function) I get the following error:

Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: delete from `mailbox` where `id` is null)

Is there a possibility to delete a row with using another key than the primary id?

like image 225
ltv Avatar asked May 19 '16 21:05

ltv


2 Answers

If you have a primary key, set it in your model with

protected $primaryKey = 'my_PK';

If you don't have any PK, use a custom query:

$q = 'DELETE FROM my_table where my_field = ?'
\DB::delete($q, [$my_data]);
like image 148
Felippe Duarte Avatar answered Oct 23 '22 04:10

Felippe Duarte


If you have a Model for your table, you can use something like:

Model::where('column', $value)->delete();

Where Model is the name of your model, etc. You don't need to write the entire query yourself. You can Eloquent to make the delete query as complex as you need (i.e. multiple where queries).

like image 6
Niraj Shah Avatar answered Oct 23 '22 03:10

Niraj Shah