【问题标题】:keep getting an error while saving data into database in laravel在laravel中将数据保存到数据库时不断出错
【发布时间】:2023-03-12 19:33:02
【问题描述】:

我正在使用 laravel 版本 "laravel/framework": "5.8.*" 我想通过发布请求将数据保存到数据库中,我的发布数据是

我已经搜索了这么多主题,但我无法获得有关此特定主题的任何信息。如果有人可以,这对我很有帮助。 我在 laravel 中看到了我以前的代码,它们是这种类型的代码,但它们工作正常,为什么这没有保存并出现错误。

{
       "userID": 11,
      "fullname": "dharmendra shah",
      "fatherName": "manohar shah",
      "Mothername": "manorama devi",
      "dobdate": "21",
      "dobmonth": "August",
      "dobYear": "1999",
      "caste": "OBC",
      "casteCertificateIs": "state",
      "emailAddress": "dharmonly1999@gmail.com",
      "gender": "Male",
      "handiCappedStatus": "Not handicapped",
      "nationality": "india",
      "state": "Rajasthan",
      "district": "bikaner",
      "tahsil": null,
      "village": "NA",
      "maritalStatus": "UnMarried",
      "spouseName": null,
      "children": null
}

在 laravel 路由中,我的控制器是:

 public function store(Request $request)
{
    $this->validate($request, [
        'fullname' => 'required|max:25',
        'fatherName' => 'required|max:25',
        'Mothername' => 'required|max:25',
        'dobdate' => 'required',
        'dobmonth' => 'required',
        'dobmonth' => 'required',
        'dobYear' => 'required',
        'caste' => 'required',
        'casteCertificateIs'=>'required',
        'emailAddress' => 'required|email',
        'gender' => 'required',
        'handiCappedStatus' => 'required',
        'nationality' => 'required',
        'state' => 'required',
        'district' => 'required',
        'block' => 'sometimes|max:20',
        'tahsil' => 'sometimes|max:20',
        'village' => 'sometimes|max:20',
        'maritalStatus' => 'required',
        'spouseName' => 'sometimes|max:20',
        'children' => 'sometimes|max:5|integer'
    ]);

    $userInformationSave = new BauserInformation([
        'userID' => \Auth::user()->id,
        'fullname' => $request->get('fullname'),
        'fatherName' => $request->get('fatherName'),
        'Mothername' => $request->get('Mothername'),
        'dobdate' => $request->get('dobdate'),
        'dobmonth' => $request->get('dobmonth'),
        'dobYear' => $request->get('dobYear'),
        'caste' => $request->get('caste'),
        'casteCertificateIs' => $request->get('casteCertificateIs'),
        'emailAddress' => $request->get('emailAddress'),
        'gender' => $request->get('gender'),
        'handiCappedStatus' => $request->get('handiCappedStatus'),
        'nationality' => $request->get('nationality'),
        'state' => $request->get('state'),
        'district' => $request->get('district'),
        'block' => $request->get('block'),
        'tahsil' => $request->get('tahsil'),
        'village' => $request->get('village'),
        'maritalStatus' => $request->get('maritalStatus'),
        'spouseName' => $request->get('spouseName'),
        'children' => $request->get('children')
    ]);

    $userInformationSave->save();
       return redirect()->route('home')->with('message', 'Your information is saved now');
}

型号是

class BauserInformation extends Model
{
    protected $table = ['userFinalInformation'];
    protected $fillable = ['userID', 'fullname', 'fatherName', 'Mothername', 'dobdate', 'dobmonth', 'dobYear', 'caste', 'casteCertificateIs', 'emailAddress', 'gender', 'handiCappedStatus', 'nationality', 'state', 'district', 'Block', 'tahsil', 'village', 'maritalStatus', 'spouseName', 'children'];
}

我得到的错误参数是

 ErrorException (E_NOTICE)
Array to string conversion

【问题讨论】:

  • 首先,使用$table = ['userFinalInformation']; 而不是仅使用$table = 'userFinalInformation'; 对您来说是否可以正常工作?

标签: php database laravel error-handling


【解决方案1】:

你把事情复杂化了。

在控制器顶部放置:use Illuminate\Support\Facades\Validator;

删除$userInformationSave

验证输入:

$validator = Validator::make($request->all(), [
    'fullname' => ['required', 'max:25'],
    ... every other input like I have done
]);
if ($validator->fails()) {
    return redirect('/bla bla bla/create')->withErrors($validator);
}

并保存模型:

BauserInformation::create($request->all());

编辑 #1 在您的模型中,您应该像这样声明表和 A_I:

protected $table = 'userFinalInformation';
protected $primaryKey = 'userID';

我认为这可以完成这项工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-15
    • 2020-06-09
    相关资源
    最近更新 更多