Place boundary in relation one to many php laravel

0

I'm doing an app in laravel, but I get to a situation where the DB relationship is one to many users - articles, so my problem is that I want to place a limit that users can only add max 5 articles, I've been looking for, but nothing I try to try. I thank you

    
asked by Asdrubal Hernandez 25.07.2018 в 18:10
source

1 answer

0

That type of restrictions is better to work in the logical section of the application, which reduces the number of queries to your database. You can do a simple validation with the function count() of Laravel:

public function store($article) {
    $user = auth()->user(); // un usuario, el logueado por ejm
    if ($user->articles->count() >= 5) { // condición
        return 'Lo sentimos, has alcanzado el máximo permitido (5)' // mensaje
    }
    else {
        // la lógica de tu operación..
    }
    
answered by 26.07.2018 в 19:33