【问题标题】:Dynamic search function Photo album动态搜索功能相册
【发布时间】:2020-04-24 03:14:43
【问题描述】:

我正在尝试为我的 PhotoAlbumn 项目创建一个动态搜索功能。所以我安装了 Nicolaslopezj Searchable。但是它还不能正常工作。

错误

Illuminate\Database\Eloquent\RelationNotFoundException 调用模型 [App\Photo] 上的未定义关系 [相册]

型号

use Illuminate\Database\Eloquent\Model;
use Nicolaslopezj\Searchable\SearchableTrait;

class Photo extends Model{
    use SearchableTrait;

    protected $searchable = [
    /**
     * Columns and their priority in search results.
     * Columns with higher values are more important.
     * Columns with equal values have equal importance.
     *
     * @var array
     */
    'columns' => [
        'albums.name' => 10,
        'photos.title' => 10,
        'photos.info' => 10,
        'albums.title' => 5,
      ],

    'joins' => [
        'albums' => ['photos.id','albums.id'],
    ],
];


  protected $fillable = array('photo','title','info','album_id');

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

照片控制器

public function search(Request  $request){
    $query = $request->input('query');
    $result = Photo::search($query)
                    ->with('albums')  
                    ->get();

    return view('search.results')->with('result', $result);

  }

这种关系在使用 Nicolaslopezj Searchable 之前有效。

【问题讨论】:

    标签: laravel searchable


    【解决方案1】:

    你的关系中有一个额外的s

    替换

        $result = Photo::search($query)
                        ->with('albums')  
                        ->get();
    

        $result = Photo::search($query)
                        ->with('album')  
                        ->get();
    

    【讨论】:

    • albums 是我要加入的表的名称。如果我改变它,我会得到一个错误
    • with('albums') 调用不存在的关系albums()。你得到什么错误?
    • 我收到此错误。调用未定义的关系。 Albums() 存在于 AlbumsController 中。在使用 Nicolaslopezj Searchable 之前。这行得通,我在 PhotoController 中加入了相册
    • 您好,请记住调用with() 是调用关系而不是连接。请删除 s 并在您的问题中分享代码 - 不是评论
    猜你喜欢
    • 2016-10-02
    • 1970-01-01
    • 1970-01-01
    • 2016-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多