0
votes

I'm working on laravel's project flyer and I'm continually getting this error from artisan tinker.

 $flyer->photos()->create(['photo' => 'foo.jpg']);

BadMethodCallException with message 'Call to undefined method Illuminate \Database\Query\Builder::photos()'

Here is my Flyer.php file:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Flyer extends Model
{
    public function photos()
    {
        return $this->hasMany('App\Photo');
    }
}

and here is my Photo.php file:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Photo extends Model
{
    protected $table = 'flyer_photos';

    protected $fillable = ['photo'];

    public function flyer()
    {
        return $this->belongsTo('App\Flyer');
    }
}

looks like the method photos() doesn't get recognised or something

1
So what is $flyer ? - Machavity
Kindly post the line that creates $flyer - Ibrahim Lawal
$flyer = factory('App\Flyer')->create(); - kristi tanellari

1 Answers

0
votes

From the error description, $flyer is not a Flyer object, it's an Illuminate \Database\Query\Builder object. That's your error. When creating $flyer, be sure to use a get() or first() after building your query.