简体   繁体   中英

Database update - column not found laravel

I am getting this error message:

SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list' (SQL: update handymen set 0 = job_id, 1 = 2, updated_at = 2016-04-14 09:14:49 where id = 1)

My db structure looks like this:

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateHandymenTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
public function up()
{
    Schema::create('handymen', function (Blueprint $table) {
        $table->increments('id');
        $table->string('first_name');
        $table->string('last_name');
        $table->string('street');
        $table->string('postcode');
        $table->string('town');
        $table->string('skills');


        $table->integer('job_id')->unsigned();
        $table->foreign('job_id')->references('id')->on('jobs')->onDelete('cascade');


        $table->timestamps();
        });
    }
/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::table('handymen', function (Blueprint $table) {
        $table->dropForeign('handymen_job_id_foreign');
        $table->dropColumn('job_id');
    });
    }
}

Controller:

function jobassign(Request $request)
{
     $job_id = $request->input('job_id');
     $handymanId = $request->input('handymanid');
    $job = Handyman::where('id', $handymanId)->update(['job_id', $job_id]);
    return redirect()->back()->with('status', trans('Handyman has been successfully assigned to this job.'));
    //return view('layouts/skilledHandyman', ['jobs' => $jobs, 'skilledHandyman' => $skilledHandyman]);
}
    function skilledHandyman($handymanId)
{
    $skilledHandyman = Handyman::find($handymanId);
    $jobs = Job::all();
    return view('layouts/skilledHandyman', ['jobs' => $jobs, 'skilledHandyman' => $skilledHandyman]);
}

View:

@extends('layouts.master')

@section('skilledHandyman')

@section('content')
    <h1>Handyman details</h1>
<ul>
    <li>First Name:{{$skilledHandyman->first_name}}</li>
    <li>Last Name:{{$skilledHandyman->last_name}}</li>
    <li>Street:{{$skilledHandyman->street}}</li>
    <li>Postcode:{{$skilledHandyman->postcode}}</li>
    <li>Town:{{$skilledHandyman->town}}</li>
    <li>Skills:{{$skilledHandyman->skills}}</li>
</ul>

    <form action="{{url('jobassign')}}" method="POST">
    {{ csrf_field() }}
     @foreach ($jobs as $job)
        <div>
            <label>{{$job->name}}</label>
            <input type='checkbox' value='{{$job->id}}' name='job_id'/>
            <input type="hidden" name="handymanid" value="{{$skilledHandyman->id}}">
        </div>
    @endforeach
    <input type="submit" name="submitBtn" value="Assign Job">
</form>
@endsection

Can someone tell me why is this happening and how to fix it please?

Thank You

You have an error/typo within your update query, as you were passing an array within update query so you need to define your columns as keys

$job = Handyman::where('id', $handymanId)->update(['job_id', $job_id]);
                                                          ^^^^

should be

$job = Handyman::where('id', $handymanId)->update(['job_id' => $job_id]);
                                                           ^^^^

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