进行CRUD时一对一关系(创建零件) [英] One to one relationship while doing CRUD (create part)

查看:93
本文介绍了进行CRUD时一对一关系(创建零件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在处理与user_info表和userImage表的一对一关系时遇到了一些麻烦.当我尝试上传图片时,它没有保存到数据库中,并且user_id为0.过去我成功地完成了一对多和一对一的关系,但没有一起使用CRUD.谁能帮我?最好给我一些例子,供我参考或提出建议.预先感谢

I am having some trouble doing a one to one relationship with user_info table and userImage table. When I try to upload my image, it didn't save into my database and it user_id is 0. I managed to successfully do a one to many and one to one relationship in the past but not with CRUD together. Can anyone help me? Best to give me some example for me to refer or advice on what should I do. Thanks in advance

这是我当前的代码: createController:

Here are my current codes: createController:

public function create1(){

    return view('create1');
}

public function store1(Request $request){
     $this->validate($request, [
        'input_img' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
    ]);

 $user_info = Session::get('data');
      $UserImage = new UserImage($request->input()) ;


if($request->hasFile('input_img')) {

$file = $request->file('input_img');

$fileName = $file->getClientOriginalName();
        $destinationPath = public_path().'/images' ;
        $file->move($destinationPath,$fileName);
        $UserImage->userImage = $fileName ;
        $UserImage = UserImage::create(['file' => $request->file('input_img')]);
        $UserImage->user_infos()->associate($user_info);
    }

    $UserImage->save() ;

    return redirect('/home');
}

HomeController(这是我打印信息的地方)

HomeController(this is where I print out my information)

public function getInfo($id) {

  $data = personal_info::where('id',$id)->get();
      $data3=UserImage::where('user_id',$id)->get();
 return view('test',compact('data','data3'));

blade.php(我如何在视图中显示图像)

blade.php (how I show the image in view)

 @foreach ($data3 as $object9)
 <img width="100" height="100" src="{!! $object9->userImage!!}">
    @endforeach

UserImage模型(在表中,我使用二进制格式存储在DB中)

UserImage model(in table I used binary format to store in DB)

    class UserImage extends Eloquent
    {
            protected $fillable = array('userImage','user_id');
        public function user_infos() {
            return $this->belongsTo('App\user_info', 'user_id', 'id');
        }

class user_info extends Eloquent
{
    protected $fillable = array('Email', 'Name');
    protected $table = user_infos';
    protected $primaryKey = 'id';
        public function UserImages() {
        return $this->hasOne('App\UserImage','user_id');
    }
}

create1.blade.php(这就是我上传图片的方式)

create1.blade.php(this is how I upload the image)

     <form class="form-horizontal" method="post" action="{{ url('/userUpload')}}" enctype="multipart/form-data">

                        {{  csrf_field()  }}


<div class="form-group">
    <label for="imageInput" class="control-label col-sm-3">Upload Image</label>
            <div class="col-sm-9">
            <input data-preview="#preview" name="input_img" type="file" id="imageInput">
            <img class="col-sm-6" id="preview"  src="" ></img>
        </div>
    </div>

 <div class="form-group">
            <div class="col-md-6-offset-2">
              <input type="submit" class="btn btn-primary" value="Save">
            </div>
          </div>
          </form>

推荐答案

您需要签出 Laravel关系来清理代码并简化许多步骤.

You need to check out Laravel relations to clean up that code and simplify a lot of steps.

更改此行

$UserImage->user_infos()->associate($user_info);

为此

$UserImage->user_id = $user_info;

请记住,您正在该方法中覆盖$UserImage的值.

Take in mind that you're overriding the value of $UserImage inside that method.

这篇关于进行CRUD时一对一关系(创建零件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