mercredi 17 octobre 2018

Where to put and how to call a helper function in my Laravel app?

Currently I have this code:

$image = Image::find($id);
$views = $image->views;
$image->views = $views + 1;
$image->save();

The code is located in the function which returns the view for a specificImage. The point is that whenever a user requests this view, the image views count should increase, however, I don't like having this logic in the function that returns the view so I am hoping to create a:

public function updateImageViews() {
    logic that updates views;
}

somewhere in my controllers (I've been thinking about putting it in my ArtworkController) and then just call the function in my function that returns the view.

Now I'm wondering whether this is a good way to do it. The problem is that I don't think this will work as of now since I don't think I'm allowed to call a function from another controller without doing something first.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire