I have written a Controller which delete a record from the database, which is pretty straight forward. The code look like below.
public function destroy(Request $request)
{
try {
MyModel::where('id', json_decode($request->get('data'),true)['id'])->delete();
return response()->json([
'status' => 'success',
'message' => 'Deleted successfully'
]);
} catch (\Exception $e) {
return response()->json(['status' => 'error', 'message' => 'Something went wrong!!', 'exception_message' => $e]);
}
}
Now i wish to use the logic of delete at one common place, there will be many model which will have destroy function. So I wrote this
public function destroy(Request $request)
{
return Crud::destroy(MyModel::class, $request);
}
Crud Class
<?php
namespace App\Helper;
use App\Http\Controllers\Controller;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
class Crud extends Controller
{
public static function destroy(Model $model,Request $request)
{
try {
$output = $model::where('id', $request->get('id'))->delete();
return response()->json([
'status' => 'success',
'message' => 'Deleted successfully',
'output' => $output
]);
} catch (\Exception $e) {
return response()->json(['status' => 'error', 'message' => 'Something went wrong!!', 'exception_message' => $e]);
}
}
}
?>
But when i call destroy function i am getting error as Type error: Argument 1 passed to App\Helper\Crud::destroy() must be an instance of Illuminate\Database\Eloquent\Model, string given,
How can i pass Laravel Model in a function.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire