简体   繁体   中英

How can I update all fields except for 1 in one table, and update 1 input in a second table?

I have a basic form for a candidate to fill out a Profile. One of the required fields is to upload a Resume, with a resume_id column, int(11) , in the job_seeker_profiles table. I have another table called Resumes, I thought I created the relationship properly but maybe I didn't. In my store method, everything in the form gets stored in the job_seeker_profiles table, except the resume. This gets stored in the resume's table, column called 'file' and the id from that record gets inserted into the resume_id column in the job_seekers_profiles table with the relationship I created earlier. My store method is working perfectly, but my update method is not. It keeps inserting the actual filename inside of the resume_id column and nothing gets inserted into the Resumes table.

AdminJobSeekerProfileController.php file:

public function store(JobSeekerProfileCreateRequest $request)
    {
        $input = $request->all();

        $user = Auth::user();

        if($file = $request->file('resume_id')) {

            $name = time() . $file->getClientOriginalName();

            $name = preg_replace('/\s+/', '-', $name);

            $file->storeAs('resumes', $name);

            $resume = Resume::create(['file'=>$name]);

            $input['resume_id'] = $resume->id;

        }

        if($file = $request->file('video_one_id')) {

            $name = time() . $file->getClientOriginalName();

            $name = preg_replace('/\s+/', '-', $name);

            $file->storeAs('videos', $name);

            $video = Video::create(['file'=>$name]);

            $input['video_one_id'] = $video->id;

        }

        if($file = $request->file('video_two_id')) {

            $name = time() . $file->getClientOriginalName();

            $name = preg_replace('/\s+/', '-', $name);

            $file->storeAs('videos', $name);

            $video = Video::create(['file'=>$name]);

            $input['video_two_id'] = $video->id;

        }

        if($file = $request->file('video_three_id')) {

            $name = time() . $file->getClientOriginalName();

            $name = preg_replace('/\s+/', '-', $name);

            $file->storeAs('videos', $name);

            $video = Video::create(['file'=>$name]);

            $input['video_three_id'] = $video->id;

        }

        $user->jobSeekerProfile()->create($input);

        Alert::success('Your Profile has been created successfully!')->autoclose(5000);

        return redirect('/admin/job-seeker/profile');
    }

public function update(JobSeekerProfileCreateRequest $request)
    {

        $input = $request->all();

        $user = Auth::user();

        $user->jobSeekerProfile()->update($input);

        // Sweet Alert
        Alert::success('Your Profile has been updated successfully!')->autoclose(5000);

        return redirect('/admin/job-seeker/profile');
    }

This is the error I'm getting:

SQLSTATE[42S22]: Column not found: 1054 Unknown column '_method' in 'field list'

Here is a screenshot of the full error, you can see on the screenshot it's trying to insert the name of the file uploaded into the resume_id column instead of an integer.

`resume_id` = Peter_Brewed_Resume_2020.docx,

在此处输入图片说明

your problem is $request->all(); . You rarely want to allow users to arbitrarily put data into your DB. At minimum, you should specify which fields they are allowed to update. Better would be to scrub the data for appropriateness before committing to the DB.

You probably want something like

$request->only(['first_name', 'last_name', 'email', 'date_of_birth' ..... ]);

Also, you need to place your files the same way you did in the create method.

I was missing the 'files'=>true on my edit Form:

{!! Form::model($jobSeekerProfiles, ['method'=>'PATCH', 'action'=> ['AdminJobSeekerProfileController@update', $jobSeekerProfiles->id], 'files'=>true]) !!}

Combined with using $request->only it is now working.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM