dongyi4420 2019-07-31 06:26
浏览 70

laravel这样做是否正确? 从一个视图表单多次插入到不同的表

I wanna know that it is correctly to something like this. In my database i have table with users and photos. Every one user has exactly one picture. I have one view with form which is route post to UsersController to store method.
UsersController


    public function store(Request $request)
    {
       // some validation

       $user = new Users();
       $user->name = $request->input('name')
       .....
       if($user->save())
       {
         if($request->hasFile('photo'))
         {
              $photo = new Photos();
              ....
              $photo->save();
              return redirect......
         }
       }
    }

It is correctly to insert data from one controller to muliple tables. In this example it's only one additional table but every know there may be more of them.

  • 写回答

1条回答 默认 最新

  • doulan4371 2019-07-31 07:00
    关注

    If you aren't going to use the images for other models, it is better to have an image column directly in the users table to avoid the extra work that seems unnecessary to me.

    Or if you need to have a different table for images, I suggest you should use Eloquent OneToOne polymorphic relationships.

    Schema for photos table:

    photos
        id - integer
        url - string
        photoable_id - integer
        photoable_type - string
    

    And in your Photo model:

    public function photoable()
    {
        return $this->morphTo();
    }
    

    And in your User model:

    public function photo()
    {
        return $this->morphOne('App\Photo', 'photoable');
    }
    

    Now in your controller:

    $user = new Users();
    $user->name = $request->input('name')
    if($user->save())
    {
        if($request->hasFile('photo'))
        {
            // Save the file to disk and get it's URL as $photoUrl
            $user->photo()->create([
                'url' => $photoUrl
            ]);
        }
    }
    

    Now once the photo is saved, you can get the photo for the user using photo property on user object, i.e. $user->photo. The benefit of this architecture is that you can use the same table to store photos for other models too when needed. You will just need to copy the photo() method from User model to your other models where you intend to have the photos.

    Apart from all this, I see that you have used Photos name for the model, but in Laravel, you should always use singular names for models, not the plural ones. So I'd suggest that you should change your model name from Photos to Photo in order to ensure that you flawlessly use the power of Eloquent without any headache.

    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