$ php artisan make:model Example -m
$ code database/migrations/*_create_examples_table.php
add your columns in the Schema::create('example'....)
$table -> id();
$table -> timestamps();
$table -> string('name');
$table -> text('details');
$ code app/Models/Example.php
After use HasFactory add:
protected $fillable = [
'name',
'details'
];
MIGRATE THE NEW TABLE INTO THE DATABASE$ php artisan migrate
$ php artisan make:resource Example
$ code app/Http/Resources/Example.php
CHANGE FROM:
return parent::toArray($request);
CHANGE TO:
return [
'id' => $this->id,
'name' => $this->name,
'details' => $this->details,
'created_at' => $this->created_at->format('d/m/Y'),
'updated_at' => $this->updated_at->format('d/m/Y'),
];
$ php artisan make:controller API/AuthController
$ php artisan make:controller API/BaseController
$ php artisan make:controller API/ExampleController
$ code app/Http/Controllers/API/BaseController.php
ADD FUNCTION TO BASECONTROLLER CLASS:
public function handleResponse($result, $msg)
{
$res = [
'success' => true,
'data' => $result,
'message' => $msg,
];
return response()->json($res,200);
}
public function handleError($error, $errorMsg =[], $code=404)
{
$res = [
'success' => false,
'message' => $error,
];
if(!empty($errorMsg)){
$res['data'] = $errorMsg;
}
return response()->json($res,$code);
}
$ code app/Http/Controllers/API/ExampleController.php
ADD:
use App\Http\Controllers\API\BaseController as APIBaseController;
use App\Http\Resources\Example;
use App\Models\Example;
php artisan make:request ExampleRequest
7/29/2023
addNewPage.sh
addNewPage-api.sh https://www.webune.com/forums/Laravel-AddNewpage-api.sh.html
$ rm -f Laravel-AddNewpage-api.sh
$ curl -o ./Laravel-AddNewpage-api.sh https://www.webune.com/forums/Laravel-AddNewpage-api.sh.html
$ chmod +x Laravel-AddNewpage-api.sh
$ ./Laravel-AddNewpage-api.sh