你好,我是 Laravel 的新手,现在我遇到了这个错误 BadMethodCallException in Macroable.php line 74: Method save does does notence.
如果我var_dump(); die();
所有输出,然后我得到了所有输出,但是当我使用 save 方法将结果保存到数据库中时,它给了我这个错误 save 方法不存在
。我不知道我的代码哪里做错了。
请参阅路由 Controller 和 View 以获得正确的理解。预先感谢您。
Controller
public function td($id) {
$tn = $this->t->getAllTn();
$to = $this->t->getAllTo();
$time = Carbon\Carbon::now(); // current time
$time->toDateTimeString(); // converting time to string
if((isset($_POST["n"]) && !empty($_POST["n"]))) {
$tn->t_type_id = Input::get('options');
$tn->d_id = $id;
$tn->result = Input::get('message');
$tn->date = $time;
// var_dump($tn->date);
// var_dump($tn->t_type_id);
// var_dump($tn->d_id);
// var_dump($tn->result);
// die();
Session::flash('message', 'Your tn has been added.');
$tn->save();
} else if((isset($_POST["o"]) && !empty($_POST["o"]))) {
$to->d_id = $id;
$to->outcome = Input::get('message');
$to->date = $time->toDateTimeString();
// var_dump($to->d_id);
// var_dump($to->outcome);
// var_dump($to->date = $time->toDateTimeString());
// die();
Session::flash('message', 'Your to has been added.');
$to->save();
}
return redirect('/t');
}
路线
Route::get('/t/{id}', 'TController@td');
Route::post('/t/{id}', 'TController@td');
查看
<div class="form-group">
<form action="/t/{{ $d['id'] }}" method="post">
{{ csrf_field() }}
<div class="panel-body"><h4>Heading here</h4></div>
<select class="form-control" id="options" name="options" style="width:100%" type="checkbox">
@foreach($t as $t)
<option value="{{ $t->id }}">{{ $t->type }}</option>
@endforeach
</select>
</div>
<div class="col-md-4" id="value" align="center">
<div class="panel panel-warning">
<div class="panel-heading">
Enter text below
</div>
<div class="form-group has-success">
<textarea class="form-control" id="message" name="message" placeholder="Please enter your message here..." rows="5"></textarea>
<input type="submit" class="btn btn-primary" name="n" value="A-N">
<input type="submit" class="btn btn-primary" name="o" value="A-O">
</div>
</form>
<!-- Notes: <br/>-->
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
function hide() {
$("#value").hide();
$("#h").hide();
$("#search").hide();
}
function show() {
$("#value").show();
$("#h").show();
$("#search").show();
}
function initHandlers() {
$("#options").on('click', function() {
show();
});
}
hide();
initHandlers();
</script>
接口(interface)实现
public function getAllTn() {
return TN::all();
}
public function getAllTO(){
return TO::all();
}
最佳答案
您实例化模型的方式是错误的。尝试这样的事情:
$to = new TO();
$to->d_id = $id;
$to->outcome = Input::get('message');
$to->date = $time->toDateTimeString();
$to->save();
关于php - Macroable.php 第 74 行 : Method save does not exist. Laravel 5.2 中的 BadMethodCallException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41550219/