我正在使用laravel进行我正在做的项目。 因此,我希望用户只能创建30种产品,并且如果他们拥有30种以上的产品,那么在删除某些产品之前,他们不能创建更多产品。我必须在代码中添加些什么,以便他们不能添加更多产品。
我的产品控制器
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);
}
如果我需要发送更多代码,请告诉我。
提前谢谢。
最终代码:
$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);
}
}
答案 0 :(得分:1)
用户模型
public function projects()
{
return $this->hasMany('App\Project', 'id', 'user_id');
}
项目模型
public function users()
{
return $this->belongsTo('App\User', 'user_id', 'id');
}
要添加项目的控制器
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`
}
此示例假定:
1.您的用户已通过身份验证;
2.您在项目和用户之间创建了一个多对多的数据库关系;
注意:当您比较现有项目的数量和希望保留创建而不是继续进行的项目的数量时,这应该是一项服务。 该数字应为具有适当语义的常数,以便其他开发人员了解您要实现的目标
答案 1 :(得分:1)
您可以使用此:
public function store(Request $request)
{
if($request->user()->products->count() < 30) {
//Add product
...
}else{
return redirect()->back();
}
}
答案 2 :(得分:0)
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...
答案 3 :(得分:0)
public function canAddNewProduct()
{
return $this->products->count() < 30;
}
Auth::guard('web')->user()->canAddNewProduct()
return Auth::guard('web')->user()->canAddNewProduct() ? true : false
保护路线