微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

调用数组laravel上的成员函数save()

我在做更新方法.当我尝试更新特定数据时,我收到此错误“在数组上调用成员函数save()”.为什么?我的代码中缺少什么吗?

我还尝试了print_r $result变量,它有一个值.

视图

@extends('dashboard')
@section('content')

<br><br>
<div class="x_content">
  <table class="table table-hover">
     <thead>
      <tr>
        <th>#</th>
        <th>Text Header</th>
        <th>Text Description</th>
        <th>Action</th>
        <th>Updated At</th>
        <th>Created At</th>
      </tr>
    </thead>
    <tbody>
      @foreach($data as $text)
      <tr>
        <th scope="row">{{ $text->id }}</th>
        <td>{{ $text->text_header}}</td>
        <td>{!! nl2br(e($text->text_description)) !!}</td>
        <td><button class = "btn btn-info btn-md" data-toggle="modal" data-target="#myModal-{{$text->id}}"><i class="fa fa-pencil-square-o" aria-hidden="true"></i> Edit</button></td>
        <div class="modal fade" id="myModal-{{$text->id}}" tabindex="-1" role="dialog" aria-labelledby="titleLabel">
          <div class="modal-dialog" role="document">
            <div class="modal-content">
              <div class="modal-header modal-header-success">
                <button type="button" class="close btn btn-primary" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="titleLabel">New Content</h4>
              </div>
              <div class="modal-body">
                <div class="row">
                  <div class ="col-md-2 col-lg-7">
                    <div style="display: inline">
                      <div class="form-group">
                          <form action="{{ url('updateText/'.$text->id) }}" enctype="multipart/form-data" method="POST">
                           {{ csrf_field() }}
                          <label>Title Header</label>
                          <input type= "text" name = "title_header"value =" {{ $text->text_header}}" class = "form-control"></input>
                      </div>
                    </div>
                    <div class ="col-md-4 col-lg-9">
                      <div class="form-group">
                          <label>Title Description</label>
                          <textarea style="resize:none"> {{ $text->text_description}}</textarea>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
              <div class="modal-footer">
                <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>&nbsp;
                <span class="pull-right">
                   <button type="submit" class="btn btn-success">Update</button>
                </span>
              </form>
              </div>
            </div>
          </div>
        </div>
        <td>{{ $text->updated_at}}</td>
        <td>{{ $text->created_at}}</td>

      </tr>

      @endforeach
      {{ $data->links() }}

    </tbody>
  </table>
</div>
@stop

控制者

public function updateText(Request $request, $id)
{

    $result = $request->all();
    $test = $result->save();
    print_r($test);die;

    Session::flash('flash_message', 'Task successfully added!');

    return redirect()->back();
}

路线

Route::post('updateText/{id}','AdministratorController@updateText');

解决方法:

public function updateText(Request $request, $id)
{
    $result = YourModel::find($id);
    $result->your_column_1 = $request->your_value_1;
    $result->your_column_2 = $request->your_value_2;
    $result->your_column_3 = $request->your_value_3;
    $result->your_column_4 = $request->your_value_4;
    .
    .
    .
    $result->your_column_n = $request->your_value_n;

    if($result->save()){
        Session::flash('flash_message', 'Task successfully added!');
    }else{
        Session::flash('flash_message', 'Task Failed!');
    }

    return redirect()->back();
}

这样可以帮助您.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。