单个视图中的 Laravel CRUD

问题描述 投票:0回答:0

在 Laravel 中,我希望此按钮接收我的对象的 ID,以在我单击它时出现的模态中显示其信息。

我的 AnnonceAdminController

这里是管理我的用户在互联网上的销售广告的控制器。

我想要编辑和删除按钮打开显示有关我仪表板表格中对象信息的模式。

这样我就可以在同一个刀片视图/页面上进行编辑和删除。

<?php

namespace App\Http\Controllers\Admin;

use App\Models\Annonces;
use App\Models\Categories;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Redirect;

class AnnonceAdminController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        //
        $annonces = Annonces::orderBy('updated_at', 'DESC')->paginate(5);

        return view('admin.annonce.index', compact('annonces'));
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
        $annonce = Annonces::All();

        $categories = Categories::orderBy('nom', 'asc')->get();

        return view('admin.annonce.ajouter', compact(
            'annonce',
            'categories'
        ));
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        //
        // dd($request);

        // création d'une instance de class (model Annonces) pour enregistrer en base .
        $newAnnonce = new Annonces;

        $newAnnonce->category_id = $request->category;

        $newAnnonce->user_id =  Auth::user()->id;

        $newAnnonce->nom = $request->nom;
        $newAnnonce->description = $request->description;
        $newAnnonce->prix = $request->prix;

        // Traitement de l'upload de 'image
        if ($request->file()) {
            $fileName = $request->image->store('public/images/annonces');
            $newAnnonce->image = $fileName;
        }
        // Enregistrement des données
        $newAnnonce->save();

        return Redirect::route('admin.annonce.index');
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        // Voir une annonce
        $showAnnonce = Annonces::findOrFail($id);

        // dd($showAnnonce);

        return view('admin.annonce.show', compact(
            'showAnnonce',
        ));
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        //
        $editAnnonce = Annonces::findOrFail($id);

        $categories = Categories::orderBy('nom', 'asc')->get();

        return view('admin.annonce.ajouter', compact(
            'editAnnonce',
            'categories'
        ));
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        //
        $editAnnonce = Annonces::findOrFail($id);

        $editAnnonce->category_id = $request->category;

        $editAnnonce->user_id = Auth::user()->id;

        $editAnnonce->nom = $request->nom;
        $editAnnonce->description = $request->description;
        $editAnnonce->prix = $request->prix;

        // Traitement de l'upload de 'image
        if ($request->file()) {

            if ($editAnnonce->image != '') {
                Storage::delete($editAnnonce->image);
            }

            $fileName = $request->image->store('public/images/annonces');
            $editAnnonce->image = $fileName;
        }

        $editAnnonce->save();
        return Redirect::route('admin.annonce.index');
    }

    /**
     * Remove the specified resource from storage.
     */
    public function delete(string $id)
    {
        //
        $deleteAnnonce = Annonces::findOrFail($id);

        $deleteAnnonce->delete();

        return redirect(route('admin.annonce.index'));
    }
}

我的按钮

<button type="button" id="updateProductButton"
    data-drawer-target="drawer-update-product-default"
    data-drawer-show="drawer-update-product-default"
    aria-controls="drawer-update-product-default" data-drawer-placement="right"
    class="inline-flex items-center px-3 py-2 text-sm font-medium text-center text-white rounded-lg bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800">
    <svg class="w-4 h-4" fill="currentColor" viewBox="0 0 20 20"
        xmlns="http://www.w3.org/2000/svg">
        <path
            d="M17.414 2.586a2 2 0 00-2.828 0L7 10.172V13h2.828l7.586-7.586a2 2 0 000-2.828z">
        </path>
        <path fill-rule="evenodd"
            d="M2 6a2 2 0 012-2h4a1 1 0 010 2H4v10h10v-4a1 1 0 112 0v4a2 2 0 01-2 2H4a2 2 0 01-2-2V6z"
            clip-rule="evenodd"></path>
    </svg>
</button>

我的表格模态

