Laravel API 中的数据库插入
database insertion in Laravel API
在尝试插入数据库时,出现错误 - SQLSTATE[23502]: Not null violation: 7 ERROR: null value in column \"city\" of relation \"personal_details\" violates not-null constraint\nDETAIL: Failing row contains (12, abc, null, fff, dgh, null, 2021-06-28 08:34:53, 2021-06-28 08:34:53). (SQL: insert into \"personal_details\" (\"pincode\", \"district\", \"state\", \"updated_at\", \"created_at\") values (abc, fff, dgh, 2021-06-28 08:34:53, 2021-06-28 08:34:53) returning \"id\")",
。我是初学者,我不知道下面的代码有什么问题。此外,我想 return 一个 json 错误消息,如果除了 $validator->fails()
条件之外插入失败,就像我在我的代码中评论的那样。如果你有什么建议请帮助我。
控制器
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\PersonalDetails;
use Illuminate\Support\Facades\Validator;
class PersonalDetailsAdmin extends Controller
{
public function insert(Request $request){
$validator = Validator::make(
$request->all(),
[
'pincode' => 'required|string',
'city' => 'required|string',
'district' => 'required|string',
'state' => 'required|string',
'country' => 'string',
]
);
if ($validator->fails()) {
return response()->json($validator->errors(), 400);
}
$obj = new PersonalDetails();
$obj->pincode=$request->pincode;
$obj->district=$request->district;
$obj->state=$request->state;
$obj->save();
return response()->json(['message'=>'success','data'=>$obj]);
/* else{
return response()->json(['message'=>'Failed'], 404);
}*/
}
}
型号
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class PersonalDetails extends Model
{
use HasFactory;
protected $table='personal_details';
protected $guarded=['id'];
}
这是因为命名空间不正确。在 PersonalDetails
文件中,namespace App\Models;
意味着 PersonalDetails
model/file 存储在路径 App\Models;
中,但是在您的 PersonalDetailsAdmin
控制器中,您正试图导入 PersonalDetails
模型来自 App\Http\Models\PersonalDetails;
正确引用您的文件,一切都应该没问题。
也像这样正确实例化模型$obj = new PersonalDetails();
在尝试插入数据库时,出现错误 - SQLSTATE[23502]: Not null violation: 7 ERROR: null value in column \"city\" of relation \"personal_details\" violates not-null constraint\nDETAIL: Failing row contains (12, abc, null, fff, dgh, null, 2021-06-28 08:34:53, 2021-06-28 08:34:53). (SQL: insert into \"personal_details\" (\"pincode\", \"district\", \"state\", \"updated_at\", \"created_at\") values (abc, fff, dgh, 2021-06-28 08:34:53, 2021-06-28 08:34:53) returning \"id\")",
。我是初学者,我不知道下面的代码有什么问题。此外,我想 return 一个 json 错误消息,如果除了 $validator->fails()
条件之外插入失败,就像我在我的代码中评论的那样。如果你有什么建议请帮助我。
控制器
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\PersonalDetails;
use Illuminate\Support\Facades\Validator;
class PersonalDetailsAdmin extends Controller
{
public function insert(Request $request){
$validator = Validator::make(
$request->all(),
[
'pincode' => 'required|string',
'city' => 'required|string',
'district' => 'required|string',
'state' => 'required|string',
'country' => 'string',
]
);
if ($validator->fails()) {
return response()->json($validator->errors(), 400);
}
$obj = new PersonalDetails();
$obj->pincode=$request->pincode;
$obj->district=$request->district;
$obj->state=$request->state;
$obj->save();
return response()->json(['message'=>'success','data'=>$obj]);
/* else{
return response()->json(['message'=>'Failed'], 404);
}*/
}
}
型号
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class PersonalDetails extends Model
{
use HasFactory;
protected $table='personal_details';
protected $guarded=['id'];
}
这是因为命名空间不正确。在 PersonalDetails
文件中,namespace App\Models;
意味着 PersonalDetails
model/file 存储在路径 App\Models;
中,但是在您的 PersonalDetailsAdmin
控制器中,您正试图导入 PersonalDetails
模型来自 App\Http\Models\PersonalDetails;
正确引用您的文件,一切都应该没问题。
也像这样正确实例化模型$obj = new PersonalDetails();