簡體   English   中英

調用數組 laravel 上的成員函數 save()

[英]Call to a member function save() on array laravel

我在做更新方法。 當我嘗試更新特定數據時,我收到此錯誤“調用數組上的成員函數 save()”。 為什么? 我的代碼中是否缺少某些內容?

我還嘗試打印 $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();
}

這個方法可以幫到你。

要使用save()方法,您需要獲取一個對象:

$result = Model::find($id);
$result->name = 'John';
$test = $result->save();

問:為什么返回此消息 -> Call to a member function save() on array laravel Ans:首先,您檢查表中是否存在$id值。

如果在表中未找到此主要$id值,則返回此消息。

如果主$id值可用,那么您可以通過兩種方式保存

    $data = Model::find($id);
    $data->name = 'Bulbul';
    $result = $data->save();
    //OR 
    $data = Model::find($id);
    $data['name'] = 'Bulbul';
    $result= $result->save();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM