dtuct88226 2016-04-13 18:20
浏览 21
已采纳

labox中数据库的复选框值

On page skilledHandyman, I have a handyman_id stored from a previous page and a form with checkboxes with list of jobs available, what I want to do is to update foreign key job_id in "handymen" table using whatever checkbox has been selected by taking job_id from table "jobs". Value of job_id is in the checkbox as it can be seen in the code, it just has to be taken and update a job_id foreign key within handyman. I have set up relationships hasMany with jobs table and belongsTo from job to handyman. This is what I have at the moment but it obviously doesn't work as intended.

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

} If any other files are needed I will update the page when requested, please help me here

  • 写回答

1条回答 默认 最新

  • douzai3399 2016-04-13 20:25
    关注

    Change this

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

    to

      <input type='checkbox' value='{{$job->id}}' name='job_id'/>
     <input type="hidden" name="handymanid" value="{{$skilledHandyman->id}}">
    

    and in the 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.'));
        }
    

    in ur routes file change the form action

    <form action="{{url('jobassign')}}" method="POST">
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度