<!-- Edit Product Drawer -->
<div id="drawer-update-product-default"
    class="fixed top-0 right-0 z-40 w-full h-screen max-w-xs p-4 overflow-y-auto transition-transform translate-x-full bg-white dark:bg-gray-800"
    tabindex="-1" aria-labelledby="drawer-label" aria-hidden="true">
    <h5 id="drawer-label"
        class="inline-flex items-center mb-6 text-sm font-semibold text-gray-500 uppercase dark:text-gray-400">Update
        Product</h5>
    <button type="button" data-drawer-dismiss="drawer-update-product-default"
        aria-controls="drawer-update-product-default"
        class="text-gray-400 bg-transparent hover:bg-gray-200 hover:text-gray-900 rounded-lg text-sm p-1.5 absolute top-2.5 right-2.5 inline-flex items-center dark:hover:bg-gray-600 dark:hover:text-white">
        <svg aria-hidden="true" class="w-5 h-5" fill="currentColor" viewBox="0 0 20 20"
            xmlns="http://www.w3.org/2000/svg">
            <path fill-rule="evenodd"
                d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z"
                clip-rule="evenodd"></path>
        </svg>
        <span class="sr-only">Close menu</span>
    </button>
    <form action="#">
        <div class="space-y-4">
            <div>
                <label for="name"
                    class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Name</label>
                <input type="text" name="title" id="name"
                    class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-600 focus:border-blue-600 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
                    value="Education Dashboard" placeholder="Type product name" required="">
            </div>
            <div>
                <label for="category"
                    class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Technology</label>
                <select id="category"
                    class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500">
                    <option selected="">Flowbite</option>
                    <option value="RE">React</option>
                    <option value="AN">Angular</option>
                    <option value="VU">Vue JS</option>
                </select>
            </div>
            <div>
                <label for="price"
                    class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Price</label>
                <input type="number" name="price" id="price"
                    class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-600 focus:border-blue-600 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
                    value="2999" placeholder="$149" required="">
            </div>
            <div>
                <label for="description"
                    class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Description</label>
                <textarea id="description" rows="4"
                    class="block p-2.5 w-full text-sm text-gray-900 bg-gray-50 rounded-lg border border-gray-300 focus:ring-blue-500 focus:border-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
                    placeholder="Enter event description here">Start developing with an open-source library of over 450+ UI components, sections, and pages built with the utility classes from Tailwind CSS and designed in Figma.</textarea>
            </div>
            <div>
                <label for="discount"
                    class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Discount</label>
                <select id="discount"
                    class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500">
                    <option selected="">No</option>
                    <option value="5">5%</option>
                    <option value="10">10%</option>
                    <option value="20">20%</option>
                    <option value="30">30%</option>
                    <option value="40">40%</option>
                    <option value="50">50%</option>
                </select>
            </div>
        </div>
        <div class="bottom-0 left-0 flex justify-center w-full pb-4 mt-4 space-x-4 sm:absolute sm:px-4 sm:mt-0">
            <button type="submit"
                class="w-full justify-center text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800">
                Update
            </button>
            <button type="button"
                class="w-full justify-center text-red-600 inline-flex items-center hover:text-white border border-red-600 hover:bg-red-600 focus:ring-4 focus:outline-none focus:ring-red-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center dark:border-red-500 dark:text-red-500 dark:hover:text-white dark:hover:bg-red-600 dark:focus:ring-red-900">
                <svg aria-hidden="true" class="w-5 h-5 mr-1 -ml-1" fill="currentColor" viewBox="0 0 20 20"
                    xmlns="http://www.w3.org/2000/svg">
                    <path fill-rule="evenodd"
                        d="M9 2a1 1 0 00-.894.553L7.382 4H4a1 1 0 000 2v10a2 2 0 002 2h8a2 2 0 002-2V6a1 1 0 100-2h-3.382l-.724-1.447A1 1 0 0011 2H9zM7 8a1 1 0 012 0v6a1 1 0 11-2 0V8zm5-1a1 1 0 00-1 1v6a1 1 0 102 0V8a1 1 0 00-1-1z"
                        clip-rule="evenodd"></path>
                </svg>
                Delete
            </button>
        </div>
    </form>
</div>
laravel eloquent laravel-blade dashboard
© www.soinside.com 2019 - 2024. All rights reserved.