Put the column name you want to add into the fillable array in your model.
And it's better return a response.
return response("Ok");
And of course remove dump line if not testing.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello everyone.
I am having problems with storing data in postgresql database.
This is the migration.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('ciudadanos', function (Blueprint $table) {
$table->id();
$table->bigInteger('cuil')->unique();
$table->string('nombre');
$table->string('apellido');
$table->string('email');
$table->string('password');
$table->timestamp('email_verified_at')->nullable();
$table->boolean('confirmed')->default(0);
$table->string('confirmation_code')->nullable();
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('ciudadanos');
}
};
The php artisan migrate runs ok!
This is the model:
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Ciudadano extends Model
{
protected $table = 'ciudadanos';
protected $fillable =[
'cuil',
'nombre',
'apellido',
'email',
'password',
];
protected $hidden = [
'password',
'remember_token',
];
protected $casts = [
'email_verified_at' => 'datetime',
];
}
The api.php is this:
Route::post('ciudadano', 'App\Http\Controllers\CiudadanoController@store');
and this is the controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Ciudadano;
class CiudadanoController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
dump($request);
$this->validate($request, [
'cuil'=>'required',
'nombre'=>'required',
'apellido'=>'required',
'email'=>'required',
'password'=>'required',
]);
$ciudadano = Ciudadano::create($request->all());
$ciudadano->save();
return ("OK");
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
The problem is that it does not return "OK" and I can not debug it because console does not show anything.
If I set the return before $ciudadano = Ciudadano::create($request->all()); it returns "OK"
So the problem is with this line Ciudadano::create($request->all()) The client axios is this:
axios({
method: 'post',
url: 'http://127.0.0.1:8000/api/ciudadano',
data: {"cuil":"xxxxx",nombre":"xxx", "apellido":"xxx", "email":"[email protected]", "password":"xxxxx"},
headers: {
"Content-Type": "application/json",
'Access-Control-Allow-Origin' : '*',
},
}).then(function (response) {
console.log(response)
});
What can be the problem here?
you say console does not show anything. But what is the status code on the network tab? also does the request on the network tab has any response preview?
One bonus tip, the Controller@validated method returns an array with the validated data, so you can avoid calling $request->all() which can bite your feet if you have a loose $fillable property:
public function store(Request $request)
{
$validated = $this->validate($request, [
'cuil' => 'required',
'nombre' => 'required',
'apellido' => 'required',
'email' => 'required',
'password' => 'required',
]);
$ciudadano = Ciudadano::create($validated);
$ciudadano->save();
return "OK";
}
Please or to participate in this conversation.