php - 图片没有显示并且无法在 laravel 中更新我的图片

标签 php mysql image laravel updates

我正在做一个项目。在这个项目中,我想更新名称、网站和图像字段。我希望如果用户选择新字段,则该字段会更新,否则它会保留过去的值。这对我的名字和 website_link 来说完全没问题。但我不能做这样的图像字段。请帮帮我。 我的 Controller 是

public function edit($id)
    {
        if (Auth::check()) {
            if (Auth::user()->user_role->role_id == 1) {
                $sponsor = Sponsor::where('id', $id)->first();
                if (!empty($sponsor)) {

                    $data = array(

                        'menu' => 'sponsor',
                        'sub_menu' => 'all',
                        'sponsor' => $sponsor
                    );
                    return view('backends.sponsors.edit', $data);
                } else {
                    Session::flash('error', 'Try again.');
                    return redirect();
                }
            } else {
                return redirect('');
            }
        } else {
            return redirect('');
        }
    }

public function update(Request $request, $id)
    {
        if (Auth::check()) {
            if (Auth::user()->user_role->role_id == 1) {
                $sponsor = Sponsor::where('id', $id)->first();
                if (!empty($sponsor)) {
                    $rules = array(
                        'name' => '',
                        'website_link' => '',
                        'logo' => ''
                    );
                    $valid = Validator::make($request->input(), $rules);
                    if ($valid->fails()) {
                        return redirect('sponsors/edit/' . $sponsor->id)->withErrors($valid)->withInput();
                    } else {
                        $sponsor->name = $request->input('name');
                        $sponsor->website_link = $request->input('website_link');
//                        $sponsor->logo = $request->input('logo');
                        $photo = $request->file('logo');
                        if($photo)
                        {
                            $ext = $photo->getClientOriginalExtension();
                            $fileName = rand(100, 5000000) . '.' .$ext;
                            $sponsor->logo = 'public/assets/uploads/sponsors/'.$fileName;
                            $photo->move(base_path().'/public/assets/uploads/sponsors/',$fileName);
                        } else {

                        }


                        if ($sponsor->save()) {
                            Session::flash('success', 'Area of experience updated successful.');
                            return redirect('sponsors/all');
                        } else {
                            Session::flash('error', 'Try again.');
                            return redirect('sponsors/edit//' . $sponsor->id);
                        }
                    }
                } else {
                    Session::flash('error', 'Try again.');
                    return redirect('sponsors/all');
                }
            } else {
                return redirect('');
            }
        } else {
            return redirect('');
        }
    }

我的查看页面是

@extends ('backends.layouts.app')
@section('main')
    <main id="main-container">
        <div class="content bg-gray-lighter">
            <div class="row items-push">
                <div class="col-sm-7">
                    <h1 class="page-heading">
                        Sponsors <small>That feeling of delight when you start your awesome new project!</small>
                    </h1>
                </div>
                <div class="col-sm-5 text-right hidden-xs">
                    <ol class="breadcrumb push-10-t">
                        <li><a class="link-effect" href="{{ URL::to('admin/dashboard') }}">Home</a></li>
                        <li>Sponsor</li>
                    </ol>
                </div>
            </div>
        </div>

        <div class="content">
            <div class="block">
                <div class="block-header">
                    <h3 class="block-title">Sponsor</h3>
                </div>
                <div class="col-md-12">
                    @if(Session::has('success'))
                        <div class="alert alert-success">
                            <strong> {{ Session::get('success') }}</strong>
                        </div>
                    @endif
                    @if(Session::has('error'))
                        <div class="alert alert-danger">
                            <strong> {{ Session::get('error') }}</strong>
                        </div>
                    @endif
                </div>
                <div class="clearfix"></div>
                <div class="block-content">
                    <form method="POST" action="{{ (!empty($sponsor->id)) ? URL::to('sponsors/edit/'.$sponsor->id) : '' }}" class="push-10-t" enctype="multipart/form-data">
                        {{ csrf_field() }}
                        <div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
                            <div class="form-material floating">
                                <input type="text" name="name" value="{{ (!empty($sponsor->name)) ? $sponsor->name : old('name') }}" id="name" class="form-control" required  >
                                <label for="name">Company Name</label>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="form-material floating">
                                <input type="text" name="website_link" value="{{ (!empty($sponsor->website_link)) ? $sponsor->website_link : old('name') }}" id="website_link" class="form-control"  >
                                <label for="website_link">Website Link</label>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="logo">Logo</label><br/>
                            <div id="prev" style="display: none" class="col-md-3 thumbnail">
                                <img id="blah" class="img-responsive">
                            </div>
                            <div class="clearfix"></div>
                            <div class="form-group">
                                <label for="logo"> <span class="btn btn-primary" value="{{ (!empty($sponsor->logo)) ? $sponsor->logo : old('logo') }}" id="fileName0">Browse</span></label>
                                <input type="file"  name="logo" style="visibility: hidden; position: absolute;" value="{{ (!empty($sponsor->logo)) ? $sponsor->logo : old('logo') }}" id="logo"  class="form-control" required>
                            </div>
                        </div>

                        <div class="form-group">
                            <button type="submit" class="btn btn-primary">Update</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </main>
    <script>
        function readURL(input) {
            if (input.files && input.files[0]) {
                var reader = new FileReader();

                reader.onload = function (e) {
                    $('#blah').attr('src', e.target.result);
                }

                reader.readAsDataURL(input.files[0]);
            }
        }

        $("#logo").change(function(){
            $('#prev').show();
            readURL(this);
        });
    </script>
