Laravel morphTo函数返回null,尽管存在关系
我创建了一个多态关系,这很有效。仅当尝试检索“父级”(使用Laravel morphTo函数返回null,尽管存在关系,laravel,eloquent,polymorphism,Laravel,Eloquent,Polymorphism,我创建了一个多态关系,这很有效。仅当尝试检索“父级”(使用和)时,结果是null 假设有一些表/模型需要特殊的缓存关系 迁移 class CreateSearchCachesTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('search_cache
和
)时,结果是null
假设有一些表/模型需要特殊的缓存关系
迁移
class CreateSearchCachesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('search_caches', function (Blueprint $table) {
$table->bigIncrements('id');
$table->text('searchable');
$table->string('url');
$table->morphs('searchCacheable');
$table->timestamps();
});
}
}
型号
class SearchCache extends Model
{
protected $fillable = [
'searchable',
'url'
];
public function searchCacheable()
{
return $this->morphTo();
}
}
使用$model->searchCache()->save($searchCache)保存代码>工作
但在尝试检索关系时,它显示为null:
$results = SearchCache::query();
foreach ($search as $searchString) {
$results = $results->where('searchable', 'like', "%$searchString%");
}
$results = $results->with('searchCacheable')->get();
dd($results);
问题似乎在于在名称中使用CamelCase。所以请注意先检查一下
从searchCacheable
重命名为searchCacheable
修复了它