laravel 5.4 传递数据创建控制器

laravel 5.4 pass data to create controller

我有一个视图 /station/{id},其中有一个新闻广播列表。我想在列表中创建新闻广播。

station.show.blade:

<td colspan="11">
    @can('newscast_edit')
    <a href="{{ route('newscast.create', [$station->id]) }}"
       class="btn btn-xs btn-info">Add Newscast</a>
    @endcan
</td>

NewscastsController 中:
(更改前)

/**
 * Show the form for creating new Newscast.
 *
 * @return \Illuminate\Http\Response
 */
public function create()
{
    if (!Gate::allows('newscast_create')) {
        return abort(401);
    }
    $relations = [
        'for_stations' => \App\Station::get()->pluck('calls', 'id')->prepend('Please select', ''),
    ];
    $backUrl = Session('_previous');
    session(['backUrl' => $backUrl]);

    return view('newscast.create', $relations);
}

newscast.create.blade:

<div class="panel panel-default">
    <div class="panel-body">
        <div class="row">
            <div class="col-xs-12 form-group">
                {!! Form::label('for_station_id', 'For Station*', ['class' => 'control-label']) !!}
                {!! Form::select('for_station_id', $for_stations, old('for_station_id'), ['class' => 'form-control
                select2']) !!}
                <p class="help-block"></p>
                @if($errors->has('for_station_id'))
                <p class="help-block">
                    {{ $errors->first('for_station_id') }}
                </p>
                @endif
            </div>
        </div>
    </div>
</div>

我的目标是删除 NewscastsController 中的 relations 行并将站 ID 传递给 newscast.create.blade代替 select,如 for_station_id

我已经尝试了几种方法来解决这个问题,包括 and https://laravel.io/forum/07-26-2014-routing-passing-parameters-to-controller 但没有成功。

create函数修改为public function create($station_id) 导致此错误: ReflectionException in RouteDependencyResolverTrait.php line 57: Internal error: Failed to retrieve the default value

我也用谷歌搜索了该错误消息,但没有提出任何改进意见。 我敢肯定,这可能是在炫耀我在 php 和 Laravel 上仍然是个菜鸟。我错过了什么?

因为您正在通过传递参数来修改默认 create() 函数,所以您不能再从 Route::resource() 开始使用它。因此,你必须手动定义它,上面 Route::resource('newscast')

Route::get('newscast/{station_id}', 'NewscastController@create')->name('newscast.create');

//Define your resource routes, excluding 'create'
Route::resource('newscast', 'NewscastsController', ['except' => ['create']);

然后在你的控制器中,你可以毫无问题地传递你的参数

public function create($station_id)

解决方案是进行以下更改:

NewscastsController 中:

public function create($id, $calls)
{
    if (! Gate::allows('newscast_create')) {
        return abort(401);
    }

    $backUrl = Session('_previous');
    session(['backUrl' => $backUrl]);
    return view('newscast.create', [ 'station_id' => $id, 'calls' => $calls ]);
}  

(当第一个参数工作时,我最终传递了第二个参数。)

web.php(路由器)中:
Route::get('newscast/create/{station_id}/{calls}', 'NewscastsController@create')->name('newscast.create');
station.show.blade:
<a href="{{ route('newscast.create', [$station->id, $station->calls]) }}" class="btn btn-xs btn-info">Add Newscast</a>

newscast.create.blade中:

<h3 class="page-title">Add Newscast for {{ $calls }}</h3>
{!! Form::open(['method' => 'POST', 'route' => ['newscast.store']]) !!}

<div class="panel panel-default">
    <div class="panel-body">
        <div class="row">
            <div class="col-xs-12 form-group">
                {!! Form::hidden('for_station_id', $station_id, old('for_station_id'), ['class' => 'form-control']) !!}
            </div>
        </div>

这似乎基本上就是 @EddyTheDove 推荐的内容,但要么是我没有正确理解该推荐,要么是我遇到了其他问题导致我第一次进行这些更改时出现错误。对我来说最终的解决方案是当我偶然发现 Laravel 5 ( "barryvdh/laravel-debugbar": "^2.3",) 的 DebugBar 包并且能够挖掘整个请求时。