database - laravel-5.4 如何存储storage\app\public 的图片路径

标签 database image laravel file-upload laravel-5.4

我正在使用 laravel-5.4 make:auth。在 register.blade.php 中,为用户添加了一个额外的字段 Profile picture

<form class="form-horizontal" role="form" method="POST" action="{{ route('register') }}" enctype="multipart/form-data">
    {{ csrf_field() }}
    <div class="form-group{{ $errors->has('image') ? ' has-error' : '' }}">
        <label for="image" class="col-md-4 control-label"> Profile picture</label>
        <div class="col-md-6">
            <input id="image" type="file" class="form-control" name="image">

            @if ($errors->has('image'))
                <span class="help-block">
                    <strong>{{ $errors->first('image') }}</strong>
                </span>
            @endif
        </div>
    </div>

我想将图像路径存储在数据库中。我也执行了: php artisan storage:link[public/storage] 目录已链接。

app\Http\Controllers\Auth\RegisterController.php:

public function store(Request $request)
{
    if($request->hasFile('image')) {
        $image_name = $request->file('image')->getClientOriginalName();              
        $image_path = $request->file('image')->store('public'); 
        $image = Image::make(Storage::get($image_path))->resize(320,240)->encode();
        Storage::put($image_path,$image);
        $image_path = explode('/',$image_path);
        $user->image = $image_path;
        $user->save();
    } else{
        return "No file selected";
    }

}

web.php

Route::post('/store', 'RegisterController@store');
Route::get('/show', 'RegisterController@show');

在数据库中,image下的user表中存储为一个临时路径:

C:\xampp\tmp\phpC762.tmp.

如何存储storage\app\public的图片路径。

最佳答案

在您的 Controller 中更改此代码

$user->image = $image_path;

$user->image = Storage::url($image_name);

文档:https://laravel.com/docs/5.4/filesystem#file-urls

关于database - laravel-5.4 如何存储storage\app\public 的图片路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43439759/

相关文章:

javascript - Laravel-Javascript | Laravel即使返回 false 后表单仍会提交

php - 预期响应代码 250 但得到代码 "535",消息“535-5.7.8 用户名和密码不被接受

sql - 在这种情况下我怎样才能陷入僵局?

php - 无法连接服务器上的MySQL

sql - SELECT 查询是否总是以相同的顺序返回行?带聚簇索引的表

android - android中的图像缓存

php - Docker 构建留下中间图像

database - 拉维尔 |模型/数据库约定未按预期工作

c# - ExternalException (0x80004005) GDI+ 中发生一般性错误

php - GuzzleHTTP如何获取请求头