Article management

Article management is the most advanced example included in the Pro theme, because every article has a picture, belongs to a category and has multiple tags. To access this example click the "Examples/Article Management" link in the left sidebar or add /article to the URL.

Here you can manage the articles. A list of articles will appear once you start adding them (to access the add page click "Add article"). On the add page, besides the Name and Description fields (which are present in most of the CRUD examples) you can see a category dropdown, which contains the categories you added, a file input and a tag multi select. If you did not add any categories or tags, please go to the corresponding sections (category management, tag management) and add some.

The code for saving a new article is a bit different than before (see snippet bellow):


public function store(ArticleRequest $request, Article $model)
{
    $article = $model->create($request->merge([
        'picture' => $request->photo->store('pictures', 'public'),
        'status' => $request->status ? 'published' : 'draft',
        'show_on_homepage' => $request->show_on_homepage ? 1 : 0,
        'publish_date' => $request->publish_date ? Carbon::parse($request->publish_date)->format('Y-m-d') : null,
        'author_id' => auth()->user()->id
    ])->all());
    
    $article->tags()->sync($request->get('tags'));

    return redirect()->route('article.index')->withStatus(__('Article successfully created.'));
}

Notice how the picture and tags are easily saved in the database and on the disk.

Similar to all the examples included in the theme, this one also has validation rules in place. Note that the picture is mandatory only on the article creation. On the edit page, if no new picture is added, the old picture will not be modified.


public function rules()
{
    return [
        'title' => [
            'required', 'min:3', Rule::unique((new Article)->getTable())->ignore($this->route()->article->id ?? null)
        ],
        'category_id' => [
            'required', 'exists:'.(new Category)->getTable().',id'
        ],
        'content' => [
            'required'
        ],
        'tags' => [
            'required'
        ],
        'tags.*' => [
            'exists:'.(new Tag)->getTable().',id'
        ],
        'photo' => [
            $this->route()->article ? 'nullable' : 'required', 'image'
        ],
        'publish_date' => [
            'required',
            'date_format:d-m-Y'
        ]
      ];
}
    

In the article management we have an observer (`app\Observers\ArticleObserver`) example. This observer handles the deletion of the picture from the disk when the article is deleted or when the picture is changed via the edit form. The observer also removes the association between the article and the tags.


public function deleting(Article $article)
{
    File::delete(storage_path("/app/public/{$article->picture}"));
    
    $article->tags()->detach();
}
        

The policy which authorizes the user on article management pages is implemented in App\Policies\ArticlePolicy.php.