1

Hi im new to the slim framework so i want to know that how to inset data to the table of users with eloquent database ?

model

namespace App\Models;


use Illuminate\Database\Eloquent\Model;

class User extends Model
{

    protected  $table = "users";

    protected $fillable =[
        "first_name",
        "last_name",
   
    ];
}

Controller function im mention create function with that model static

controller function

  public function index( $request, $response){
             $user = User::create([
            'first_name' => $request->getParam("email"),
            'last_name' => "team"
          ]);                
    }

this is my database table

CREATE TABLE `users` (
  `id` bigint(20) NOT NULL,
  
  `first_name` varchar(32) NOT NULL,
  `last_name` varchar(32) NOT NULL,
  `created_on` datetime DEFAULT NULL,
  `is_deleted` tinyint(4) NOT NULL DEFAULT 0,
  `is_active` tinyint(4) NOT NULL DEFAULT 1,
  `deleted_on` datetime DEFAULT NULL,
  `deleted_by` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

i can able to get data form the user table with User::where('id',1); but i cant able to inset data using User::create([]) if i tried 500 internal error

{

"success": false,
"error": "INTERNAL_ERROR",
"message": "something went wrong internally",
"status_code": "500",
"trace": "#0 D:\\XAMPP7.4\\htdocs\\Slim_auth - test\\vendor\\illuminate\\database\\Connection.php(672): Illuminate\\Database\\Connection->runQueryCallback('insert into `xe...', Array, Object(Closure))\n#1 D:\\XAMPP7.4\\htdocs\\Slim_auth - test\\vendor\\illuminate\\database\\Connection.php(502): Illuminate\\Database\\Connection->run('insert into `xe...', Array, Object(Closure))\n#2 D:\\XAMPP7.4\\htdocs\\Slim_auth - test\\vendor\\illuminate\\database\\Connection.php(454): Illuminate\\Database\\Connection->statement('insert into `xe...', Array)\n#3 D:\\XAMPP7.4\\htdocs\\Slim_auth - test\\vendor\\illuminate\\database\\Query\\Processors\\Processor.php(32): Illuminate\\Database\\Connection->insert('insert into `xe...', Array)\n#4 D:\\XAMPP7.4\\htdocs\\Slim_auth - test\\vendor\\illuminate\\database\\Query\\Builder.php(3028): Illuminate\\Database\\Query\\Processors\\Processor->processInsertGetId(Object(Illuminate\\Database\\Query\\Builder), 'insert into `xe...', Array, 'id')\n#5 D:\\XAMPP7.4\\htdocs\\Slim_auth - test\\vendor\\illuminate\\database\\Eloquent\\Builder.php(1657): Illuminate\\Database\\Query\\Builder->insertGetId(Array, 'id')\n#6 D:\\XAMPP7.4\\htdocs\\Slim_auth - test\\vendor\\illuminate\\database\\Eloquent\\Model.php(1188): Illuminate\\Database\\Eloquent\\Builder->__call('insertGetId', Array)\n#7 D:\\XAMPP7.4\\htdocs\\Slim_auth - test\\vendor\\illuminate\\database\\Eloquent\\Model.php(1153): Illuminate\\Database\\Eloquent\\Model->insertAndSetId(Object(Illuminate\\Database\\Eloquent\\Builder), Array)\n#8 D:\\XAMPP7.4\\htdocs\\Slim_auth - test\\vendor\\illuminate\\database\\Eloquent\\Model.php(994): Illuminate\\Database\\Eloquent\\Model->performInsert(Object(Illuminate\\Database\\Eloquent\\Builder))\n#9 D:\\XAMPP7.4\\htdocs\\Slim_auth - test\\vendor\\illuminate\\database\\Eloquent\\Builder.php(896): Illuminate\\Database\\Eloquent\\Model->save()\n#10 D:\\XAMPP7.4\\htdocs\\Slim_auth - test\\vendor\\illuminate\\support\\helpers.php(263): Illuminate\\Database\\Eloquent\\Builder->Illuminate\\Database\\Eloquent\\{closure}(Object(App\\Models\\Test))\n#11 D:\\XAMPP7.4\\htdocs\\Slim_auth - test\\vendor\\illuminate\\database\\Eloquent\\Builder.php(897): tap(Object(App\\Models\\Test), Object(Closure))\n#12 D:\\XAMPP7.4\\htdocs\\Slim_auth - test\\vendor\\illuminate\\support\\Traits\\ForwardsCalls.php(23): Illuminate\\Database\\Eloquent\\Builder->create(Array)\n#13 D:\\XAMPP7.4\\htdocs\\Slim_auth - test\\vendor\\illuminate\\database\\Eloquent\\Model.php(2132): Illuminate\\Database\\Eloquent\\Model->forwardCallTo(Object(Illuminate\\Database\\Eloquent\\Builder), 'create', Array)\n#14 D:\\XAMPP7.4\\htdocs\\Slim_auth - test\\vendor\\illuminate\\database\\Eloquent\\Model.php(2144): Illuminate\\Database\\Eloquent\\Model->__call('create', Array)\n#15 D:\\XAMPP7.4\\htdocs\\Slim_auth - test\\app\\Controllers\\ForgetController.php(74): Illuminate\\Database\\Eloquent\\Model::__callStatic('create', Array)\n#16 [internal function]: App\\Controllers\\ForgetController->index(Object(Slim\\Http\\Request), Object(Slim\\Http\\Response), Array)\n#17 D:\\XAMPP7.4\\htdocs\\Slim_auth - test\\vendor\\slim\\slim\\Slim\\Handlers\\Strategies\\RequestResponse.php(40): call_user_func(Array, Object(Slim\\Http\\Request), Object(Slim\\Http\\Response), Array)\n#18 D:\\XAMPP7.4\\htdocs\\Slim_auth - test\\vendor\\slim\\slim\\Slim\\Route.php(281): Slim\\Handlers\\Strategies\\RequestResponse->__invoke(Array, Object(Slim\\Http\\Request), Object(Slim\\Http\\Response), Array)\n#19 D:\\XAMPP7.4\\htdocs\\Slim_auth - test\\vendor\\slim\\slim\\Slim\\MiddlewareAwareTrait.php(117): Slim\\Route->__invoke(Object(Slim\\Http\\Request), Object(Slim\\Http\\Response))\n#20 D:\\XAMPP7.4\\htdocs\\Slim_auth - test\\vendor\\slim\\slim\\Slim\\Route.php(268): Slim\\Route->callMiddlewareStack(Object(Slim\\Http\\Request), Object(Slim\\Http\\Response))\n#21 D:\\XAMPP7.4\\htdocs\\Slim_auth - test\\vendor\\slim\\slim\\Slim\\App.php(503): Slim\\Route->run(Object(Slim\\Http\\Request), Object(Slim\\Http\\Response))\n#22 D:\\XAMPP7.4\\htdocs\\Slim_auth - test\\vendor\\tuupola\\callable-handler\\src\\CallableHandler.php(51): Slim\\App->__invoke(Object(Slim\\Http\\Request), Object(Slim\\Http\\Response))\n#23 D:\\XAMPP7.4\\htdocs\\Slim_auth - test\\vendor\\tuupola\\slim-jwt-auth\\src\\JwtAuthentication.php(132): Tuupola\\Middleware\\CallableHandler->handle(Object(Slim\\Http\\Request))\n#24 D:\\XAMPP7.4\\htdocs\\Slim_auth - test\\vendor\\tuupola\\callable-handler\\src\\DoublePassTrait.php(47): Tuupola\\Middleware\\JwtAuthentication->process(Object(Slim\\Http\\Request), Object(Tuupola\\Middleware\\CallableHandler))\n#25 [internal function]: Tuupola\\Middleware\\JwtAuthentication->__invoke(Object(Slim\\Http\\Request), Object(Slim\\Http\\Response), Object(Slim\\App))\n#26 D:\\XAMPP7.4\\htdocs\\Slim_auth - test\\vendor\\slim\\slim\\Slim\\DeferredCallable.php(57): call_user_func_array(Object(Tuupola\\Middleware\\JwtAuthentication), Array)\n#27 [internal function]: Slim\\DeferredCallable->__invoke(Object(Slim\\Http\\Request), Object(Slim\\Http\\Response), Object(Slim\\App))\n#28 D:\\XAMPP7.4\\htdocs\\Slim_auth - test\\vendor\\slim\\slim\\Slim\\MiddlewareAwareTrait.php(70): call_user_func(Object(Slim\\DeferredCallable), Object(Slim\\Http\\Request), Object(Slim\\Http\\Response), Object(Slim\\App))\n#29 D:\\XAMPP7.4\\htdocs\\Slim_auth - test\\Middleware\\middleware.php(28): Slim\\App->Slim\\{closure}(Object(Slim\\Http\\Request), Object(Slim\\Http\\Response))\n#30 [internal function]: Closure->{closure}(Object(Slim\\Http\\Request), Object(Slim\\Http\\Response), Object(Closure))\n#31 D:\\XAMPP7.4\\htdocs\\Slim_auth - test\\vendor\\slim\\slim\\Slim\\DeferredCallable.php(57): call_user_func_array(Object(Closure), Array)\n#32 [internal function]: Slim\\DeferredCallable->__invoke(Object(Slim\\Http\\Request), Object(Slim\\Http\\Response), Object(Closure))\n#33 D:\\XAMPP7.4\\htdocs\\Slim_auth - test\\vendor\\slim\\slim\\Slim\\MiddlewareAwareTrait.php(70): call_user_func(Object(Slim\\DeferredCallable), Object(Slim\\Http\\Request), Object(Slim\\Http\\Response), Object(Closure))\n#34 D:\\XAMPP7.4\\htdocs\\Slim_auth - test\\vendor\\slim\\slim\\Slim\\MiddlewareAwareTrait.php(117): Slim\\App->Slim\\{closure}(Object(Slim\\Http\\Request), Object(Slim\\Http\\Response))\n#35 D:\\XAMPP7.4\\htdocs\\Slim_auth - test\\vendor\\slim\\slim\\Slim\\App.php(392): Slim\\App->callMiddlewareStack(Object(Slim\\Http\\Request), Object(Slim\\Http\\Response))\n#36 D:\\XAMPP7.4\\htdocs\\Slim_auth - test\\vendor\\slim\\slim\\Slim\\App.php(297): Slim\\App->process(Object(Slim\\Http\\Request), Object(Slim\\Http\\Response))\n#37 D:\\XAMPP7.4\\htdocs\\Slim_auth - test\\config\\boot.php(32): Slim\\App->run()\n#38 D:\\XAMPP7.4\\htdocs\\Slim_auth - test\\public\\index.php(3): require('D:\\\\XAMPP7.4\\\\htd...')\n#39 {main}"
Thiru
  • 17
  • 4
  • Please share the whole error message you are receiving. – Laisender May 31 '22 at 12:38
  • error all updated help me – Thiru May 31 '22 at 12:41
  • Not sure, but your columns are defined as _not null_, but you are only creating the entity with _first_name_ and _last_name_. Try setting _displayErrorDetails_ to true to see more details about the error. See https://www.slimframework.com/docs/v3/handlers/error.html#default-error-handler – waterloomatt May 31 '22 at 12:49
  • yes but not NULL have DEFAULT value – Thiru May 31 '22 at 12:51
  • What about your `id` column? Should that be auto-increment? Try setting the id column to a value that you know doesn't exist in the DB. – waterloomatt May 31 '22 at 12:52
  • id is auto increment in my DB im not mention it here – Thiru May 31 '22 at 12:59
  • any one have solution ? – Thiru Jun 01 '22 at 05:12
  • Have you set _displayErrorDetails_ (see link above) to true? Currently, you are getting a 500 error which is a generic server error and no one can really see what is going on. I'd spend some time trying to get a more detailed error message. You can also set it when configuring your connection, https://www.slimframework.com/docs/v3/cookbook/database-eloquent.html#configure-eloquent – waterloomatt Jun 02 '22 at 20:07
  • 1
    thank you all guys. i got my mistake i did not add create_at and updated_at on my database table. after i done it work fine. – Thiru Jun 03 '22 at 04:56

2 Answers2

0
CREATE TABLE `users` (
  `id` bigint(20) NOT NULL,
  
  `first_name` varchar(32) NOT NULL,
  `last_name` varchar(32) NOT NULL,
  `created_on` datetime DEFAULT NULL,
  `is_deleted` tinyint(4) NOT NULL DEFAULT 0,
  `is_active` tinyint(4) NOT NULL DEFAULT 1,
  `deleted_on` datetime DEFAULT NULL,
  `deleted_by` int(11) DEFAULT NULL
  `created_at`  timestamp DEFAULT NULL
  `updated_at`  timestamp DEFAULT NULL
Thiru
  • 17
  • 4
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 03 '22 at 11:04
0

controller function

public function set($request, $response, $args) {
  $user = new Models\User;

  $user->first_name = $args['first_name'];
  $user->last_name = $args['last_name'];        

  $user->save();
}
Tim Lewis
  • 27,813
  • 13
  • 73
  • 102