简体   繁体   English

laravel 迁移更改未反映

[英]laravel migration changes not reflecting

i changed my post_id foreign key to posts_id in images migration in order for the relationship to work.我在图像迁移中将我的 post_id 外键更改为 posts_id 以使关系正常工作。 however after changing, the insertion of images in images table is still picking post_id which is giving me an error column not found.但是更改后,在图像表中插入图像仍在选择 post_id,这给了我一个未找到的错误列。 i tried cache:clear and config but nothing worked.我尝试了缓存:清除和配置,但没有任何效果。 post_id is nowhere in my code post_id 在我的代码中不存在

postcontroller后控制器

 public function store( Request $request )
{    
    $data = request()->validate([
        'user_id' => 'required',
        'about' => 'required',
        'category' => '',
        'expire_date' => '',
        

    ]); 
if (Auth::guard('web')->check())
     {
       $user = Auth::user();
       $post = new Post();

       /*$post = $user->posts()->create([
            'about' => $data['about'],
            'category' => $data['category'],
            'expire_date' => $data['expire_date'],
            
        ]);*/
        if($request->hasFile('image'))
        {
          $files = $request->file('image');
          
          
          
          foreach($files as $file)
          {
            
            $name = time().'-'.$file->getClientOriginalName();
            $name = str_replace('','-',$name);
            echo $name;
            
            $file->move('images',$name);
            
            $post->images()->create(['image' => $name ]);
           

          }
        }
       


        $user = Auth::guard('web')->id() ;

        return redirect()->route('home',['user'=>$user]);

        
    }


  }

postmodel后模型

public function posts()
{
    return $this->belongsTo(User::class);
}

images model图像 model

class images extends Model
{

    protected $fillable = [
    'posts_id',
    'image'
];
 
public function posts(){
    return $this->belongTo(Posts::class);

}
}

posts migration发布迁移

 public function up()
{
    Schema::create('posts', function (Blueprint $table) {
        $table->id();
        $table->uuid('user_id')->nullable;
        $table->uuid('admin_id')->nullable;
        $table->string('category')->nullable;
        $table->string('about');
       
        $table->timestamps();
    });
}

images migration图像迁移

public function up()
{
    Schema::create('images', function (Blueprint $table) {
        $table->id();
        $table->unsignedBigInteger('posts_id');
        $table->string('image');
        $table->timestamps();
        
        $table->index('posts_id');

    });
}

Try to change Images model尝试更改图像 model

public function posts(){
    return $this->belongTo(Posts::class);
}

to

public function posts(){
    return $this->belongTo(Posts::class, 'posts_id');
}

Why would you change it?你为什么要改变它? Laravel can handle this for you if leave it to post_id.如果将其留给 post_id,Laravel 可以为您处理。 Otherwise you'll have to update the relation posts in your images model by adding extra arguments.否则,您必须通过添加额外的 arguments 来更新图像 model 中的关系帖子。 See the documentation: laravel.com/docs/7.x/eloquent-relationships请参阅文档:laravel.com/docs/7.x/eloquent-relationships

public function posts(){
    return $this->belongTo(Posts::class, 'posts_id');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM