调用非对象上的成员函数 eloquent attach

Call to a member function on a non-object eloquent attach

我有一个问题,laravel 没有看到我的 tags() 方法用于在新条目上附加新标签。当我尝试 运行 方法并将标签附加到我的 Tile 模型时,我不断收到 Call to a member function on a non-object。所有方法都在返回它们的关系。我遵循与文档所述相同的顺序 eloquent.

控制器

      $tile = \Tiles\Tile::find($tile_id);
      $tile->tags()->attach($tag_array);        

型号

<?php namespace Tiles;

use Illuminate\Database\Eloquent\Model;

class Tile extends Model {

    /**
     * The Tile table
     * @var string
     */
    protected $table = 'tiles';

    /**
     * Pivot table for tags
     * @var string
     */
    protected $pivot = 'tag_tile';


    /**
     *  Get the tags associated with the given tile
     * 
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
   public function tags() {

        return $this->belongsToMany('Tiles\Tag', $this->pivot, 'tile_id', 'tag_id')->withTimestamps();

    }

}

试一试

型号

public function tags() {

        return $this->belongsToMany('Tiles\Tag', $this->pivot, 'tag_id', 'tile_id')->withTimestamps();

    }

感谢您的帮助。我想出了解决办法。我在我的模型中创建了一个方法,并将每个方法推送到一个数组并将其提供给附加方法。现在可以使用了。