- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我已经在 Stack 上搜索了 Laravel 文档和类似问题,但似乎找不到任何有用的东西。
我设置了一个名为 post_tag 的数据透视表,它旨在分别从帖子表和标签表中收集 post_id 和 tag_id - 但它们没有。数据完美地发布到post表和tag表,但post_tag表仍然是空的。这是我的代码,我将永远感激任何能够提供答案,或者至少为我指明正确方向的人,因为我已经尝试了迄今为止我能找到的一切。
只是为了确认,如果每个文件与多对多关系无关,我已经从每个文件中省略了代码块:
Post.php
class Post extends Model
{
public function tags()
{
return $this->belongsToMany(Tag::class);
}
}
标签.php
class Tag extends Model
{
public function posts()
{
return $this->belongsToMany(Post::class);
}
}
后 Controller .php
public function store(Request $request)
{
$this->validate(request(), [
'content' => 'required',
'tag' => 'required'
]);
Post::create([
'content' => request('content'),
'user_id' => auth()->id()
]);
Tag::create([
'tag' => request('tag'),
]);
return redirect('/');
}
create_post_tag_table.php
class CreatePostTagTable extends Migration
{
public function up()
{
Schema::create('post_tag', function (Blueprint $table) {
$table->integer('post_id');
$table->integer('tag_id');
$table->primary(['post_id', 'tag_id']);
});
}
我还尝试过在数据库中显式设置外键。当我在 Sequel Pro 中检查它们时,它们确实显示为外键,但每当我发布新帖子时,我的 post_tag 表仍然显示为空。
我也尝试过使用 tinker 在 post_tag 表中手动附加 ID,这很有效——我从 Laracasts 上的 Laravel 5.4 基础视频系列中发现了这一点,但没有提到如何将其放入我的代码中,所以每当我发布新帖子时,它都会将 post_id 和 tag_id 动态添加到数据透视表。
感谢并期待您的帮助!
最佳答案
您只需要附上您的模型。看起来像:
$post = Post::create([
'content' => request('content'),
'user_id' => auth()->id()
]);
$tag = Tag::create([
'tag' => request('tag'),
]);
$post->tags()->attach($tag->id);
阅读更多 https://laravel.com/docs/5.5/eloquent-relationships#many-to-many
关于php - Laravel 5.4 及以上版本 : Nothing posting to pivot table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46624636/
我有一个 todolist 应用程序,它在 Pivot 控件内的单独 PivotItems 中具有多个 ListBox 控件。如果我导航到另一个页面,然后使用后退按钮返回,则显示的 Pivot 没有响
我想 pivot_long() 下面数据集的多列避免硬编码。我看过一些类似的问题,但我仍然做不到。 宽数据: > head(data) ID IND_TEST_SCORE ARG_G1_A
假设我有一个 orders 表,它将与另外三个名为 typings、translates 和 论文 。我知道数据透视表应该有点像 many to many polymorphic relation但这
当我尝试将 null 替换为 zero 时,收到以下错误消息。 The column name "jan" specified in the PIVOT operator conflicts with
有没有办法在数据透视表中为计算为零的单元格隐藏或显示空白单元格? 最佳答案 使用数字格式隐藏所选单元格中的零值: 按照此过程隐藏所选单元格中的零值。如果其中一个单元格中的值更改为非零值,则该值的格式将
我正在尝试理解 Select algorithm我遇到了 a good pivot VS a bad pivot 。我可以看到该算法正在使用 Partition 算法来分隔右侧的较大元素pivot 和
我有以下代码:
我有一个国家表和一个数据透视表 Country_language,其中列出了所有国家及其可用语言的翻译。 表结构如下: Languages -------------- ID Locale Recor
目前,PWS 上唯一的 RabbitMQ 服务看起来有点不确定。我想知道我是否可以使用 Pivotals 解决方案 https://network.pivotal.io/products/p-redi
我是使用 Spark 数据帧的新手。我正在尝试将 pivot 方法与 Spark(Spark 版本 2.x)一起使用并遇到以下错误: Py4JError: An error occurred whil
我需要转置一个表,其中 column1 是实体的名称,column2 到 column366 是一年中包含美元金额的日期。表,select语句,输出结果都给了 以下 - 问题 - 此语法要求我创建一个
我想知道是否可以像在 python 和 R 上那样在 OpenRefine 上创建值的聚合和汇总?示例: 包含 30 万条记录的医疗预约表身份识别患者 |年龄 |身份证预约 |值 患者汇总和总结的结果
我想知道是否可以像在 python 和 R 上那样在 OpenRefine 上创建值的聚合和汇总?示例: 包含 30 万条记录的医疗预约表身份识别患者 |年龄 |身份证预约 |值 患者汇总和总结的结果
我不熟悉 SQL 并使用 Google BigQuery。我有一个表,其中有一条记录如下所示: publication_number |受让人 US-6044964-A|索尼公司 |数字音频光盘公司
在尝试转换 sql 表时,我看到了这篇文章 Here .通过使用这种方法,我创建了一个查询。但是我现在已经意识到它当然会使用 MAX 函数聚合结果。但是,我需要 Colum 旋转,但要显示所有事件。从
我们能否将行旋转到多列,即 Create table #Temp_Trans ( P_ID int, Custom_Name varchar(30), Text_Value var
计算字段很棒,但有一些限制,例如无法对其进行排序(无法将字段移动到报告过滤器区域)。 我试过“强制”一个过滤器,就像微软的人建议的那样:https://answers.microsoft.com/en
我有以下数据集,我需要从中计算数据透视中不同值的计数。我尝试了几个函数,如 FREQUENCY、COUNTIFS 等,但我做不到。 输入 Input Data 输出 Expected Output 最
请看下面的数据: 我正在寻找生成以下输出的查询: 我正在试验“PIVOT”,但尚未取得预期的结果。 最佳答案 这应该有效: SELECT ReviewType, DER, LEI, NOR, [NOT
我有两个表Person(person_id, name) 和另一个表Contacts(person_id, phone_type, phone_no)。 Person-----------------
我是一名优秀的程序员,十分优秀!