尝试存储数据时的参数 #2,Laravel 8

Argument #2 when trying to store data, Laravel 8

我正在尝试为我的表单存储数据,但我不断收到

App\Http\Controllers\KeyresultController::store(): Argument #2 ($objective) must be of type App\Models\Objective, string given, called in /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Controller.php on line 54

这是我在里面的商店功能 KeyresultController.php

public function store(Request $request, Objective $objective)
{
    //

    $objective = Objective::where('id',$objective->id)->first();

    $request->validate([
        'keyresult_name' => 'required',
    ]);

     keyresult::create([
        'keyresult_name' => $request->keyresult_name,
        'keyresult_details' => $request->keyresult_details,
        'progress' => $request->progress
    ]);

    Deadline::create([
        'date' => $request->date,
        'until' => $request->until,
    ]);

    return redirect('/sistem/monitor/objective/details/{team}/{objective}')->with('status', 'keyresult and new Keyresult Successfully Added');
    
}

这是我来自 new_keyresult.balde.php 的表格:

<form method="POST" action="{{route('sistem.monitor.objective.details', [$team->id, $objective->id])}}">
                @csrf   
                <div class="form-group row">
                    <label for="newkeyresult" class="col-md-4 col-form-label text-md-right">Keyresult</label>
                    <div class="col-md-6">
                        <input type="text" id=newkeyresult name="keyresult_name" class="form-control @error ('keyresult_name') is-invalid @enderror">
                            @error('keyresult_name')
                            <div class="invalid-feedback">
                                {{ $message }}
                            </div>
                            @enderror
                    </div>
                </div>
                        <button type="submit" class="btn btn-primary">Save</button>
                        <a href="/sistem/monitor/objective/details/{{ $team->id }}/{{ $objective->id }}"><input type="button" class="btn btn-danger" value="Cancel"></a>
                    </div>
                </div>
            </form>

这是我的路线:

Route::get('/sistem/monitor/keyresult/new/{team}/{objective}', 'KeyresultController@create');


Route::post('/sistem/monitor/objective/details/{team}/{objective}', 'KeyresultController@store')

将路线更改为

Route::get('/sistem/monitor/keyresult/new/{team}/{objective}', [KeyresultController::class,'create']);


Route::post('/sistem/monitor/objective/details/{team}/{objective}', [KeyresultController::class],'store')

参考:https://laravel.com/docs/8.x/routing#the-default-route-files

并且在存储方法参数中你有

public function store(Request $request, Objective $objective)

但是根据路线你有参数 teamobjective 所以

public function store(Request $request,$team, Objective $objective)

您甚至可以通过 $request 实例

获取 objective id
public function store(Request $request) 
{

    $objective = Objective::where('id',$request->objective)->first();