One thing I would maybe do is move the logic of uploading the cover_image to the model itself. I would also add an observer method to the model for setting the approved attribute.
// Your post model
class Post extends Model
{
protected $guarded = []; // for mass assigning in the controller
public function setCoverImage($file)
{
// your logic for storing the file
// then update the model (variables come from your logic)
$this->update(['cover_image' => $fileName, 'thumb_image' => $thumbnailpic]);
}
public static function boot()
{
parent::boot();
static::creating(function ($post) {
$post->approved = auth()->user()->type == 'admin' ? '1' : '0';
});
}
}
With that in your model now, your controller method could be something like this:
public function store(PostRequest $request)
{
// Create post
$post = new Post([
'title' => $request->input('title'),
'url' => $request->input('url'),
'slug' => str_slug($request->input('title')),
'body' => $request->input('body'),
'user_id' => auth()->id(),
]);
// Set cover image if set
if ($request->hasFile('cover_image')) {
$post->setCoverImage($request->file('cover_image'));
}
return redirect('/')->with('success', 'Post Created');
}