画廊图像未在数据库中更新

时间:2019-04-15 17:39:31

标签: laravel

我有一个活动管理公司的网站。 https://redcarpetevents.in。唯一的问题是我无法更新图库。我可以上传新图像,效果很好。但是当我编辑数据库中已有的图像时,它没有得到更新。

routes.php

    Route::get('admin/editGalleryForm/{type}/{editId}', array('as'=>'editGalleryForm','before'=>'authAdmin','uses'=>'AdminController@editGalleryForm'));

Route::post('admin/editGallery', array('as'=>'editGallery','before'=>'csrf|xss_clean|authAdmin','uses'=>'FormController@editGallery'));

Model.php

public static function editGallery($data){
        $gallery = Gallery::find($data['editId']);
        $gallery->type = $data['type'];
        $gallery->tag = $data['tag'];


        $gallery->position = $data['order'];

        if (isset($data['status'])) {
            $gallery->status = $data['status'];
        }else{
            $gallery->status = 'off';
        }
        if (isset($data['home'])) {
            $gallery->home = $data['home'];
        }else{
            $gallery->home = 'off';
        }
        if (isset($data['title'])) {
            $gallery->title = $data['title'];
        }

        if($gallery->update()){
            return true;
        }else{
            return false;
        }
    }

    public static function getGalleryCounts(){
        $galleryCounts = array();

        $galleryCounts['all'] = count(Gallery::all());
        $galleryCounts['allActive'] = count(Gallery::where('status','=','on')->get());
        $galleryCounts['allInActive'] = count(Gallery::where('status','=','off')->get());
        $galleryCounts['personal'] = count(Gallery::where('type','=','1')->get());
        $galleryCounts['personalActive'] = count(Gallery::where('type','=','1')->where('status','=','on')->get());
        $galleryCounts['personalInActive'] = count(Gallery::where('type','=','1')->where('status','=','off')->get());
        $galleryCounts['corporate'] = count(Gallery::where('type','=','2')->get());
        $galleryCounts['corporateActive'] = count(Gallery::where('type','=','2')->where('status','=','on')->get());
        $galleryCounts['corporateInActive'] = count(Gallery::where('type','=','2')->where('status','=','off')->get());

        return $galleryCounts;
    }

Admincontroller.php

public function editGalleryForm($type,$id){
    $data['id'] = $id;
    $data['type'] = $type;

    $data['galleryCounts'] = Gallery::getGalleryCounts();
    $data['image'] = Gallery::getGalleryById($id);

    $data['personalTags'] = GalleryTag::getPersonalActive();
    $data['corporateTags'] = GalleryTag::getCorporateActive();

    return View::make('admin.editGalleryForm', $data);
}

Formcontroller.php

public function editGallery(){
        $type = Input::get('type');
        $editId = Input::get('Id');
        $filename=Input::get('image');
        $rules = array(

            'order'=>'Numeric');

        $validation = Validator::make(Input::all(), $rules);

        if ($validation->fails()) {
            $data['type'] = $type;
            $data['Id'] = $editId;
            return Redirect::route('editGalleryForm',$data)->withInput()->withErrors($validation);
        }

        if ($validation->passes()) {

            $fileName='';
            if (Input::hasFile('image')) {
                $name = Input::file('image')->getClientOriginalName();
                $destinationPath = 'assets/img/gallery/';
                $data['Id'] = $Id;
                $fileName = $name;
                Input::file('image')->move($destinationPath, $fileName);
                //Gallery::convertImage($destinationPath,$fileName);
            }

            $editGallery = Gallery::editGallery(Input::all());


            if ($editGallery) {
                $data['success'] = 'Gallery Image Updated!';
                $data['type'] = $type;
                return Redirect::route('newGallery',$data);
            }
            else
            {
                $data['failure'] = 'Operation failed!';
                $data['type'] = $type;
                return Redirect::route('newGallery',$data);
            }

        }
    }

这是表格

@extends('admin.layouts.master')
@section('pageHeader')

<h2>Edit Gallery Image</h2>

<ol class="breadcrumb">
    <li>{{HTML::linkRoute('adminHome','Dashboard')}}</li>
    <li>{{HTML::linkRoute('newGallery','Gallery',array('type'=>$type))}}</li>


    <li class="active">Edit Gallery Image</li>
</ol>


@stop

@section('content')
<div class="cl-mcont">
<div class="status_bar">

  <div class="butpro butstyle {{($type=='1')? 'status_active':''}}"><a href="{{URL::route('newGallery',array('type'=>'1'))}}">
    <div class="sub"><h2>PERSONAL</h2><span id="total_clientes">{{$galleryCounts['personal']}} Image{{($galleryCounts['personal'] > 1)? 's':''}}</span></div>
    <div class="stat">{{$galleryCounts['personalActive']}} <i style="color:#00FF00" class="fa fa-check"></i> {{$galleryCounts['personalInActive']}} <i style="color:#FF0000" class="fa fa-times"></i></div>
  </a></div>
  <div class="butpro butstyle {{($type=='2')? 'status_active':''}}"><a href="{{URL::route('newGallery',array('type'=>'2'))}}">
    <div class="sub"><h2>CORPORATE</h2><span id="total_clientes">{{$galleryCounts['corporate']}} Image{{($galleryCounts['corporate'] > 1)? 's':''}}</span></div>
    <div class="stat">{{$galleryCounts['corporateActive']}} <i style="color:#00FF00" class="fa fa-check"></i> {{$galleryCounts['corporateInActive']}} <i style="color:#FF0000" class="fa fa-times"></i></div>
  </a></div>
