使用laravel更新数据库

时间:2016-04-10 10:32:58

标签: php mysql laravel

让我解释一下情况,人正在搜索技能,当点击显示具有该技能的杂工列表时,当用户点击其中一个时,显示完整的详细信息。用户然后单击分配作业链接并进入带有作业的页面以及复选框,当选择了所需的作业,并单击提交按钮时,我希望数据库更新" job_id" " handymen"数据库。怎么可能这样做?

@extends('layouts.master')

@section('title', 'Assign Job')

@section('content')

@section('content')

<form action="{{url('assignjob')}}" 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 search()
{
    $skills = Skill::all();
    return view('layouts/search',['skills' => $skills]);
}
function details($skillId)
{
$skill = Skill::find($skillId);
$handymen = $skill->handymen;
return view('layouts/details', ['skill' => $skill,'handymen' => $handymen]);
}
function assignJob($handymanId)
{
    $assignJob = Hadnyman::find($handymanId);
    $jobs = Job::all();
    return view('layouts/assignjob',['jobs' => $jobs]);
}   
function jobassign(Request $request)
{
    return redirect('assignjob');
}
function skilledHandyman($handymanId)
{
    $skilledHandyman = Handyman::find($handymanId);
    return view('layouts/skilledHandyman', ['skilledHandyman' => $skilledHandyman]);
}

如果需要特定代码,请告诉我

1 个答案:

答案 0 :(得分:1)

你应该看看Eloquent Relationship。 杂工有很多工作

class Handymen extends Model {
  ...
  public function jobs() {
     return $this->hasMany(App\Job::class);
  }
}

在您的控制器中

function assignJob(Request $request, $id)
{
    $handymen = Handyman::findOrFail($id);

    // $request->get('jobs') = [1, 6, 7, etc...]
    $handymen->saveMany($request->get('jobs'));
    return ...;
}