image不显示,无法更新我在laravel中的图像

ugmeyewa  于 2021-06-25  发布在  Mysql
关注(0)|答案(1)|浏览(336)

我在做一个项目。在这个项目中,我想更新名称,网站和图像字段。我希望如果用户选择了新的,那么字段将更新,否则它将保留过去的值。这对我的名字和网站链接都很好。但我不能像这样做的图像领域。请帮帮我。我的控制器是

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

请帮我解决这个问题

qvsjd97n

qvsjd97n1#

答案是

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

视图文件将

@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

相关问题