数据库更新 - 未找到列的laravel

时间:2016-04-14 09:18:22

标签: php mysql laravel laravel-5 laravel-5.2

我收到此错误消息:

  

SQLSTATE [42S22]:未找到列:1054未知列' 0'在'领域   表' (SQL:更新handymen设置0 = job_id,1 = 2,updated_at   = 2016-04-14 09:14:49其中id = 1)

我的数据库结构如下所示:

<?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');
    });
    }
}

控制器:

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]);
}

查看:

@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

有人可以告诉我为什么会发生这种情况以及如何解决它?

谢谢

1 个答案:

答案 0 :(得分:2)

您的更新查询中有错误/拼写错误,因为您在更新查询中传递了一个数组,因此您需要将列定义为键

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

应该是

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