labox中数据库的复选框值

时间:2016-04-13 18:20:17

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

在熟练的HandDandman页面上,我有一个handyman_id存储在上一页和一个带有复选框的表单,其中包含可用的作业列表,我想要做的是更新" handymen"中的外键job_id。通过从表" jobs"中获取job_id来选择使用任何复选框的表格。 job_id的值在复选框中,因为它可以在代码中看到,它只需要在杂工中更新job_id外键。我已经建立了关系hasMany与jobs表和belongsTo从job到handyman的关系。这就是我现在所拥有的,但它显然没有按预期工作。

@extends('layouts.master')

@section('skilledHandyman', 'Add Job')
        @section('header2')
            <ul>
                <li><a href="{{url('assignjob')}}">Assign job</a></li>
            </ul>
        @show
@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('skilledHandyman')}}" method="POST">
    {{ csrf_field() }}
     @foreach ($jobs as $job)
        <div>
            <label>{{$job->name}}</label>
            <input type='checkbox' value='{{$job->id}}' name='jobs[]'/>
        </div>
    @endforeach
    <input type="submit" name="submitBtn" value="Assign Job">
</form>
@endsection
function jobassign(Request $request, $id)
{
    $job = Handyman::where('handyman_id', $handymanId)->update(['job_id', $request->job_id]);
    return redirect()->back()->with('status', trans('Handyman has been successfully assigned to this job.'));
}
function skilledHandyman($handymanId)
{
    $skilledHandyman = Handyman::find($handymanId);
    $jobs = Job::all();
    return view('layouts/skilledHandyman', ['jobs' => $jobs, 'skilledHandyman' => $skilledHandyman]);

}

Route::group(['middleware' => ['web']], function () {

Route::get('home', 'HandymanController@home');

Route::get('search', 'HandymanController@search');

Route::get('details/{skill}', 'HandymanController@details');

Route::get('skilledHandyman/{handymanId}', 'HandymanController@skilledHandyman');

Route::post('jobassign', 'HandymanController@jobassign');

//Route::get('assignjob/{handymanId}', 'HandymanController@assignJob');

Route::get('addjob', 'HandymanController@addJob');

Route::post('addjform', 'HandymanController@addjForm');

Route::get('jobs', 'HandymanController@jobs');

Route::get('jobsdetails/{jobId}', 'HandymanController@jobsdetails');

Route::get('deletejob', 'HandymanController@deleteJob');

Route::post('deletejform', 'HandymanController@deletejForm');

});

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

} 如果需要任何其他文件,我会在要求时更新页面,请在这里帮助我

1 个答案:

答案 0 :(得分:1)

更改此

 <input type='checkbox' value='{{$job->id}}' name='jobs[]'/>

  <input type='checkbox' value='{{$job->id}}' name='job_id'/>
 <input type="hidden" name="handymanid" value="{{$skilledHandyman->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.'));
    }
在你的路线文件中 更改表单操作

<form action="{{url('jobassign')}}" method="POST">