使用一对一关系laravel上传图像

时间:2017-10-26 05:49:40

标签: php laravel file-upload one-to-one

我在解决这个问题时遇到了一些困难,我想要的是将图像上传到我的数据库,其中该图像的user_id必须等于personal_information表的id。

这就是我正在制作的:登录时,我是一名管理员,我可以看到所有用户名都是从user_information表中获取的,我可以点击他们的名字查看他们的信息,我也可以上传图片,以便我知道它们的样子。

当我点击人名时,这就是我的视图中的样子: enter image description here

我当前的代码:

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>

UserImage模型:

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');

获取此错误: enter image description here

1 个答案:

答案 0 :(得分:1)

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'