我正在阅读laravel 5.2 docs以在我的Laravel应用程序中实现多对多的多态关系.我有许多车型,如Blog,Question,Photo等我想有标签系统为所有的人.我用以下模式创建了Tag表
Schema::create('tags', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('slug')->unique();
$table->timestamps();
});
Run Code Online (Sandbox Code Playgroud)
下面是数据透视表模式.数据透视表名称是entity_tags
Schema::create('entity_tags', function (Blueprint $table) {
$table->increments('id');
$table->integer('tag_id')->unsigned();;
$table->integer('taggable_id')->unsigned();
$table->string('taggable_type');
$table->timestamps();
$table->index('tag_id');
$table->index('taggable_id');
$table->index('taggable_type');
});
Run Code Online (Sandbox Code Playgroud)
这是Tag模型Question模型中定义的关系
public function questions()
{
return $this->belongsToMany('App\Question', 'entity_tags', 'tag_id', 'taggable_id');
}
Run Code Online (Sandbox Code Playgroud)
并且在QuestionModel中定义了以下关系
public function tags()
{
return $this->belongsToMany('App\Tag', 'entity_tags', 'taggable_id', 'tag_id');
}
Run Code Online (Sandbox Code Playgroud)
现在我想定义Laravel 5.2中定义的Many to Many Polymorphic关系.
我的问题是
able作为多态关系一部分的单词后缀列名吗?好吧,我使用的是Laravel 5.4,并且达到了需要更新一对多(hasMany)关系的情况。我有一张products桌子和一张product_images桌子。以下是我在每个模型中的关系定义。
//Product.php
public function productImages()
{
return $this->hasMany('App\ProductImage');
}
//ProductImage.php
public function product()
{
return $this->belongsTo('App\Product');
}
Run Code Online (Sandbox Code Playgroud)
我正在提供一个表单界面来更新现有产品。在那我有4个可选的文件上传输入html标签,用于更新或添加产品的(新)图像。请记住,所有这4张图片都是可选的。
所以我的问题是什么是最好的方法(laravel方法)
product_images表我知道attachor detach方法(sync)删除所有内容,然后添加提供的内容。但是,如果我们要替换任何现有图像,我想保留ID。
在我的Laravel应用程序中,我使用Fetch API滚动加载数据。下面是我JS在刀片文件中的代码
let route = '{{ route("load") }}' + '?q=' + encodeURI('{{ $para }}');
fetch(route)
.then(response => {
if(!response.ok) {
throw new Error('Network response was not ok');
}
return response.text();
})
.then(data => {
console.log(data);
})
.catch((error) => {
console.error('Error:', error);
})
Run Code Online (Sandbox Code Playgroud)
在我的控制器中
public function loadData( Request $request )
{
if( $request->ajax() ) {
return $request->query('q');
}
return 'Not Ajax!';
}
Run Code Online (Sandbox Code Playgroud)
它总是返回“不是 Ajax!”;
我不确定为什么会这样。这不是一个问题,与其他图书馆喜欢jQuery或Axios