【问题标题】:How do I add data to other tables when creating a post?创建帖子时如何将数据添加到其他表?
【发布时间】:2021-07-11 15:22:53
【问题描述】:

有一个表post,在hasMany的帮助下附加了4个表,其中几乎相同的字段,即表(store,address,number,buy)。所有数据必须在编写时填写邮政 PostContorller@store

function storee(Request $request)
{
$this->validate($request,[
'name' => 'required'
]);
$post = Post = Post::query()->create([
'name' => $request->get('name'),
'decr' => $request->get('decr'),
]);
}

模特发帖

public function getStoreForPost() 
{
 return $this->hasMany(Store::class);
}

查看创建帖子

@extends('index')
 
@section('content')
<form method="post" action="{{ route('post.store') }}">
    @csrf
    <div align="center">
        <div style="padding-bottom: 25px;">
            <label for="name">Post name</label>
            <input type="text" name="name" id="name">
        </div>
        <div style="padding-bottom: 25px;">
            <label for="description">Post Description</label>
            <input type="text" name="decr" id="description">
        </div>
    </div>
 
    <div class="store" align="center" style="padding-bottom: 10px;">
        <button class="store_add" type="button">Add Store</button>
        <br>
        <br>
        <div>
            <label for="getStoreForPost">Store</label>
                <input type="text" id="store_name" name="store_name[]">
                <input type="text" id="store_link" name="store_link[]">
        </div>
    </div>
    <div align="center">
    <button type="submit">Add Post</button>
    </div>
</form>
@endsection
 
@push('script')
$(document).ready(function () {
        $(".store_add").on('click',function(){
            $(".store").append(
              `<div style="padding: 10px 0">
                <label for="getStoreForPost">Store</label>
                    <input type="text" id="store_name" name="store_name[]">
                    <input type="text" id="store_link" name="store_link[]">
              </div>`
            );
        });
        
    });
@endpush

当我尝试添加帖子时,我收到错误 1364,无法填写没有空属性的字段。这可以理解,我没有指定要填写什么。所以我决定在控制器中添加新的技巧。

$post->getStoreForHouse()->create([
'name' => $request->get('store_name'),
'url' => $request->get('store_url')
]);

迁移 post_migration

$table->string('name');
$table->text('decr')->nullable();

store_migration

$table->string('store_name');
$table->text('store_link');
$table->unsignedBigInteger('post_id')->nullable();
$table->foregin('post_id)->references('id')->on('posts');

模型商店

use App\Models\Post;

use HasFactory;

protected $table = 'store';

protected $fillable = ['store_name', 'store_url'];
protected $hidden = ['created_at', 'updated_at'];

public function post()
{
return $this->belongsTo(Post::class);
}

但没有任何改变,无论我如何尝试爬出 oishbka 1364 或请求中的错误 ::create-没有此操作的指令 升级版: 如果我通过 pma 填写字段,那么所有数据都会输出并正常工作,即我手动填写测试的帖子表(帖子名称,帖子描述),然后转到存储表,然后在那里,我添加了商店名称和商店 url,并将其附加到 post 表中的特定条目。事实证明,当我进入查看这篇文章的页面时,我看到了我在 pma 中附加到它的所有行。

请帮帮我,我已经把这个填满了。

【问题讨论】:

  • 如果您有 4 个具有相同标题的表,您可能应该将它们组合成一个表,其中有一列指示记录类型。
  • 你能显示确切的错误信息吗?它通常会准确地告诉您填写时缺少什么?
  • @GrumpyCrouton,它们几乎与我拥有的字段相同,而不是全部。我需要这些表格来收集数据。但是目前我只对创建帖子时如何以相同的形式将数据添加到其他表中感兴趣。
  • @mrhn, BadMethodCallException Method Illuminate\Database\Eloquent\Collection::create does not exist.
  • 您的代码不应抛出该异常,请更新包含的代码

标签: php mysql laravel forms eloquent


【解决方案1】:

好的,所以这些关系中的每一个都需要单独查看,因为正如您在回复中提到的那样 - 它们必须是不同的表。

$post = Post::query()->create([
'name' => $request->get('name'),
'decr' => $request->get('decr'),
]);

这应该可以正常工作,如果您需要其他值可以为空或默认为某个值,则需要通过数据库上的迁移来设置它。

进一步,

BadMethodCallException Method Illuminate\Database\Eloquent\Collection::create does not exist.

表示您已经选择了一个集合(可能通过关系)然后调用了该函数,因此类似于:

$post->addresses->create(['whatever'=>'data']);

如果您编辑您的消息并使用您的模型代码添加信息,以及可能的迁移(或至少在 phpMyAdmin 或其他内容中的图片),那么我们将能够为您提供帮助,但在那之前,它是一个水晶球的预言。

【讨论】:

    猜你喜欢
    • 2012-03-07
    • 1970-01-01
    • 1970-01-01
    • 2020-06-06
    • 1970-01-01
    • 1970-01-01
    • 2014-08-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多