2016-07-28 121 views
1

我试图使用评论模型视图associate文件模式电流id。但点击按钮后。它总是说。使用联想()方法将无法正常工作(BadMethodCallException)

调用未定义的方法照亮\数据库\查询\生成器::联想()

CommentController

class CommentController extends Controller 
{ 

    public function postComments(Request $request, Document $document) 
    { 

     $this->validate($request, 
     [ 
      'comment' => 'required', 
     ]); 

     $document = Document::find($document); 

     $commentObject = new Comment(); 

     $commentObject->comment = $request->comment; 
     $commentObject->associate($document); 
     $commentObject->save(); 

     return redirect()->back(); 
    } 
} 

我想在这里得到的当前id从视图模型,所以我可以把这个在我的$commentObject

DocumentController

//READ 
public function readDocuments($id) 
{ 
    //Find the document in the database and save as var. 
    $document = Document::find($id); 

    return view ('document.read')->with('document', $document); 
} 

路线

Route::get('/document/{id}', 
[ 
    'uses' => '\App\Http\Controllers\[email protected]', 
    'as' => 'document.read', 
    'middleware' => 'auth', 
]); 

这是我得到的视图的电流id。

//COMMENT 
Route::post('/document/{document}/comments', 
[ 
'uses' => '\App\Http\Controllers\[email protected]', 
'as' => 'comments', 
]); 

查看

<div class = "col-md-6"> 

    <div class = "form-group"> 

     <textarea id = "content">{{ $document->content }}</textarea> 

    </div> 

    <div class = "form-group"> 

     <button type = "submit" class = "btn btn-success">Approve</button> 

    </div> 
</div> 

<!--COMMENT CONTROLLER--> 
<div class = "col-md-6">           
    <form class = "form-vertical" method = "post" action = "{{ url('/document/'.$document->id.'/comments') }}"> 

     <div class = "form-group {{ $errors->has('comment') ? ' has-error' : '' }}"> 

      <label for = "comment">Comment:</label> 
      <textarea class = "form-control" rows = "4" id = "comment" name = "comment" placeholder = "Leave a feedback"></textarea> 

      @if ($errors->has('comment')) 
       <span class = "help-block">{{ $errors->first('comment') }}</span> 
      @endif 

     </div> 

     <div class = "form-group"> 

      <button type = "submit" class = "btn btn-primary">Comment</button> 

     </div> 

     <input type = "hidden" name = "_token" value = "{{ Session::token() }}"> 

    </form> 
</div> 

我想我有我的CommentController一个错误,因为当我试图保存插入。我认为它无法获得模型的当前ID。任何帮助或提示?谢谢!

更新

型号:

评论

class Comment extends Model 
{ 
    protected $tables = 'comments'; 

    protected $fillable = 
    [ 
     'comment', 
     'document_id', 
    ]; 

    public function documents() 
    { 
     return $this->belongsTo('App\Models\Document'); 
    } 
} 

文件

class Document extends Model 
{ 

    protected $table = 'documents'; 

    protected $fillable = 
    [ 
     'title', 
     'content', 
     'category_id', 
    ]; 

    public function comments() 
    { 
     return $this->hasMany('App\Models\Comment'); 
    } 
} 

回答

1

associate()方法应该应用于关系。

我相信(考虑到你Comment模式已documents关系),你应该使用

$commentObject->documents()->associate($document); 

而且,我相信你的方法应该如下..

public function postComments(Request $request, Document $document) 
{ 

    $this->validate($request, 
    [ 
     'comment' => 'required', 
    ]); 

    //No need to do the following - Laravel 5.2 will Model Bind it because of Document $document in parameters above 
    //$document = Document::find($document); 

    $commentObject = new Comment(); 

    $commentObject->comment = $request->comment; 

    $document->comments()->save($commentObject); 
    //..other tasks 


    return redirect()->back(); 
} 

$document->comments()是一个查询生成器,当你通过它的->save($comment),它更新$comment对象的document_id财产,将其设置为$document->id,即$document对象调用此查询的id。然后保存$comment对象。

+0

如何'关联()'链接到我的评论模型的'document_id'(外键)?请参阅我更新的帖子..我有一个错误说。 SQLSTATE [42S22]:未找到列:1054'字段列表'中的未知列'documents_id'(SQL:插入到“评论”('comment','documents_id','updated_at','created_at')values(hahaha, ,2016-07-28 15:40:22,2016-07-28 15:40:22))' – Francisunoxx

+0

@francisunoxx请检阅..我更新了 –

+0

哇!我只是哇!有用!!我在这里有点困惑。 '$ document-> comments() - > save($ commentObject);'你能说出这件事吗? – Francisunoxx