动态搜索功能相册

时间:2020-04-24 03:14:43

标签: laravel searchable

我正在尝试为我的PhotoAlbumn项目创建一个动态的serch函数。所以我已经安装了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');
  }

PhotoController

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之前,这种关系有效。

1 个答案:

答案 0 :(得分:0)

您的恋爱关系中有另外s

替换

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

通过

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