上传具有一对一关系的图像 laravel

Uploading image with one to one relationship laravel

我在做这个问题时遇到了一些困难,我想做的是将图像上传到我的数据库,该图像必须有一个 user_id 等于 personal_information table.

这就是我正在做的:登录时,我是管理员,我可以看到所有用户名都取自 user_information table,我可以点击他们的名字可以看到他们的信息,我也可以上传照片,这样我就知道他们的样子了。

这是我点击人名时在我的视图中的样子:

我目前拥有的密码:

test.blade.php(根据屏幕截图显示)

@foreach ($data as $object)
    <b>Name: </b>{{ $object->Name }}<br><br>
 <a href="{{ url('/user/show/'.$object->id.'/edit') }}">Edit</a><br>

 @foreach($data3 as $currentUser)
<a href="{!! route('user.upload.image', ['user'=>$currentUser->user_id])  !!}">
    <button class="btn btn-primary"><i class ="fa fa-plus"></i>Upload Images</button>
</a>
@endforeach


@endforeach

CreateController(上传图片和上传页面的控制器)

public function create1(personal_info $user){
     return view('create1')->withUser($user);

public function store1(Request $request){

   $this->validate($request, [
        'file' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
    ]);

   if ($request->hasFile('file')) {
        $image = $request->file('file');
        $name = $image->getClientOriginalName();
        $size = $image->getClientSize();
        $id = $request->user_id;
        $destinationPath = public_path('/images');
        $image->move($destinationPath, $name);

        $userImage = new UserImage;
        $personal_info = new personal_info;
        $userImage->name = $name;
        $userImage->size = $size;
        $user= personal_info::find($id);
         $user->UserImages()->save($userImage);
}
       return redirect('/home');
    }
public function test($id){
   $user = personal_info::where('id',$id)->first();
  return view('create1', compact('user'));
}
}

Create1.blade.php(上传页面)

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

                        {{  csrf_field()  }}
<input type="hidden" name="user_id" value="{{$user->id()}}">

<div class="form-group">
    <label for="imageInput" class="control-label col-sm-3">Upload Image</label>
            <div class="col-sm-9">
                <input type="file" name="file">

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

用户图像模型:

class UserImage extends Eloquent
{
   protected $fillable = array('name','size','user_id');
    public function personal_infos() {
        return $this->belongsTo('App\personal_info', 'user_id', 'id');
    }  
}

personal_info 型号:

class personal_info extends Eloquent
{
    protected $fillable = array('Name');
    protected $table = 'personal_infos';
    protected $primaryKey = 'id';

        public function UserImages() {
        return $this->hasOne('App\UserImage','user_id');
    }
}

路线:

Route::get('/userUpload/{id}/create1','CreateController@create1')->name('user.upload.iamge');
Route::post('/userUpload','CreateController@store1');
Route::get('/user/showImage/{id}', 'HomeController@Insert')->name("image");
Route::post('/userUpload/create1', 'CreateController@test');

出现此错误:

Upload按钮仅在$data3不为空时显示。要在它为空时显示它,请使用条件检查它是否为空,如果不是,则执行其他操作。像这样:

@foreach ($data as $object)
    <b>Name: </b>{{ $object->Name }}<br><br>
 <a href="{{ url('/user/show/'.$object->id.'/edit') }}">Edit</a><br>

  @if($data3->count())
 @foreach($data3 as $currentUser)
<a href="{!! route('user.upload.image', ['id'=>$currentUser->user_id])  !!}">
    <button class="btn btn-primary"><i class ="fa fa-plus"></i>Upload Images</button>
</a>
@endforeach
@else
 <a href="{!! route('user.upload.image', ['id'=>$object->id])  !!}">
    <button class="btn btn-primary"><i class ="fa fa-plus"></i>Upload Images</button>
@endif    

@endforeach

已经在控制器中设置了关系。您只需要显示按钮,控制器就会处理其余的事情

编辑

您收到错误消息是因为您的路由名称是 'user.upload.iamge' 而不是 'user.upload.image'