In my laravel project I create a controller for delete the item from all the blade call the same controller.With passing dynamic id and model name. but showing an error for class not found. ex: Class 'User' not found.
How can I add use User; code in my controller dynamically.
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class AjaxController extends Controller
{
public function deleteItem(Request $req)
{
use $req['model'];
$id = $req['id'];
$model_name = $req['model'];
$mdoel_name = $model_name::find($id);
$mdoel_name->delete();
sactivity('delete')->performedOn($mdoel_name)->log('');
return "success";
}
}
This should work:
$id = $req['id'];
$model_name = '\\App\\'.$req['model'];
$model = new $model_name;
$model_name = $model->find($id);
$model->delete();
sactivity('delete')->performedOn($model_name)->log('');
return "success";
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments