首页 > 解决方案 > 我想在 count = 1 时禁用删除按钮,我想在 count 等于 0 时启用删除按钮

问题描述

@extends('layouts.app')
@section('content')
    <table class="table">
        <thead>
            <tr>
                <th scope="col">id</th>
                <th scope="col">PRODUCT_NAME</th>
                <th scope="col">PRODUCT_DESCRIPTION</th>
                <th scope="col">PRODUCT_IMAGE</th>
                <th scope="col">PRODUCT_CODE</th>
                <th scope="col">CATEGORY_NAME</th>
                <th scope="col">OPERATION</th>
            </tr>
        </thead>
        <tbody> @foreach ($products as $product ) <tr>
                <td scope="col">{{ $product->id}}</td>
                <td scope="col">{{ $product->Product_name}}</td>
                <td scope="col">{{ $product->Product_description}}</td>
                <td scope="col">{{ $product->Product_image}}</td>
                <td scope="col">{{ $product->Product_code}}</td>
                <td scope="col">{{ $product->Category_id}}</td>
                <td scope="col">
                    <form action="{{route('store.product.destroy',$product->id)}}" method="POST"
                        onsubmit="return confirm('Are you sure want to delete it')"> <a class="btn btn-info"
                            href="{{route('store.product.edit',$product->id)}}">EDIT</a> <a class="btn btn-warning"
                            href="{{ route('store.product.show',$product->id)}}">SHOW</a> @csrf @method('DELETE') <button
                            type="submit" class="btn btn-danger" id="delete">DELETE</button> </form>
                </td>
            </tr> @endforeach </tbody>
    </table>
    <form action="{{route('store.product.create')}}" method="GET">
        <div class="form-group"> <button type="submit" class="btn btn-primary">Create</button> </div>
    </form>
@endsection

这是我的控制器页面:

<?php

namespace App\Http\Controllers;

use App\Models\Category;
use App\Models\Product;
use Illuminate\Http\Request;

class ProductController extends Controller {
    /**
     * Display a listing of the resource.
     * @return \Illuminate\Http\Response
     */
    public function index() {
        $data=array( 'products' =>Product::all() );
        return view('product.index',$data);
    }
    
    /**
     * Show the form for creating a new resource. *
     * @return \Illuminate\Http\Response
     */
    public function create() {
        $category=array( 'category' =>Category::all() );
        return view('home',$category);
    }
    
    /**
     * Store a newly created resource in storage.
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $req) {
        $product =new Product();
        $product->Product_name=$req->input('Product_name');
        $product->Product_description= $req->input('Product_description');
        $product->Product_image= $req->file('Product_image')->store('Product_image');
        $product->Product_code= $req->input('Product_code');
        $product->Category_id= $req->input('Category_id');
        $product->save();
        return redirect()->route('store.product.index',['products'=>$product->id]);
    }
    
    /**
     * Display the specified resource.
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id) {
        $category=array( 'category' =>Category::all() );
        return view('product.show',$category, ['product' => Product::findOrFail($id)]);
    }
    
    /**
     * Show the form for editing the specified resource.
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id) {
        $category=array( 'category' =>Category::all() );
        return view('product.edit',$category, ['product'=>Product::FindOrFail($id)]);
    }

    /**
     * Update the specified resource in storage.
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request) {
        $product=Product::find($request->id);
        $product->Product_name=$request->input('Product_name');
        $product->Product_description= $request->input('Product_description');
        $product->Product_image= $request->input('Product_image','public');
        $product->Product_code= $request->input('Product_code');
        $product->Category_id= $request->input('Category_id');
        $product->save();
        return redirect()->route('store.product.index', ['product'=> $product->id]);
    }
    
    /**
     * Remove the specified resource from storage.
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id) {
        $product = Product::findOrFail($id);
        $product->delete();
        echo ("User Record deleted successfully.");
        return redirect()->route('product.index');
    }
}

标签: phplaravellaravel-bladelaravel-8

解决方案


推荐阅读