dongwei8440 2019-07-16 19:39 采纳率: 100%
浏览 45
已采纳

数据库中的列为null

I need to know how can I save the id of the post in my table post_tag, column post_id?Now, when I add a new post, in my table is saved only tag_id, can you give me some help with that?I'm watching a tutorial but I'm stuck with this problem.Thank you.

create_post_tag_table

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePostTagTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('post_tag', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('post_id')->unsigned();
            $table->foreign('post_id')->references('id')->on('posts');

            $table->integer('tag_id')->unsigned();
            $table->foreign('tag_id')->references('id')->on('tags');

        });
    }

}

PostsController with function store

 public function store(Request $request )
    {



        $data = request()->validate([

            'caption' => 'required|max:255',
            'image' => 'required|image',
        ]);
        $post = new Post;

        $post->tags()->sync($request->tags, false);



        $imagePath = request('image')->store('uploads', 'public');

        $image = Image::make(public_path("storage/{$imagePath}"))->fit(1600, 1100);
        $image->save();

        auth()->user()->posts()->create([
            'caption' => $data['caption'],
            'image' => $imagePath,
        ]);



         return redirect('/profile/' . auth()->user()->id);
    }

create_tags_table

  public function up()
    {
        Schema::create('tags', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->timestamps();
        });
    }

create_posts_table

 public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('user_id');
            $table->string('caption');
            $table->string('image');
            $table->timestamps();


            $table->index('user_id');
        });
    }

Now, with this lines $post = new Post; and $post->tags()->sync($request->tags, false); my tag id is saved in tag_id column, but my new post id from table posts isn't.How can I save my post id in my post_id table?

  • 写回答

4条回答 默认 最新

  • duangan6731 2019-07-16 20:03
    关注

    You are creating two different Post objects.

    $post = new Post;
    
    $post->tags()->sync($request->tags, false);
    
    auth()->user()->posts()->create([ // this creates another one
        'caption' => $data['caption'],
        'image' => $imagePath,
    ]);
    

    Withouth knowing to much about what you are trying to achieve. I think you can obtain your desired logic by instead of creating a new one, saving the one already existing to the user. (Usually i think $request->user() is a better aproach to getting the user out). You will also need to add, these fields to the first post object.

    $post = new Post([
        'caption' => $data['caption'],
        'image' => $imagePath,
    ]);
    
    //code in between should still be there
    
    $request->user()->posts()->save($post);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比