</div>

<div class="row">
<div class="col-md-12">
{{Form::open(array('action'=>'editGallery','method'=>'POST','files'=>true,'class'=>'form-horizontal'))}}

  <div class="form-group">
    {{Form::label('forType', 'Type', array('class'=>'col-sm-2 control-label'))}}
  <div class="col-sm-6">
    {{Form::select('type', array('1'=>'personal','2'=>'corporate'), ($image->type=='2')? '2':'1', array('class'=>'form-control','onChange'=>'tagChange(this)'))}}  
    <div class="color-danger">{{$errors->first('type')}}</div>
  </div>
  </div>

<div class="form-group">
    {{Form::label('forTag', 'Tag', array('class'=>'col-sm-2 control-label'))}}
  <div class="col-sm-6">
    <select class="form-control" id="tags" name="tag">
      @if($type == 2)
        @foreach($corporateTags as $tag)
          <option {{($image->tag == $tag->id)? 'selected':''}} value="{{$tag->id}}">{{$tag->name}}</option>
        @endforeach
      @else
        @foreach($personalTags as $tag)
          <option {{($image->tag == $tag->id)? 'selected':''}} value="{{$tag->id}}">{{$tag->name}}</option>
        @endforeach
      @endif
    </select>  
    <div class="color-danger">{{$errors->first('tag')}}</div>
  </div>
  </div>

  <div class="form-group">
    {{Form::label('forTitle', 'Title', array('class'=>'col-sm-2 control-label'))}}
  <div class="col-sm-6">
    {{Form::text('title', $image->title, array('class'=>'form-control','placeholder'=>'Image Title'))}}  
    <div class="color-danger">{{$errors->first('title')}}</div>
  </div>
  </div>

  <div class="form-group">
    {{Form::label('forImage', 'Gallery Image', array('class'=>'col-sm-2 control-label'))}}
  <div class="col-sm-6">
    {{Form::file('forimage',  array('class'=>'form-control','placeholder'=>'image'))}}  (600 X 400)px
    <div class="color-danger">{{$errors->first('image')}}</div>
  </div>
  </div>

  <div class="form-group">
    {{Form::label('', '', array('class'=>'col-sm-2 control-label'))}}
  <div class="col-sm-6">
    <div class="col-md-2 padding photo">
        <a href="{{asset('assets/img/gallery/'.$image->image)}}" rel="shadowbox"><img src="{{asset('assets/img/gallery/'.$image->image)}}" width="200px" height="120px"></a>
    </div>
  </div>
  </div>

  <div class="form-group">
    {{Form::label('forOrder', 'Order', array('class'=>'col-sm-2 control-label'))}}
  <div class="col-sm-6">
    {{Form::text('order', $image->position, array('class'=>'form-control','placeholder'=>'Image Order'))}}  
    <div class="color-danger">{{$errors->first('order')}}</div>
  </div>
  </div>

  <div class="form-group">
    {{Form::label('forStatus', 'Publish Status', array('class'=>'col-sm-2 control-label'))}}
  <div class="col-sm-6">
    <div class="switch" data-on="success">
        <input type="checkbox" name="status"{{($image->status=='on')? 'checked':''}}>
    </div>
  </div>
</div>


  <div class="form-group">
    {{Form::label('forShow', 'Show on Homepage', array('class'=>'col-sm-2 control-label'))}}
  <div class="col-sm-6">
    <div class="switch" data-on="success">
      <input type="checkbox" name="home"{{($image->home=='on')? 'checked':''}}>
  </div>
  </div>
</div>

    {{Form::text('editId', $image->id, array('style'=>'display:none'))}}

  <div class="form-group">
  <div class="col-sm-offset-2 col-sm-10">
    {{Form::submit('Update Gallery', array('class'=>'btn btn-primary'))}}
    {{HTML::linkRoute('newGallery','Cancel',array('type'=>$type),array('class'=>'btn btn-default'))}}

  </div>
  </div>
{{Form::token().Form::close()}}         
</div>
</div>
</div>
@stop
@section('customScripts')
<script type="text/javascript">
function tagChange(element){
  console.log(element.value);
  $('#tags').empty();
  $.ajax({
    url:'{{URL::route("getTagsAjax")}}',
    type:'POST',
    data:{'type':element.value},
    dataType:'JSON',
    success:function(data){
      console.log(data);
      for (var i = 0;  i <= data.length; i++) {
        $('#tags').append('<option value="'+data[i].id+'">'+data[i].name+'</option>');
      };
    }
  });
}
</script>
@stop

0 个答案:

没有答案