Too few arguments to function App\Http\Controllers\EventController::edit(), 0 passed and exactly 1 expected

huangapple go评论105阅读模式
英文:

Too few arguments to function App\Http\Controllers\EventController::edit(), 0 passed and exactly 1 expected

问题

"嗨,各位,请帮助我,我是 Laravel 新手,我不知道为什么会出现这个错误。

Symfony\Component\Debug\Exception\FatalThrowableError
App\Http\Controllers\EventController::edit() 函数的参数太少,期望传入 1 个参数,但传入了 0 个。

这是我的 EventController:

public function show()
{
    $events = Event::all();
    return view('superadminpage.admin_event.admin_update_event')->with('events', $events);
}

/**
 * Show the form for editing the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function edit($id)
{
    $events = Event::find($id);
    return view('superadminpage.admin_event.admin_editform', compact('events', 'id'));
}

/**
 * Update the specified resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function update(Request $request, $id)
{
    $this->validate($request,[
        'title' => 'required',
        'color' => 'required',
        'start_date' => 'required',
        'end_date' => 'required',
    ]);

    $events = Event::find($id);

    $events->title = $request->input('title');
    $events->color = $request->input('color');
    $events->start_date = $request->input('start_date');
    $events->end_date = $request->input('end_date');

    $events->save();

    return redirect('admin_calendar')->with('success', '已添加');
}

这是我的路由:

Route::get('/admin_update_event', 'EventController@show');
Route::get('/admin_editform', 'EventController@edit');

这是我的 admin_update_event.blade.php:

@foreach ($events as $event)
    <tbody>
        <tr>
            <td>{{ $event->id}}</td>
            <td>{{ $event->title}}</td>
            <td>{{ $event->color}}</td>
            <td>{{ $event->start_date}}</td>
            <td>{{ $event->end_date}}</td>

            <th> <a href="{{ route('edit',['id' => $event->id, 'event' => 1]) }}">
                    编辑 </a>
            </th>

        </tr>
    </tbody>
@endforeach

这是我的 admin_editform.blade.php:

<form method="POST" action="{{action('EventController@update')}}">

    {{csrf_field() }}
    <div class="container">
        <div class="jumnbotron"> 
            <h1> 更新事件 </h1>
        <br>
    <input type="hidden" name="_method" value="UPDATE" />

    <div class="form-group">
        <label for="">输入事件的名称</label>
        <input type="text" class="form-control" name="title" placeholder="输入名称" value="{{ $events->title}}">
    </div>

    <div class="form-group">
        <label for="">选择颜色</label>
        <input type="color" class="form-control" name="color" placeholder="选择颜色" value="{{ $events->color}}">
    </div>

    <div class="form-group">
        <label for="">输入事件的开始日期</label>
        <input type="datetime-local" class="form-control" name="start_date" class="date" placeholder="输入开始日期" value="{{ $events->start_date}}">
    </div>
    
    <div class="form-group">
        <label for="">输入事件的结束日期</label>
        <input type="datetime-local" class="form-control" name="end_date" class="date" placeholder="输入结束日期" value="{{ $events->end_date}}">
    </div>
    {{ method_field('PUT') }}
    <input type="submit" name="submit" class="btn btn-primary" value="添加事件数据"/>

        </div>
    </div>
</form>
英文:

hi guys please help me im new to the laravel i don't know why i have this error

Symfony\Component\Debug\Exception\FatalThrowableError
Too few arguments to function App\Http\Controllers\EventController::edit(), 0 passed and exactly 1 expected

this is my EventController

 &#39; public function show()
{
$events = Event::all();
return view(&#39;superadminpage.admin_event.admin_update_event&#39;)-&gt;with(&#39;events&#39;, $events);
}
/**
* Show the form for editing the specified resource.
*
* @param  int  $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$events = Event::find($id);
return view(&#39;superadminpage.admin_event.admin_editform&#39;, compact(&#39;events&#39;, &#39;id&#39;));
}
/**
* Update the specified resource in storage.
*
* @param  \Illuminate\Http\Request  $request
* @param  int  $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$this-&gt;validate($request,[
&#39;title&#39; =&gt; &#39;required&#39;,
&#39;color&#39; =&gt; &#39;required&#39;,
&#39;start_date&#39; =&gt; &#39;required&#39;,
&#39;end_date&#39; =&gt; &#39;required&#39;,
]);
$events = Event::find($id);
$events-&gt;title = $request-&gt;input(&#39;title&#39;);
$events-&gt;color = $request-&gt;input(&#39;color&#39;);
$events-&gt;start_date = $request-&gt;input(&#39;start_date&#39;);
$events-&gt;end_date = $request-&gt;input(&#39;end_date&#39;);
$events-&gt;save();
return redirect(&#39;admin_calendar&#39;)-&gt;with(&#39;success&#39;, &#39; has been added&#39;);
}

'

this is my route

&quot;Route::get(&#39;/admin_update_event&#39;, &#39;EventController@show&#39;);
&quot;Route::get(&#39;/admin_editform&#39;, &#39;EventController@edit&#39;);

this is my admin_update_event.blade.php

 @foreach ($events as $event)
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;{{ $event-&gt;id}}&lt;/td&gt;
&lt;td&gt;{{ $event-&gt;title}}&lt;/td&gt;
&lt;td&gt;{{ $event-&gt;color}}&lt;/td&gt;
&lt;td&gt;{{ $event-&gt;start_date}}&lt;/td&gt;
&lt;td&gt;{{ $event-&gt;end_date}}&lt;/td&gt;
&lt;th&gt; &lt;a href=&quot;{{ route(&#39;edit&#39;,[&#39;id&#39; =&gt; $events-&gt;id, &#39;event&#39; =&gt; 1]) }}&quot;&gt;
Edit &lt;/a&gt;
&lt;/th&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
@endforeach

this is my admin_editform.blade.php

            &lt;form method=&quot;POST&quot; action=&quot;{{action(&#39;EventController@update&#39;)}}&quot;&gt;
{{csrf_field() }}
&lt;div class=&quot;container&quot;&gt;
&lt;div class=&quot;jumnbotron&quot;&gt; 
&lt;h1&gt; Update Event &lt;/h1&gt;
&lt;br&gt;
&lt;input type=&quot;hidden&quot; name=&quot;_method&quot; value=&quot;UPDATE&quot; /&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;label for=&quot;&quot;&gt;Enter name of the Event&lt;/label&gt;
&lt;input type=&quot;text&quot; class=&quot;form-control&quot; name=&quot;title&quot; placeholder=&quot;Enter The name&quot; value=&quot;{{ $events-&gt;title}}&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;label for=&quot;&quot;&gt;Choose a Color&lt;/label&gt;
&lt;input type=&quot;color&quot; class=&quot;form-control&quot; name=&quot;color&quot; placeholder=&quot;Choose a color&quot; value=&quot;{{ $events-&gt;color}}&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;label for=&quot;&quot;&gt;Enter startdate of the Event&lt;/label&gt;
&lt;input type=&quot;datetime-local&quot; class=&quot;form-control&quot; name=&quot;start_date&quot; class=&quot;date&quot; placeholder=&quot;Enter The start date&quot; value=&quot;{{ $events-&gt;start_date}}&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;label for=&quot;&quot;&gt;Enter enddate of the Event&lt;/label&gt;
&lt;input type=&quot;datetime-local&quot; class=&quot;form-control&quot; name=&quot;end_date&quot; class=&quot;date&quot; placeholder=&quot;Enter The end date&quot; value=&quot;{{ $events-&gt;end_date}}&quot;&gt;
&lt;/div&gt;
{{ method_field(&#39;PUT&#39;) }}
&lt;input type=&quot;submit&quot; name=&quot;submit&quot; class=&quot;btn btn-primary&quot; value=&quot;add Event data&quot;/&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/form&gt;

答案1

得分: 2

问题出在你的 "edit" 路由没有参数,而 edit() 方法需要一个 $id 参数。尝试这样做:

Route::get('/admin_editform/{id}', 'EventController@edit');
英文:

The issue is your "edit" route having no parameters and the edit() method expects a $id parameter. Try this:

Route::get(&#39;/admin_editform/{id}&#39;, &#39;EventController@edit&#39;);

答案2

得分: 0

请尝试这个:

Route::get('/admin_editform/{id}', 'EventController@edit');
英文:

Please try this

   Route::get(&#39;/admin_editform/{id}&#39;, &#39;EventController@edit&#39;);

huangapple
  • 本文由 发表于 2020年1月3日 22:26:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/59580257.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定