@endsection

请帮我解决这个问题

最佳答案

答案是

public function update(Request $request, $id)
    {
        if (Auth::check()) {
            if (Auth::user()->user_role->role_id == 1) {
                $sponsor = Sponsor::where('id', $id)->first();
                if (!empty($sponsor)) {
                    $rules = array(
                        'name' => 'required',
                        'website_link' => 'required',
                        'logo' => ''
                    );
                    $valid = Validator::make($request->input(), $rules);
                    if ($valid->fails()) {
                        return redirect('sponsors/edit/' . $sponsor->id)->withErrors($valid)->withInput();
                    } else {
                        $sponsor->name = $request->input('name');
                        $sponsor->website_link = $request->input('website_link');
                        $hdLogo = $request->input('hdLogo');
                        $photo = $request->file('logo');
                        if(!empty($photo) && !empty($hdLogo)){
                            $ext = $photo->getClientOriginalExtension();
                            $fileName = rand(100, 5000000) . '.' .$ext;
                            $sponsor->logo = 'public/assets/uploads/sponsors/'.$fileName;
                            $photo->move(base_path().'/public/assets/uploads/sponsors/',$fileName);
                        }else if(!empty($photo) && empty($hdLogo)){
                            $ext = $photo->getClientOriginalExtension();
                            $fileName = rand(100, 5000000) . '.' .$ext;
                            $sponsor->logo = 'public/assets/uploads/sponsors/'.$fileName;
                            $photo->move(base_path().'/public/assets/uploads/sponsors/',$fileName);
                        }else if(empty($photo) && !empty($hdLogo)){
                            $sponsor->logo = $sponsor->logo;
                        }
                        else if(empty($photo) && empty($hdLogo)){
                            Session::flash('error','Logo is required.');
                            return redirect()->back();
                        }

                        if ($sponsor->save()) {
                            Session::flash('success', 'Sponsor updated successful.');
                            return redirect('sponsors/all');
                        } else {
                            Session::flash('error', 'Try again.');
                            return redirect()->back();
                        }
                    }
                } else {
                    Session::flash('error', 'Try again.');
                    return redirect('sponsors/all');
                }
            } else {
                return redirect('');
            }
        } else {
            return redirect('');
        }
    }

View 文件将是

