I have a HTML website that post data to laravel API but the data is not saving in the database and it is not showing any error message. This is the jquery im using inside the html page
<script>
$("#submit").click(function(){
var inputs = {};
inputs.fullname = $("#Fullname").val();
inputs.email = $("#Email").val();
inputs.constituent = $("#Constituent").val();
inputs.subject = $("#Subject").val();
inputs.message = $("#Message").val();
$.ajax({
url : "http://127.0.0.1:8000/api/complaints/",
type: "POST",
data: inputs,
returnType: "JSON",
success: function(response)
{
alert("Complaint sent!");
},
error: function(a, b, c)
{
alert("Error!");
}
});
})
this is the api.php
Route::post("complaints", [ApiController::class, 'store']);
And this is my controller
{
$v = new Feedback;
$v->FullName = $request->FullName;
$v->Constituent = $request->Constituent;
$v->Email = $request->Email;
$v->Subject = $request->Subject;
$v->Message = $request->Message;
$v->save();
}
Are you sure your laravel model and database migration files are correct? Make sure the db configs in the env file.
note: no migration table file is required. The parts I have given below are sufficient for the process.
.env db config
DB_CONNECTION=mysql
DB_HOST=xxx.xxx.xxx
DB_PORT=3307
DB_DATABASE=xxx
DB_USERNAME=xxx
DB_PASSWORD=xxxxx
Table Model
class XXModel extends Model
{
use HasFactory, Notifiable, SoftDeletes;
protected $table = 'dbq_xx_table'; // db table name
protected $primaryKey = 'id';
public $timestamps = true;
protected $fillable = [ // db colums
'xx_1',
'xx_2'
];
}
Table data create
return XXModel::create([
'xx_1' => $request->xx_1,
'xx_2' => $request->xx_2,
]);
Table data update
return XXModel::where('id',$request->id)->update([
'xx_1' => $request->xx_1,
]);
The object returned by created is the model register object
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.