图片未在laravel 4中更新

时间:2015-02-19 08:31:54

标签: jquery laravel-4

我有一个页面,我可以更新我的画廊和所有更新,除了我的图像。如果我使用

echo "<PRE>";
print_r($input);
die();

然后我看到除了图像之外的所有内容都已发布。我不确定我在哪里出错了。

我的GalleryController.php

public function edit($id)
{
    $galleries = Gallery::find($id);

    if(is_null($galleries)){
        return Redirect::route('admin.gallery.edit');
    }

    return View::make('gallery::edit', compact('galleries'));
}

/**
 * Update the specified resource in storage.
 * PUT /gallery/{id}
 *
 * @param  int  $id
 * @return Response
 */
public function update($id)
{
    $input = Input::all();
    $validation = Validator::make($input, Gallery::$rules);

    if($validation->fails()){
        return Redirect::route('admin.gallery.edit')
            ->withInput()
            ->withErrors($validation)
            ->with('message', 'There were validation errors');
    }

    if($validation->passes()){
        $galleries = Gallery::FindOrFail($id);
        // echo "<PRE>";
        // print_r($galleries);
        // die();
        $galleries->update($input);

        echo "<PRE>";
        print_r($input);
        die();

        return Redirect::route('admin.gallery.index', $id);
    }
}

我的edit.blade.php

@extends('templates::admin')
@section('content')

<script>
$(".add-form").submit(function(event){
    $(this).find(".test-upload").each(function(){
        var image = [];
        $(this).parent().find(".ajax-file-upload-statusbar").each(function()   {
            image.push($.trim($(this).find(".ajax-file-upload-filename").html()));
        });
        $(this).parent().find(".img-hidden").val(JSON.stringify(image));
    });

    $.ajax({
        type: 'POST',
        url: $(this).attr('action'),
        data: $(this).serialize(),
        dataType: 'json',
        encode : true,
    });
});
</script>

<script type="text/javascript">
    var settings = {
            url: '{{ asset("upload/upload.php") }}',
            //checkExisting : '{{ asset("vendor/afishinsea/uploadify/check-exists.php?target=banners_images") }}',
            dragDrop:true,
            multiple : false,
            showFileCounter:false,
            showDone: false,
            fileName: "myfile",
            allowedTypes:"jpg,png,gif,pdf", 
            returnType:"json",
            showDelete:true,
        }
</script>
<div class="row">
    <div class="col-lg-12">
        {{ Form::model($galleries, array('method' => 'PATCH', 'route' => array('admin.gallery.update', $galleries->id))) }}
    <div class="form">
        <div>
            {{ Form::label('title', 'Title') }}
        </div>
        <div>
            {{ Form::text('title', $galleries->title, array('id' => 'title', 'class' => 'form-control')) }}
        </div>
        <div>
            {{ Form::label('content', 'Content') }}
        </div>
        <div>
            {{ Form::textarea('content', $galleries->content) }}
        </div>




<div class="image_uploader">
<div id="fileuploader-{{ $galleries->id }}" class="test-upload">Upload</div>
{{ getImages($galleries->image) }}
<script>
    var uploadObj = $(".test-upload").uploadFile(settings);
</script>
{{ Form::hidden('image', '', array('id' => 'img-add_'.$galleries->id, 'class' => 'img-hidden')) }}
</div>




        <div>
            {{ Form::submit('Submit', array('class' => 'btn btn-default', 'role' => 'button')) }}
        </div>
    </div>
{{ Form::open() }}

@if($errors->any())
    <ul>
        {{ implode('', $errors->all('<li class="error">:message</li>')) }}
    </ul>
@endif
    </div>
</div>
@stop

我的upload.php

<?php
$output_dir = "../uploads/";
if(isset($_FILES["myfile"]))
{
$ret = array();

//  This is for custom errors;  
/*  $custom_error= array();
$custom_error['jquery-upload-file-error']="File already exists";
echo json_encode($custom_error);
die();
*/
$error =$_FILES["myfile"]["error"];
//You need to handle  both cases
//If Any browser does not support serializing of multiple files using  FormData() 
if(!is_array($_FILES["myfile"]["name"])) //single file
{
    $fileName = $_FILES["myfile"]["name"];
    move_uploaded_file($_FILES["myfile"]["tmp_name"],$output_dir.$fileName);
    $ret[]= $fileName;
}
else  //Multiple files, file[]
{
  $fileCount = count($_FILES["myfile"]["name"]);
  for($i=0; $i < $fileCount; $i++)
  {
    $fileName = $_FILES["myfile"]["name"][$i];
    move_uploaded_file($_FILES["myfile"]["tmp_name"] [$i],$output_dir.$fileName);
    $ret[]= $fileName;
  }

}
echo json_encode($ret);
}
?>

UPDATE 我在我的视图中编辑了我的代码

{{ Form::model($galleries, array('files' => 'true', 'method' => 'PATCH', 'route' => array('admin.gallery.update', $galleries->id))) }}

我在控制器中添加了这段代码

dd($input);

这就是我在编辑项目时得到的内容

  

数组(大小= 5)    '_method'=&gt;字符串'PATCH'(长度= 5)    '_token'=&gt; string'cOOg56pbcKJ3BDgsQWL6MGxK4CO1SXdGRq8rd1rV'(长度= 40)    'title'=&gt;字符串'test'(长度= 4)    'content'=&gt;字符串'

测试

'(长度= 11)    'image'=&gt; string''(length = 0)

1 个答案:

答案 0 :(得分:0)

我认为您错过了'files' => true来电中的Form::model设置。我从未在Form :: model上使用它,但我想象你想写:

{{ Form::model($galleries, array('method' => 'PATCH', 'route' => array('admin.gallery.update', $galleries->id), 'files' => true)) }}