调用数组 laravel 上的成员函数 save()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41532416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call to a member function save() on array laravel
提问by Angel
Im doing the update method. When I tried to update the specific data, im getting this error "Call to a member function save() on array". Why? Is there's something missing in my codes?
我在做更新方法。当我尝试更新特定数据时,我收到此错误“调用数组上的成员函数 save()”。为什么?我的代码中是否缺少某些内容?
I also tried to print_r the $result variable, it has a value..
我还尝试打印 $result 变量,它有一个值..
View
看法
@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">×</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>
<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
Controller
控制器
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
路线
Route::post('updateText/{id}','AdministratorController@updateText');
采纳答案by mizan3008
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();
}
This way can help you.
这个方法可以帮到你。
回答by Alexey Mezenin
To use save()
method you need to get an object:
要使用save()
方法,您需要获取一个对象:
$result = Model::find($id);
$result->name = 'John';
$test = $result->save();