关系方法必须在Laravel FormBuilder中返回一个类型的对象

时间:2017-01-02 09:04:42

标签: php laravel

我正在使用laravel 5.1。在编辑功能中,我收到以下错误:

Model.php第2673行中的LogicException: 关系方法必须返回类型为Illuminate \ Database \ Eloquent \ Relations \ Relation

的对象

这是我的控制器功能:

public function edit(FormBuilder $formBuilder, $id) {
    $result  = News::findOrFail($id);
    $form    = $formBuilder->create('employment_bank\Forms\NewsForm', [
        'method' => 'PUT',
        'model' => $result,
        'url' => route($this->route.'update', $id)
    ])->remove('save');
    return view($this->content.'edit', compact('form'));
}

新闻模式:

protected $table = 'news';
public static $rules = [
    'title' => 'required',
];

protected $guarded = ['id', '_token'];
protected $fillable = ['title', 'photo', 'status', 'summary', 'description', 'news_category_id', 'slug'];
public $timestamps = true;

public function news_category(){
    return $this->belongsTo('employment_bank\Models\NewsCategory', 'news_category_id');
}

表格是:

public function buildForm(){

    $category = NewsCategory::lists('title', 'id')->all();
    $status = array(1 => "Publish",0 => "Draft");

    $this->add('title', 'text', [
        'attr' => ['required', 'maxlength' => '255', 'placeholder'=> 'News title'],
        'wrapper' => ['class' => 'form-group'] // Shows the wrapper for each e
    ]);
    $this->add('photo', 'file', [
        'attr'  =>  ['id' => 'photo_url', 'accept'=>'.jpg, .png', 'title'=>'Please upload a passport size photograph'],
        'label' =>  'Photo'
    ]);
    $this->add('news_category_id', 'select', [
        'choices' => $category,
        'empty_value' => '==== Select Category ===',
        'label' => 'Category',
        'attr' => ['required'],
    ]);
    $this->add('status', 'select', [
        'choices' => $status,
        'empty_value' => '==== Status ===',
        'label' => 'status',
        'attr' => ['required'],
    ]);
    $this->add('summary', 'textarea', [
        'attr' => ['maxlength' => '255', 'rows' => '5', 'placeholder'=> 'News summary'],
        'wrapper' => ['class' => 'form-group'] // Shows the wrapper for each e
        //'wrapper' => ['class' => 'form-group col-md-6'] // Shows the wrapper for each e
    ]);
    $this->add('description', 'textarea', [
        'attr' => ['rows' => '10', 'placeholder'=> 'News description'],
        'wrapper' => ['class' => 'form-group'] // Shows the wrapper for each e
        //'wrapper' => ['class' => 'form-group col-md-6'] // Shows the wrapper for each e
    ]);

    $this->add('save', 'submit', [
        'attr' => ['class'=>'btn btn-lg btn-primary col-md-12']
    ]);

    $this->add('update', 'submit', [
        'attr' => ['class'=>'btn btn-lg btn-success col-md-12']
    ]);
}

完整错误:

    LogicException in Model.php line 2673:
Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation
in Model.php line 2673
at Model->getRelationshipFromMethod('save') in Model.php line 2643
at Model->getRelationValue('save') in Model.php line 2585
at Model->getAttribute('save') in Model.php line 3339
at Model->__get('save') in Model.php line 3411
at Model->__isset('save') in helpers.php line 529
at object_get(object(News), 'save') in FormField.php line 191
at FormField->getModelValueAttribute(object(News), 'save') in FormField.php line 113
at FormField->setupValue() in FormField.php line 100
at FormField->__construct('save', 'submit', object(NewsForm), array('attr' => array('class' => 'btn btn-lg btn-primary col-md-12'), 'real_name' => 'save')) in Form.php line 161
at Form->makeField('save', 'submit', array('attr' => array('class' => 'btn btn-lg btn-primary col-md-12'))) in Form.php line 185
at Form->add('save', 'submit', array('attr' => array('class' => 'btn btn-lg btn-primary col-md-12'))) in NewsForm.php line 46
at NewsForm->buildForm() in FormBuilder.php line 53
at FormBuilder->create('employment_bank\Forms\NewsForm', array('method' => 'PUT', 'model' => object(News), 'url' => 'http://localhost:8000/master/news/1')) in NewsController.php line 104
at NewsController->edit(object(FormBuilder), '1')
at call_user_func_array(array(object(NewsController), 'edit'), array(object(FormBuilder), 'news' => '1')) in Controller.php line 256
at Controller->callAction('edit', array(object(FormBuilder), 'news' => '1')) in ControllerDispatcher.php line 164
at ControllerDispatcher->call(object(NewsController), object(Route), 'edit') in ControllerDispatcher.php line 112
at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 114
at ControllerDispatcher->callWithinStack(object(NewsController), object(Route), object(Request), 'edit') in ControllerDispatcher.php line 69
at ControllerDispatcher->dispatch(object(Route), object(Request), 'employment_bank\Http\Controllers\Master\NewsController', 'edit') in Route.php line 203
at Route->runWithCustomDispatcher(object(Request)) in Route.php line 134
at Route->run(object(Request)) in Router.php line 708
at Router->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in AuthenticateAdmin.php line 33
at AuthenticateAdmin->handle(object(Request), object(Closure))
at call_user_func_array(array(object(AuthenticateAdmin), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Router.php line 710
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 675
at Router->dispatchToRoute(object(Request)) in Router.php line 635
at Router->dispatch(object(Request)) in Kernel.php line 236
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in VerifyCsrfToken.php line 50
at VerifyCsrfToken->handle(object(Request), object(Closure))
at call_user_func_array(array(object(VerifyCsrfToken), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in StartSession.php line 62
at StartSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure))
at call_user_func_array(array(object(EncryptCookies), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Kernel.php line 122
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 87
at Kernel->handle(object(Request)) in index.php line 54
at require_once('/var/www/jobportal/public/index.php') in server.php line 21

我做错了什么?

2 个答案:

答案 0 :(得分:1)

在NewsForm中,两个按钮(保存更新)被视为关键字。

$this->add('save', 'submit', [
    'attr' => ['class'=>'btn btn-lg btn-primary col-md-12']
]);

$this->add('update', 'submit', [
    'attr' => ['class'=>'btn btn-lg btn-success col-md-12']
]);

将按钮更改为保存保存新闻更新更改为更新新闻为我工作。

答案 1 :(得分:0)

在您的应用中,您似乎正在尝试访问模型实例上不存在的方法,这就是它搜索关系的原因。

您可以发布错误的完整堆栈跟踪吗?