尝试在单个函数中使用更新和创建的问题

时间:2019-05-21 13:04:16

标签: laravel crud

我想为一个表创建一行,但是要更新另一个表中的列,为此,我同时使用了update()create(),但是它始终以相同的错误结尾,不管我怎么放下它。

See error here

    public function createuserinfo(Request $request)
    {
        $user = Auth::user();

        $userupdate = User::update([
            'status' => '1'
        ]);

        $infocreate = Personal::create([
            'user_id' => Auth::user()->id,
            'first_name' => $request->first_name,
            'last_name' => $request->last_name,
            'email' => $request->email,
            'password' => $request->password,
            'address' => $request->address,
            'zipcode' => $request->zipcode,
            'city' => $request->city,
            'phone_number' => $request->phone_number,
            'ss_number' => $request->ss_number,
            'document_number' => $request->document_number,
            'license_number' => $request->license_number,
            'expiration_date' => $request->expiration_date,
            'employer' => $request->employer,
            'jobtitle' => $request->jobtitle,
            'contract' => $request->contract,
            'start_date' => $request->start_date,
            'general_practitioner' => $request->general_practitioner,
            'dentist' => $request->dentist,
            'health_insurance' => $request->health_insurance,
            'contact_one_name' => $request->contact_one_name,
            'contact_one_relation' => $request->contact_one_relation,
            'contact_one_address' => $request->contact_one_address,
            'contact_one_zipcode' => $request->contact_one_zipcode,
            'contact_one_city' => $request->contact_one_city,
            'contact_one_phone_number' => $request->contact_one_phone_number,
            'contact_two_name' => $request->contact_two_name,
            'contact_two_relation' => $request->contact_two_relation,
            'contact_two_address' => $request->contact_two_address,
            'contact_two_zipcode' => $request->contact_two_zipcode,
            'contact_two_city' => $request->contact_two_city,
            'contact_two_phone_number' => $request->contact_two_phone_number
        ]);

        return $userupdate . $infocreate . redirect()->route('users.details');
    }

我需要它重定向到用户详细信息页面,并添加/更改他们的信息。我希望有人能够帮助我解决这个问题。

4 个答案:

答案 0 :(得分:2)

也许试试这个

@Cacheable

答案 1 :(得分:1)

您正在尝试更新用户而不传递其ID,这肯定会导致错误。

尝试一下:

public function createuserinfo(Request $request)
{ 
 $user = Auth::user();

 $userupdate = User::find($user->id);
 $userupdate->status = '1';
 $userupdate->save();

 $infocreate = Personal::create([
        'user_id' => $user->id,
        'first_name' => $request->first_name,
        'last_name' => $request->last_name,
        'email' => $request->email,
        'password' => $request->password,
        'address' => $request->address,
        'zipcode' => $request->zipcode,
        'city' => $request->city,
        'phone_number' => $request->phone_number,
        'ss_number' => $request->ss_number,
        'document_number' => $request->document_number,
        'license_number' => $request->license_number,
        'expiration_date' => $request->expiration_date,
        'employer' => $request->employer,
        'jobtitle' => $request->jobtitle,
        'contract' => $request->contract,
        'start_date' => $request->start_date,
        'general_practitioner' => $request->general_practitioner,
        'dentist' => $request->dentist,
        'health_insurance' => $request->health_insurance,
        'contact_one_name' => $request->contact_one_name,
        'contact_one_relation' => $request->contact_one_relation,
        'contact_one_address' => $request->contact_one_address,
        'contact_one_zipcode' => $request->contact_one_zipcode,
        'contact_one_city' => $request->contact_one_city,
        'contact_one_phone_number' => $request->contact_one_phone_number,
        'contact_two_name' => $request->contact_two_name,
        'contact_two_relation' => $request->contact_two_relation,
        'contact_two_address' => $request->contact_two_address,
        'contact_two_zipcode' => $request->contact_two_zipcode,
        'contact_two_city' => $request->contact_two_city,
        'contact_two_phone_number' => $request->contact_two_phone_number
    ]);

    return $userupdate . $infocreate . redirect()->route('users.details');
}

答案 2 :(得分:1)

使用同一对象代替staticUser模型的调用。

    $user = Auth::user();
    $updateUser = $user->update('status' => '1']);

此外,您可以使用save方法。

    $user = Auth::user();
    $user->status = '1';
    $user->save();

引用Eloquent ORM

答案 3 :(得分:1)

您具有该createOrUpdate()的laravel函数。 而且,如果要使用update()更新行,则需要指定要更新的内容。

您想使用User :: update()更新用户,但Model不知道您选择哪个用户

User::find(1)->update() or User::where('name', 'test')->update() 

会知道的。

或者您的情况

$user = Auth::user();

 $user->update([
    'status' => '1'
 ]);