php eloquent code example
Example 1: laravel eloquent remove from db
$res=User::where('id',$id)->delete();
Example 2: laravel eloquent remove from db
public function destroy($id){
$res=User::find($id)->delete();
if ($res){
$data=[
'status'=>'1',
'msg'=>'success'
];
}else{
$data=[
'status'=>'0',
'msg'=>'fail'
];
return response()->json($data);
Example 3: laravel list of models
<?php
function getAllModels(): array
{
$composer = json_decode(file_get_contents(base_path('composer.json')), true);
$models = [];
foreach ((array)data_get($composer, 'autoload.psr-4') as $namespace => $path) {
$models = array_merge(collect(File::allFiles(base_path($path)))
->map(function ($item) use ($namespace) {
$path = $item->getRelativePathName();
return sprintf('\%s%s',
$namespace,
strtr(substr($path, 0, strrpos($path, '.')), '/', '\\'));
})
->filter(function ($class) {
$valid = false;
if (class_exists($class)) {
$reflection = new \ReflectionClass($class);
$valid = $reflection->isSubclassOf(\Illuminate\Database\Eloquent\Model::class) &&
!$reflection->isAbstract();
}
return $valid;
})
->values()
->toArray(), $models);
}
return $models;
}
Example 4: crrate model in laravel
php artisan make:model Flight -mcr