I am so confused by this error because this is my first time in back end subject , our instructor didn’t included the CRUD operations on the video he provided so I decided to discover all by myself then I came up with this error on POST operation
HTTP – CONTROLLER – USER CONTROLLER PHP
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppModelsUser;
Class UserController extends Controller {
private $request;
public function __construct(Request $request){
$this->request = $request;
}
public function getUsers(){
$users = User::all();
return response()->json($users, 200);
}
public function add(Request $request ){
$rules = [
'first_name' => 'required|max:20',
'last_name' => 'required|max:20',
// 'gender' => 'required|in:Male,Female',
];
$this->validate($request,$rules);
$user = User::create($request->all());
return $this->successResponse($user,
Response::HTTP_CREATED);
}
}
MODEL – USER PHP
<?php
namespace AppModels;
use IlluminateDatabaseEloquentModel;
class User extends Model{
public $timestamps = false;
protected $table = 'students';
// column sa table
protected $fillable = [
'first_name', 'last_name'
];
}
ROUTES – WEB PHP
<?php
/** @var LaravelLumenRoutingRouter $router */
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It is a breeze. Simply tell Lumen the URIs it should respond to
| and give it the Closure to call when that URI is requested.
|
*/
$router->get('/', function () use ($router) {
return $router->app->version();
});
$router->get('/users',['uses' => 'UserController@getUsers']);
$router->post('/postUsers', 'UserController@add'); // create new user record
This is the all good I cant identify where the error occured or I have something miss to put
I just want to run the POST in postman to add data on column because I think POST is updating the database
so this is the data that I want to add
{
"first_name": "Lorem",
"last_name": "Ipsum"
}
2
Answers
in userController of parent controller you have this method?
successResponse
if not register this method you get exception
I assume that you are trying to format the response by using a trait. Correct me if im wrong. If thats the case, you have called the trait function without importing the class in the first place. Then import the class by use keyword.
So the general flow would be as follows you can make adjustments if you are using traits as i assumed.
And also you dont need to typehint the request in constructor manually hence laravel service container automatically resolves the requests through out the app.