lundi 27 mars 2023

Call to undefined method App\Http\Controllers\UserController::successResponse() (500 Internal Server Error)

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 App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
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 App\Models;

use Illuminate\Database\Eloquent\Model;
class User extends Model{
public $timestamps = false;
protected $table = 'students';
// column sa table
protected $fillable = [
'first_name', 'last_name'
];
}

ROUTES - WEB PHP


<?php

/** @var \Laravel\Lumen\Routing\Router $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" }



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire