One to one relationship while doing CRUD (create part)

▼魔方 西西 提交于 2019-12-13 02:49:40

问题


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

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(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 (how I show the image in view)

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

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(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>

回答1:


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

Change this line

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

For this

$UserImage->user_id = $user_info;

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



来源:https://stackoverflow.com/questions/46902201/one-to-one-relationship-while-doing-crud-create-part

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!