当我想为表格编辑一个值时,我得到了一个非法的字符串偏移量,但是当我尝试添加一个新值时,它可以添加一个新值。如果我注释掉表单的form::file部分,它可以返回view,所以我猜问题出在那儿。

<div class="col-md-12">
            <div class="portlet light bordered">
                <div class="portlet-body">
                    <form action="{{ $formIs === 'add' ? route('campus-media.save', $campusId) : route('campus-media.update', [$campusId, $campusMedia->id]) }}" method="post" class="form-horizontal" enctype="multipart/form-data">
                    {{ csrf_field() }}
                        <div class="form-body">
                            <div class="form-group">
                                <label class="control-label col-md-3">Title</label>
                                <div class="col-md-4">
                                    {!! Form::text('title', $campusMedia->title, array('class' => 'form-control', 'placeholder' => 'Campus Media Title')) !!}
                                </div>
                            </div>
                        </div>
                        <div class="form-body">
                            <div class="form-group">
                                <label class="control-label col-md-3">Media</label>
                                <div class="col-md-4">
                                    {!! Form::file('media', $campusMedia->media, array('class' => 'form-control')) !!}
                                </div>
                            </div>
                        </div>
                        <div class="form-body">
                            <div class="form-group">
                                <label class="control-label col-md-3">Media type</label>
                                    <div class="col-md-4">
                                        {!! Form::select('media_type', $fileType, $campusMedia->media_type,array('placeholder' => 'Choose media type', 'class' => 'form-control')) !!}
                                        <span class="help-block">0: Image, 1: Video, 2: Logo</span>
                                    </div>
                            </div>
                        </div>
                        <div class="form-body">
                            <div class="form-group">
                                <label class="control-label col-md-3">Description</label>
                                <div class="col-md-4">
                                    {!! Form::textarea('description', $campusMedia->description, array('class' => 'form-control', 'placeholder' => 'Campus Media Description', 'rows' => 3)) !!}
                                </div>
                            </div>
                        </div>
                        <div class="form-body">
                            <div class="form-group">
                                <label class="control-label col-md-3">Status</label>
                                    <div class="col-md-4">
                                        {!! Form::select('status', $status, $campusMedia->status,array('placeholder' => 'Choose status', 'class' => 'form-control')) !!}
                                        <span class="help-block">0: Inactive, 1: Active</span>
                                    </div>
                            </div>
                        </div>
                        <div class="form-body">
                            <div class="form-group">
                                <label class="control-label col-md-3">Tags</label>
                                <div class="col-md-4">
                                    {!! Form::text('tags', $campusMedia->tags, array('class' => 'form-control', 'placeholder' => 'Campus Media Tags')) !!}
                                </div>
                            </div>
                        </div>
                        <div class="form-actions">
                            <div class="row">
                                <div class="col-md-offset-3 col-md-9">
                                    <input type="submit" name="save" class="btn btn-primary" value="Save"/>
                                    <a href="{{ route('campus-media.index', $campusId) }}" class="btn btn-default"> Cancel </a>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>


这是我的控制器

class CampusMediaController extends Controller
{
//
public function index($campusId)
{
    $campusMedia = CampusMedia::ofCampus($campusId)->get();
    $data = [
        'campusMedia'   => $campusMedia,
        'campusId'      => $campusId
    ];

    return view('campus-media.index', $data);
}

public function add($campusId)
{
    $campusMedia = new CampusMedia;
    $data = [
        'campusMedia' => $campusMedia,
        'campusId'    => $campusId,
        'fileType'    => CampusMedia::getMediaType(),
        'status'      => CampusMedia::getStatus(),
        'formIs'      => 'add'
    ];

    return view('campus-media.form', $data);
}

public function save(Request $request, $campusId)
{
    $this->validation($request);
    $post = $request->all();
    $fileName = $this->upload($request, $campusId);
    $campusMedia = $this->bindToObject($post, $fileName, $campusId);
    $campusMedia->save();

    return redirect()->route('campus-media.index', $campusId)->with('message','Item Succesfully Added');
}

public function edit($campusId, $id)
{
    $campusMedia = CampusMedia::find($id);

    $data = [
        'campusMedia'             => $campusMedia,
        'campusId'                => $campusId,
        'fileType'                => CampusMedia::getMediaType(),
        'status'                  => CampusMedia::getStatus(),
        'formIs'                  => 'edit'
    ];

    return view('campus-media.form', $data);
}

public function update(Request $request,$campusId,$id)
{
    $this->validation($request);
    $post = $request->all();
    $fileName = $this->upload($request, $campusId);
    $campusMedia = $this->bindToObject($post, $fileName,$campusId,$id);
    $campusMedia->save();

    return redirect()->route('campus-media.index', $campusId)->with('message','Item updated Succesfully');
}

public function upload($request, $campusId)
{
    $this->validate($request, [
            'media' => 'mimes:png,jpeg,mp4,avi,mpeg,flv,m4v'
        ]
    );

    if($request->input('media_type') == 0){
        $type = "image";
    }elseif($request->input('media_type') == 1){
            $type = "video";
    }else{
        $type = "logo";
    }

    $file   = $request->file('media');
    if(is_null($file))
        return null;

    $ext = $file->getClientOriginalExtension();


    $fileName    = sprintf('%s-%s-%s.%s',
        $campusId,
        $type,
        time(),
        $ext
    );

    $upload      = config('app.upload_path').'/campus/'.$campusId;
    $file->move($upload,$fileName);

    return $fileName;
}

public function delete($campusId,$id)
{
    $campusMedia = CampusMedia::find($id);
    $campusMedia->delete();

    return redirect()->route('campus-media.index', $campusId)->with('message','Item deleted Succesfully');
}

private function validation($request){
    $this->validate($request, [
            'media' => 'required'
        ]
    );
}

private function bindToObject($post, $fileName,$campusId,$id=null)
{
    if(is_null($id)){
        $campusMedia = new CampusMedia;
    } else {
        $campusMedia = CampusMedia::find($id);
    }

    $campusMedia->campus_id = $campusId;
    $campusMedia->title = $post['title'];
    $campusMedia->media = $fileName;
    $campusMedia->media_type = $post['media_type'];
    $campusMedia->description = $post['description'];
    $campusMedia->status = $post['status'];
    $campusMedia->tags = $post['tags'];

    return $campusMedia;
}


}

我希望有人可以帮上忙,我是上载文件的新手。

最佳答案

您要将一个对象作为第二个参数传递给Form :: file,它必须是一个数组。

尝试这个:

{!! Form::file('media', array('class' => 'form-control')) !!}

关于php - 非法字符串偏移量'名称'Laravel,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44548423/

10-15 09:57