2017-01-08 13 views
2

更新メソッドを実行しています。特定のデータを更新しようとしたときに、このエラーが発生しました。「配列のメンバー関数save()を呼び出す」どうして?私のコードに何かがありませんか?配列laravelのメンバー関数save()を呼び出す

私も$結果変数をしますprint_rしようとした、それは価値があります。..

ビュー

@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}','[email protected]'); 
+1

このエラーは簡単です。あなたは実際にどこを救おうとしていますか? –

+0

私のモデルに? – Angel

+1

あなたはdbからオブジェクトを見つけて、そのオブジェクトにモデルオブジェクトと呼び出し保存メソッドを作成する必要があります –

答えて

1
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(); 
} 

この方法では、あなたを助けることができます。

+0

あなたが言及している価値は私の見解にあるのでしょうか? – Angel

+0

ありがとうございました。私の間違いは私の変数名です。 – Angel

+0

あなたは大歓迎です。 いいコーディングがあります:) – mizan3008

0

save()メソッドを使用するには、オブジェクトを取得する必要があります。

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

Sirは$ result = text :: find($ id);を試しました。 $ result-> text_header =入力:: get( 'text_header'); $ result-> save(); .. nullとすることはできません – Angel

+0

これは通常、フィールドの1つが空であることを意味します。 'dd(Input :: get( 'text_header'));'は何をしますか? –

+0

私のビュー内の変数名がコントローラで一致しません..今、大丈夫です。Sir、updated_at列に記入したいのですが?私はどうすればいいのですか? codeigniterでも同じですか? ( '');(私はコードを忘れてしまった)? – Angel

関連する問題