@extends ('backends.layouts.app')
@section('main')
    <main id="main-container">
        <div class="content bg-gray-lighter">
            <div class="row items-push">
                <div class="col-sm-7">
                    <h1 class="page-heading">
                        Sponsors <small>That feeling of delight when you start your awesome new project!</small>
                    </h1>
                </div>
                <div class="col-sm-5 text-right hidden-xs">
                    <ol class="breadcrumb push-10-t">
                        <li><a class="link-effect" href="{{ URL::to('admin/dashboard') }}">Home</a></li>
                        <li>Sponsor</li>
                    </ol>
                </div>
            </div>
        </div>

        <div class="content">
            <div class="block">
                <div class="block-header">
                    <h3 class="block-title">Sponsor</h3>
                </div>
                <div class="col-md-12">
                    @if(Session::has('success'))
                        <div class="alert alert-success">
                            <strong> {{ Session::get('success') }}</strong>
                        </div>
                    @endif
                    @if(Session::has('error'))
                        <div class="alert alert-danger">
                            <strong> {{ Session::get('error') }}</strong>
                        </div>
                    @endif
                </div>
                <div class="clearfix"></div>
                <div class="block-content">
                    <form method="POST" action="{{ (!empty($sponsor->id)) ? URL::to('sponsors/edit/'.$sponsor->id) : '' }}" class="push-10-t" enctype="multipart/form-data">
                        {{ csrf_field() }}
                        <div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
                            <div class="form-material floating">
                                <input type="text" name="name" value="{{ (!empty($sponsor->name)) ? $sponsor->name : old('name') }}" id="name" class="form-control" required  >
                                <label for="name">Company Name</label>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="form-material floating">
                                <input type="text" name="website_link" value="{{ (!empty($sponsor->website_link)) ? $sponsor->website_link : old('name') }}" id="website_link" class="form-control"  >
                                <label for="website_link">Website Link</label>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="logo">Logo</label><br/>
                            <input type="hidden" value="{{ (!empty($sponsor->logo)) ? $sponsor->logo : ''}}" name="hdLogo">
                            @if(!empty($sponsor->logo))
                            <div id="prev" class="col-md-3 thumbnail">
                                <img id="blah" src="{{ url($sponsor->logo)}}" class="img-responsive">
                            </div>
                            @else
                                <div id="prev" style="display: none" class="col-md-3 thumbnail">
                                    <img id="blah" src="{{ (!empty($sponsor->logo)) ? $sponsor->logo : '' }}" class="img-responsive">
                                </div>
                            @endif
                            <div class="clearfix"></div>
                            <div class="form-group">
                                @if(!empty($sponsor->logo))
                                    <label for="logo"> <span class="btn btn-primary" value="{{ (!empty($sponsor->logo)) ? $sponsor->logo : old('logo') }}" id="fileName0">Browse</span></label>
                                    <input type="file"  name="logo" style="visibility: hidden; position: absolute;"  id="logo"  class="form-control">
                                @else
                                    <label for="logo"> <span class="btn btn-primary" value="{{ (!empty($sponsor->logo)) ? $sponsor->logo : old('logo') }}" id="fileName0">Browse</span></label>
                                    <input type="file"  name="logo" style="visibility: hidden; position: absolute;"  id="logo"  class="form-control" required>
                                @endif
                            </div>
                        </div>

                        <div class="form-group">
                            <button type="submit" class="btn btn-primary">Update</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </main>
    <script>
        function readURL(input) {
            if (input.files && input.files[0]) {
                var reader = new FileReader();

                reader.onload = function (e) {
                    $('#blah').attr('src', e.target.result);
                }

                reader.readAsDataURL(input.files[0]);
            }
        }

        $("#logo").change(function(){
            $('#prev').show();
            readURL(this);
        });
    </script>
@endsection

关于php - 图片没有显示并且无法在 laravel 中更新我的图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39893609/

相关文章:

php - 将两个 $_GET 值与数据库中的行进行比较

java - Android FileNotFound 异常 - 无法从没有文件格式的图像 URL 获取 InputStream

PHP验证: no spaces in string

php - 删除 Paypal 卡支付页面上的地址验证

php - 每月创建表

mysql - MySQL 记录之间的笛卡尔积

python - 如何根据另外两个给定图像对图像进行分类

c# - 在 ListViewItem C# 中添加多于 2 个图像

php - 从提交数组更新表数据会导致所有行因为相同的数据

php - 如何在ATK4中定义新模型