Reputation: 85
Hello I am new to laravel and now I am facing this error BadMethodCallException in Macroable.php line 74: Method save does not exist.
If I var_dump(); die();
all of the outputs then I am getting all of them but when I use save method to save the results in to the database it gives me this error of save method does not exist
. I dont know where in my code am I doing it wrong.
Please see the route controller and view for proper understanding. Thank you in advance.
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>
public function getAllTn() {
return TN::all();
}
public function getAllTO(){
return TO::all();
}
Upvotes: 2
Views: 5340
Reputation: 5963
You do not need to fetch all items to update all items with the same data?
If the model fields are fillable, you can just mass assign them.
$data = [
't_type_id' => request()->input('options'),
'd_id' => $id,
'result' => request()->input('message'),
'date' => $time->toDateTimeString()
];
TN::update($data); // this will update all TN entries
Not sure if you just need to update 1 or a lot of items in the TN table?
Upvotes: 1
Reputation: 1348
Your way of instantiating Model
s is wrong. Try something like:
$to = new TO();
$to->d_id = $id;
$to->outcome = Input::get('message');
$to->date = $time->toDateTimeString();
$to->save();
Upvotes: 1