2016-05-25 49 views
0

我使用Laravel 5.2,我想改變多篇文章的狀態,查看是這個樣子:Laravel 5.2:如何更改多篇文章的狀態?

觀點:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="utf-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> 
    <meta http-equiv="x-ua-compatible" content="ie=edge"> 
    <link href="//cdn.bootcss.com/bootstrap/4.0.0-alpha.2/css/bootstrap.min.css" rel="stylesheet"> 
    <link href="//cdn.bootcss.com/tether/1.3.2/css/tether.min.css" rel="stylesheet"> 
</head> 
<body> 
<div class="container"> 
    <div class="card"> 
     <h3 class="card-header">Unpublished Articles</h3> 
     <div class="card-block"> 
      <div class="row"> 
       <div class="col-sm-4"> 
       </div> 
       <div class="col-sm-4"> 
        Title 
       </div> 
       <div class="col-sm-4"> 
        Status 
       </div> 
      </div> 
      <form class="form-horizontal" role="form" method="POST" 
        action="{{ url('articles/publish') }}"> 
       {!! csrf_field() !!} 
       <div class="row"> 
        <div class="col-sm-4"> 
         <label class="c-input c-checkbox"> 
          <input type="checkbox" name="article[]" value="1"> 
          <span class="c-indicator"></span> 
         </label> 
        </div> 
        <div class="col-sm-4"> 
         article1 
        </div> 
        <div class="col-sm-4"> 
         unpublished 
        </div> 
       </div> 
       <div class="row"> 
        <div class="col-sm-4"> 
         <label class="c-input c-checkbox"> 
          <input type="checkbox" name="article[]" value="2"> 
          <span class="c-indicator"></span> 
         </label> 
        </div> 
        <div class="col-sm-4"> 
         article2 
        </div> 
        <div class="col-sm-4"> 
         unpublished 
        </div> 
       </div> 
       <fieldset class="form-group"> 
        <button type="submit" class="btn btn-primary">Publish</button> 
       </fieldset> 
      </form> 
     </div> 
    </div> 
</div> 
<script src="//cdn.bootcss.com/jquery/2.2.3/jquery.min.js"></script> 
<script src="//cdn.bootcss.com/tether/1.3.2/js/tether.min.js"></script> 
<script src="//cdn.bootcss.com/bootstrap/4.0.0-alpha.2/js/bootstrap.min.js"></script> 
</body> 
</html> 

複選框的值是文章的id

文章:

id title  content  status  created_at updated_at 
1 article1 ...   0   ...   ... 
2 article2 ...   0   ...   ... 
3 article3 ...   1   ...   ... 

有表中的未公開的文章,狀態0, 和發表的文章狀態1

我想在提交時更改狀態選定的文章,如何編寫publish方法?

public function publish(Request $request) 
{ 

} 

如果單篇文章,我可以做到。
多篇文章,我不知道該怎麼做。
請一定幫忙,
提前致謝。

回答

1

你可以嘗試一些簡單:

Article::whereIn('id', $request->input('article'))->update(['published' => 1]); 
+0

我認爲,其中第二個參數必須是一個數組,不知道任何方式,如果這個dosent工作嘗試'$請求 - >輸入(「文章」) - >指定者()' –