2017-01-09 203 views
0

你好,我是新来laravel,现在我面临这个错误BadMethodCallException in Macroable.php line 74: Method save does not exist.Macroable.php中的BadMethodCallException第74行:方法保存不存在。 Laravel 5.2

如果我var_dump(); die();所有的输出,然后我让所有的人,但是当我使用Save方法将结果保存到数据库中它给了我这个save method does not exist的错误。我不知道我的代码在哪里,我做错了。

请参阅路线控制器和视图以获得正确的理解。先谢谢你。

控制器

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}', '[email protected]'); 
Route::post('/t/{id}', '[email protected]'); 

查看

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

你想保存一个集合吗? '$ to'是'TO :: all()'的结果,它应该返回一个'Collection'。 –

+0

它只是一个文本区域消息以及一个ID和日期时间'例如'这是消息“添加在用户ID 5'' ...我不知道为什么我得到它... –

+0

我只是想在数据库中存储'id'和'result(message)'的列。 –

回答

1

你实例化Model的方法是错误的。尝试是这样的:

$to = new TO(); 

$to->d_id = $id; 
$to->outcome = Input::get('message'); 
$to->date = $time->toDateTimeString(); 

$to->save(); 
+0

这将工作肯定,但如果你在视图中看到有两个按钮“TA”和“TO”,所以我试图做的是,当用户点击在'TA'上数据保存在'TA'表中,当用户点击'TO'时,数据被存储在表'TO'中。使用'if((isset($ _ POST [“n”])&&!empty($ _ POST [“n”])))'......你明白了吗? –

+0

o是的,它已完成...非常感谢你的帮助...你是对的其实我正在实例化'模型错误'...我在'isset'中调用'new TO',它正在工作......我忘了这部分...非常感谢你的帮助... :) –

+0

我该如何选择这个作为可接受的答案? –

0

你并不需要获取所有项目更新使用相同的数据的所有项目?

如果模型字段可填写的,你可以指定质量他们。

$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 

不知道,如果你只需要更新1或很多的TN表格中的项目?

相关问题