Laravel 5.8上传图片

时间:2019-09-20 10:30:13

标签: php laravel image laravel-5 upload

我想在个人资料页面中上传图片。

我为个人资料页面创建了create.blade.php。

填写空白后,我想在index.blade.php中显示每个信息,图像,姓名,性别,国家,身体,描述。

  

现在我可以插入除图像之外的所有数据。

我已经执行了 php artisan storage:link

因此,如果有人帮我,我将感激不尽。

web.php

Route::prefix('user')->group(function(){
    Route::resource('profile', 'UserController');
});

迁移文件

Schema::create('profiles', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('user_id');
            $table->string('image');
            $table->string('name');
            $table->string('gender');
            $table->string('country');
            $table->string('bod');
            $table->string('description');
            $table->timestamps();
        });

create.blade.php

<div class="image-preview" id="imagePreview">
                @if(empty(Auth::user()->profile->image))
                <img src="{{asset('image/image1.jpg')}}" class="image-preview__image">
                @else
                <img src="{{asset('uploads/profile')}}/{{Auth::user()->profile->image}}" class="image-preview__image">
                @endif
        </div>

        <form action="{{ route('profile.store') }}" method="POST" enctype="multipart/form-data">
                @csrf
        <div class="preview">
                <input type="file" id="file" accept="image/*" name="image">
            <label for="file">
                Change Image
            </label>
        </div>
        <ul class="information">
            <li>Name :<br>
                <input type="text" class="name" name="name" required
                value="@if(!empty(Auth::user()->profile->name)){{ Auth::user()->profile->name }}@endif">
            </li><br>
            <li>Gender :<br>
                <div class="gender">
                    <select name="gender" id="" name="gender" >
                        <option class="option" value="@if(empty(Auth::user()->profile->gender))Select Gender
                                @else{{ Auth::user()->profile->gender }}@endif" selected="selected" required>
                                @if(empty(Auth::user()->profile->gender))Select Gender
                                @else{{ Auth::user()->profile->gender }}@endif
                        </option>
                        <option value="male" >male</option> 
                        <option value="female" class="selected">female</option> 
                        <option value="any">any</option> 
                    </select>       
                </div>
            </li>   
            <li>Country :<br>
                <div class="country">
                    <select name="country" id="" name="country" required>
                            <option class="option" value="@if(empty(Auth::user()->profile->country))Select country
                                    @else{{ Auth::user()->profile->country }}@endif" selected="selected">

                                    @if(empty(Auth::user()->profile->country))Select country
                                    @else{{ Auth::user()->profile->country }}@endif
                            </option>
                        <option value="United Kingdom">United Kingdom</option> 
                        <option value="United States">United States</option> 
                    </select>
                </div>    
            </li><br>
            <li>Birthday :<br>
                <input type="text" class="birthday" id="bod" name="bod" 
                value="@if(!empty(Auth::user()->profile->bod)){{ Auth::user()->profile->bod }}@endif">
            </li><br>
            <li>User Description :<br>
                <textarea name="description" id="" cols="60" rows="10">@if(!empty(Auth::user()->profile->description)){{ Auth::user()->profile->description }}@endif</textarea></li>
        </ul>
        <button type="submit" class="saveBtn">Save</button>
    </div>
</form>

UserController.php

public function store(Request $request) {
        $this->validate($request,[
            'image'=>'image|mimes:png, jpg, jpeg|max:20000'
        ]);

        $user_id = auth()->user()->id;
        if($request->hasfile('image')){
            $file = $request->file('image');
            $ext = $file->getClientOriginalExtension();
            $filename = time().'.'.$ext;
            $file->move('uploads/profile/', $filename);
            Profile::where('user_id',$user_id)->update([
                'image'=>$filename
            ]);
        }

        $validatedData = $request->validate([
            'name'=>'required',
            'gender'=>'required',
            'country' => 'required',
            'bod'=>'required|before:today'
        ]);

        $user_id = auth()->user()->id;
        Profile::updateOrCreate(
            ['user_id' => $user_id], // search requirements
            [   
                'name' => request('name'),
                'gender' => request('gender'),
                'country' => request('country'),
                'bod' => request('bod'),
                'description' => request('description')
            ]
        );
        return redirect()->route('profile.index')->with('profile', Profile::all());
    }

1 个答案:

答案 0 :(得分:1)

尝试一下。不需要先分配它,以后再存储

Profile::updateOrCreate(
            ['user_id' => $user_id], // search requirements
            [   
                'name' => $request->name,

                'description' => $request->description,
                'image'=>$filename
            ]
        );

请更改您对birhdate的验证。.更新是您的问题。提供日期格式即可使用

'bod'=>'date_format:Y-m-d|before:today'