0

I have a password_resets table in mysql database and i want to get single raw of data and delete it with one query in Lumen with DB facade like this:

$reset_row = DB::table('password_resets')->where('token', $request->token)->first()->delete();

But i have a error :

Call to undefined method stdClass::delete()

i try this code :

  $reset_row = DB::table('password_resets')->where('token', $request->token)
  //do my work whith $reset_row->first();
  $reset_row->delete();

But i think this way use 2 query to do this work.

NOTE : i know i can not delete and reason is first() method )return it to array)

Is there any way to do this?

Omid Reza Heidari
  • 658
  • 12
  • 27

3 Answers3

0

Actually no need to use ->first(). Bcz your token is unique.

if you need try this.

$reset_row = DB::table('password_resets')->where('token', $request->token)
    ->Limit(1)->delete();

I think it is possible to use "limit" with "delete".

Hashan
  • 164
  • 8
0

You can simply chain the delete method to your Query Buider. No need to select the first on since you want to delete them, not select them.

DB::table('password_resets')->where('token', $request->token)->delete();

Its already answered in another post in StackOverflow.

You can use this answer too laravel-delete-query-builder and you can also look at the official documentation about deleting using the query builder

Kerel
  • 732
  • 6
  • 20
Karan Sadana
  • 1,363
  • 7
  • 11
0

At the end I Think to create model for reset password and Use it like this:

1. set primary key to token by set it on model like this

2. use find method to find token (document for find method)

use this way:

$reset=PasswordReset::find($token);
//work with $reset
$reset->delete()

NOTE: : for set primary key to string col see this link too

Omid Reza Heidari
  • 658
  • 12
  • 27