adMethodCallException:在 Jenssegers\Mongodb\Eloquent\Model 上调用未定义的方法 App\Candidate::name()

adMethodCallException: Call to undefined method App\Candidate::name() on Jenssegers\Mongodb\Eloquent\Model

我正在尝试使用 'jenssegers/laravel-mongodb' 模型

在 mongo 中的数据库中保存文档

这是模型

<?php

namespace App;

use Jenssegers\Mongodb\Eloquent\Model;

class Candidate extends Model
{
    public function User(){
        return $this->belongsTo(User::class);
    }
}

和控制器

<?php

namespace App\Http\Controllers;

    use Illuminate\Http\Request;
    use App\Candidate;
    
    class CandidateController extends Controller
    {
        public function __construct(){
            //$this->middleware('jwt');
        }
    
        public function create(Request $request){
            //var_dump($request->all());
            $candidate = new Candidate();
            $candidate->name($request['name']);
            $candidate->source($request['source']);
            $candidate->save();
    
        }
        
    }

我收到这个错误

BadMethodCallException: Call to undefined method App\Candidate::name() in file /home/myuser/myproject/vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php on line 50

问题很清楚,但是我必须如何添加一个方法来在模型上添加属性

查看 docs 它应该与 Eloquent 中一样使用,而不是

$candidate = new Candidate();

$candidate->name($request['name']);
$candidate->source($request['source']);
$candidate->save();

您可以使用:

$candidate = new Candidate();

$candidate->name = $request->input('name');
$candidate->source = $request->input('source');
$candidate->save();