缺少ArticleController :: update()的参数1

时间:2019-07-09 09:21:00

标签: php laravel

我想编辑并保存网站中的某些字段,但这将我重定向到错误。您能告诉我我的观点出了什么问题吗?这是我的视图,路线和控制器,也许您可​​以看一下。这不是我的代码,但是我试图弄清楚如何使其工作。 Missing argument 1 for App\Http\Controllers\ArticleController::update()

我的观点:

{!! Form::open(['class' => '','url' => 'admin/article/update','id' => 'sky-form3']) !!}

我的路线:

Route::post('admin/article/update/{type?}/{id?}', 'ArticleController@update');

更新功能:

public function update($type, $id)
    {
        if($type == "News")
        {
            $article = \App\News::find($id);
            $article->subject = Request::input('subject');
            $article->public = Request::input('public');
            $article->category_id = Request::input('category_id');
            $article->information = Request::input('information');
            $article->save();
        }

        if($type == "Opinion")
        {
            $article = \App\Opinion::find($id);
            $article->subject = Request::input('subject');
            $article->public = Request::input('public');
            $article->category_id = Request::input('category_id');
            $article->opinion = Request::input('opinion');
            $article->save();
        }

        if($type == "Event")
        {

            $article = \App\Event::find($id);
            $article->subject = Request::input('subject');
            $article->public = Request::input('public');
            $article->category_id = Request::input('category_id');
            $article->event_type_id = Request::input('event_type_id');
            $article->country = Request::input('country');
            $article->save();


        }

        if($type == "CareerSolution")
        {
            $article = \App\CareerSolution::find($id);
            $article->subject = Request::input('subject');
            $article->public = Request::input('public');
            $article->topic_category_id = Request::input('topic_category_id');
            $article->topic_subcategory_id = Request::input('topic_subcategory_id');
            $article->location = Request::input('location');
            $article->on_offer = Request::input('on_offer');
            $article->city = Request::input('city');
            $article->employment_type = Request::input('employment_type');
            $article->estimated_salary = Request::input('estimated_salary');
            $article->quantity = Request::input('quantity');
            $article->expires_at = Request::input('expires_at');
            $article->optional = Request::input('optional');
            $article->save();
        }

        if($article != ""){
            flash(trans($type.' has been Updated'), 'success');
        }else{
            flash(trans($type.' has not been updated'), 'warning');
        }

        return redirect(URL::previous());
    }
}

我的按钮 <a article_type="{{ $article['type'] }}" pop_up_id="{{ $article['type'] }}_{{ $article['id'] }}" role="button" class="btn btn-xs btn-warning modify">

Article.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
    protected $table = 'articles';

    public $timestamps = false;

    protected $fillable = [
        'jobtitle', 'category_id','company','expired','formattedLocationFull','url','snippet','jobkey','approved','date','timestamp','modified_date','views','city','state','country','sponsored','indeedApply','jt','salary'
    ];

    // public function career_solution()
    // {
    //     return $this->hasMany('App\CareerSolution','topic_subcategory_id','id');
    // }
}

4 个答案:

答案 0 :(得分:1)

我认为问题在于您的路线。 {type?}未定义,请尝试将其从路由中删除,这应该没问题。

Route::post('admin/article/update/{id?}', 'ArticleController@update');

答案 1 :(得分:1)

您在路线上定义了两个可选参数,因此您需要在update方法中将它们定义为可选参数

public function update($type = false, $id = false)

您还需要确保以打开的形式传递它们,最好将路由名与此一起使用

Form::open(array('route' => array('route.name', ['id' => $id, 'type' => $type])))

但是,恕我直言,最好避免在定义路线时使用多个可选参数。

答案 2 :(得分:1)

尝试像这样更改路线

Route::post('admin/article/update/{type?}/id/{id?}', 'ArticleController@update')->name('update.article');

,您可以简单地将其用作

{{ route('update.article', ['type' => 'some_type', 'id' => 'some_id'] }}

然后

    Form::open(array('route' => array('update.article', ['type' => 'some_type', 'id' => 'some_id']), 'method' => 'POST'))
    @csrf

答案 3 :(得分:0)

根据您的路线,函数标头应为:public function update($ type = null,$ id = null)

单点改进(不能直接解决您的问题,但可能会有所帮助)。使用模型绑定!参见https://laravel.com/docs/5.8/routing#route-model-binding