How to inset data with eloquent models in slim out of the laravel?

396 Views Asked by At

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}"
2

There are 2 best solutions below

1
Thiru On
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
0
Smaili Amorim On

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();
}