af_159623
af_159623

Reputation: 327

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

I'm trying to save a document on the database in mongo usin a 'jenssegers/laravel-mongodb' model

this is the model

<?php

namespace App;

use Jenssegers\Mongodb\Eloquent\Model;

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

and the controller

<?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();
    
        }
        
    }

I get this error

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

The problem is clear but how I have to add a method to add the attributes on the model

Upvotes: 0

Views: 1480

Answers (1)

Marcin Nabiałek
Marcin Nabiałek

Reputation: 111869

Looking at docs it should be used same as in Eloquent so instead of

$candidate = new Candidate();

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

you can use:

$candidate = new Candidate();

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

Upvotes: 1

Related Questions