zwl1619
zwl1619

Reputation: 4232

Laravel 5.2 : How to change the status of multiple articles?

I am using Laravel 5.2 ,I want to change the status of multiple articles,view is like this:

view:

<!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>

the value of checkbox is the article's id.

articles:

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

There are unpublished articles in the table, the status is 0, and published articles' status is 1,

I want to change the status when submitting the selected article(s),how to write the publish method?

public function publish(Request $request)
{

}

if single article, I can do it.
multiple articles ,I don't know how to do it.
some help please,
thanks in advance.

Upvotes: 0

Views: 412

Answers (1)

Denis Mysenko
Denis Mysenko

Reputation: 6534

You could try something as simple as:

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

Upvotes: 2

Related Questions