Laravel更新联接表

时间:2018-09-06 14:14:47

标签: php laravel eloquent sql-update

所以我有2个具有OneToMany关系的表(项目和project_types)。

public function project_types()
{
    return $this->belongsTo('App\Models\ProjectType', 'project_type_id');
}

public function projects()
{
    return $this->hasMany('App\Models\Project', 'project_type_id');
}

我的ProjectController:

 public function update(Request $request, $id)
{
    if ($request->isMethod('get')){

        $types = ProjectType::all()
            ->pluck('name');

        // dd($types);
        return view('projects.form', compact('types'), ['project' => Project::find($id)]);
    }

    $rules = [
        'name' => 'required',
        'description' => 'required',
        'project_code' => 'required',
    ];

    $validator = Validator::make($request->all(), $rules);
    if ($validator->fails())
        return response()->json([
            'fail' => true,
            'errors' => $validator->errors()
        ]);

    $project = Project::find($id);
    $project->name = $request->name;
    $project->description = $request->description;
    $project->project_code = $request->project_code;
    $project->project_type_id = $request->project_type_id;
    $project->start_p = $request->start_p;
    $project->start_r = $request->start_r;
    $project->end_p = $request->end_p;
    $project->end_r = $request->end_r;
    $project->days_p = $request->days_p;
    $project->days_r = $request->days_r;
    $project->date_requested = $request->date_requested;

    $project->save();

    return response()->json([
        'fail' => false,
        'redirect_url' => url('projects')
    ]);
}

我的表单:

<div class="form-group row">
        {!! Form::label("project_types", trans('project.Type'),["class"=>"col-form-label col-md-3 col-lg-2"]) !!}
        <div class="col-md-8">
            {!! Form::select("project->project_type_id", $types, null, ["class"=>"form-control".($errors->has('project->project_type_id')?" is-invalid":""),'placeholder'=>'Project Type']) !!}
            <span id="error-name" class="invalid-feedback"></span>
        </div>
    </div>

在我的表单视图中,我有一个包含所有项目类型名称的下拉列表(因此我想我的关系很好),但是当我尝试更新项目信息时,该表单会为project_type_id发送NULL。我在控制台中没有任何错误。

任何人都可以帮忙!谢谢!

2 个答案:

答案 0 :(得分:0)

尝试这样的事情:

public function projects()
{
    return $this->hasMany(Project::class, 'project_type_id', 'id');
}

public function project_types()
{
    return $this->belongsTo(ProjectType::class, 'project_type_id', 'id');
}

然后在您的控制器中更改以下内容:

if ($request->isMethod('get')){

        $types = ProjectType::all()
            ->pluck('name');

        // dd($types);
        return view('projects.form', compact('types'), ['project' => Project::find($id)]);
    }

对此:

if ($request->isMethod('get')){

        $types = ProjectType::all()
            ->pluck('name');
        $project = Project::find($id);
        // dd($types);
        return view('projects.form', compact('types', 'project'));
    }

根据您的刀片服务器模板,我想您在Form::select()中有一些错误

Laravel docs Forms & HTML

Form::select('select_name', $types->toArray());

答案 1 :(得分:0)

确定发现了我的错误。我正在选择项目类型名称,我也需要ID。

 $types = ProjectType::all()
            ->pluck('name', 'id');

        return view('projects.form', compact('types', 'project'));

,其格式为:

 {!! Form::label("project_types", trans('project.Type'),["class"=>"col-form-label col-md-3 col-lg-2"]) !!}
        <div class="col-md-8">
            {!! Form::select("project_type_id", $types, null, ["class"=>"form-control".($errors->has('project_type_id')?" is-invalid":""),'placeholder'=>'Project Type']) !!}
            <span id="error-name" class="invalid-feedback"></span>
        </div>