首页 > 解决方案 > Laravel 5.8 上传图片

问题描述

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

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

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

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

我已经执行了php artisan storage:link

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

网页.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();
        });

创建.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>

用户控制器.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());
    }

标签: phplaravelimagelaravel-5upload

解决方案


试试这个。不需要先分配这个,以后再存起来

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'

推荐阅读