JabirM
JabirM

Reputation: 13

Add a limit to users creating a item

I am using laravel for a project i am making. So i want that users can only create 30 products, and if they have more then 30 products, they cant create more products until they have removed some. What do i have to add in my code so that they cant add more products.

My Product controller

  public function store(Request $request)
{
    //check if user has more then 30 products

    $product = Product::create($request->all());
    $productPhotos = [];
    $photos = $request->post('photo');

    if (count($photos) <= 5) {
        foreach (range(1, $photos) as $i) {
            foreach ($photos as $imageData) {
                $bcheck = explode(';', $imageData);
                if (count($bcheck) > 1) {
                    list($type, $imageData) = explode(';', $imageData);
                    list(, $extension) = explode('/', $type);
                    list(, $imageData) = explode(',', $imageData);
                    $fileName = uniqid() . '.' . $extension;
                    $imageData = base64_decode($imageData);
                    Storage::put("public/products/$fileName", $imageData);
                    $imagePath = ('storage/products/' . $fileName);

                    $productPhotos[] = ProductPhoto::create([
                        'product_id' => $product->id,
                        'path' => $imagePath
                    ]);
                }
            }
        }
    } else {
        return response()->json("You cant add any photo's to your product", 400);
    }


    return response()->json([$product, $productPhotos], 201);
}

if i need to send some more code, let me know.

Thx in advance.

FINAL CODE:

   $totalProduct = Product::where('supplier_id', $request->user()->Supplier->id)->count();

    if ($totalProduct < 30){
        $product = Product::create($request->all());
        $productPhotos = [];
        $photos = $request->post('photo');


        if (count($photos)) {
            foreach ($photos as $i => $imageData) {

                if ($i >= 5) {
                    continue;
                }
                $bcheck = explode(';', $imageData);
                if (count($bcheck) > 1) {
                    list($type, $imageData) = explode(';', $imageData);
                    list(, $extension) = explode('/', $type);
                    list(, $imageData) = explode(',', $imageData);
                    $fileName = uniqid() . '.' . $extension;
                    $imageData = base64_decode($imageData);
                    Storage::put("public/products/$fileName", $imageData);
                    $imagePath = ('storage/products/' . $fileName);

                    $productPhotos[] = ProductPhoto::create([
                        'product_id' => $product->id,
                        'path' => $imagePath
                    ]);
                }

            }
        }
        return response()->json([$product, $productPhotos], 201);

    } else{
        return response()->json("you have to much products", 201);
    }

}

Upvotes: 0

Views: 323

Answers (4)

Dawood Akram
Dawood Akram

Reputation: 9

In User Model Add

public function canAddNewProduct() { return $this->products->count() < 30; }

In Controller

Auth::guard('web')->user()->canAddNewProduct()

Better way of doing is make a new middleware and use

return Auth::guard('web')->user()->canAddNewProduct() ? true : false

to protect the routes

Upvotes: 0

Diogo Santo
Diogo Santo

Reputation: 789

User Model

public function projects()
{
     return $this->hasMany('App\Project', 'id', 'user_id');
}

Project Model

public function users()
{
     return $this->belongsTo('App\User', 'user_id', 'id');
}

Controller to Add projects

public function store(Request $request)
{
     $totalCreatedProjects = $request->user()->products->count();
     if ($totalCreatedProjects < 30) {
            `Your code to add projects here`
     }
     `Your code to alert maximum projects achieved here`    
}

This example assumed:
1. Your users are authenticated;
2. You created a database relationship 1 to many between Projects and Users;

Notes: When you compare to the amount of projects that exist and the number you wish to hold the creation instead of proceeding, this should be a service. The number should be a constant with a proper semantic so other developers understand what you are trying to achieve

Upvotes: 2

hktang
hktang

Reputation: 1833

You can use this:

public function store(Request $request)
{
    if($request->user()->products->count() < 30) {

         //Add product
         ...

    }else{
        return redirect()->back();
    }
 }

Upvotes: 1

aleksejjj
aleksejjj

Reputation: 1835

public function store(Request $request)
{
    //check if user has more then 29 products
    if ($request->user()->products->count() >= 30) {
        return response()->json("You cant add more then 30 products", 400);
    }

    // Your code...

Upvotes: 0

Related Questions