how to add onClick when conditional rendering for profile dropdown - react-hooks

I am trying to add the conditional rendering for user dropdown, so once the user is signed in, and when they click the account icon, the page will show the dropdown menu of that user. i am using useSelector to track the user state, right now, the dropdown menu is shown under the icon once the user sign in.... i want to add the onclick functionality on that, so they need to click the account icon, and then the menu will pop up, anyone know how to add that?
export default function Example() {
const user = useSelector((state) => state.user);
return (
<div className="bg-white">
<div className="ml-auto flex items-center">
<div className="hidden relative lg:flex lg:flex-1 lg:items-center lg:justify-end lg:space-x-6">
<UserCircleIcon
className="h-6 w-6 flex-shrink-0 text-gray-400 group-hover:text-gray-500"
aria-hidden="true"
/>
{user && (
<div className="w-24 z-10 rounded-md bg-white py-1 shadow-lg flex flex-col absolute mt-36 right-0 ring-1 ring-black ring-opacity-5 focus:outline-none">
<p className="bg-gray-50 flex items-center gap-3 cursor-pointer hover:bg-slate-100 trainstion-all duration-100 ease-in-out px-4 py-2 text-sm text-gray-700">
Profile
</p>
<p className="bg-gray-50 flex items-center gap-3 cursor-pointer hover:bg-slate-100 trainstion-all duration-100 ease-in-out px-4 py-2 text-sm text-gray-700">
Settings
</p>
<p className="bg-gray-50 flex items-center gap-3 cursor-pointer hover:bg-slate-100 trainstion-all duration-100 ease-in-out px-4 py-2 text-sm text-gray-700">
Signout
</p>
</div>
)}
</div>

If I understand your question correctly, what you need an additional local state to keep track of whether the user clicked on the "user" icon. You will need to pass that extra condition to your conditional rendering write a function to change the state to true once the user click on the user icon.
export default function Example() {
const user = useSelector((state) => state.user);
const [isOpen, setIsOpen] = useState(false); <--- new local state/condition ---<<
const handleOpenUserMenu = () => setIsOpen(true); <--- set function ---<<
return (
<div className="bg-white">
<div className="ml-auto flex items-center">
<div className="hidden relative lg:flex lg:flex-1 lg:items-center lg:justify-end lg:space-x-6">
<UserCircleIcon
className="h-6 w-6 flex-shrink-0 text-gray-400 group-hover:text-gray-500"
aria-hidden="true"
onClick={handleOpenUserMenu} <--- pass the set function here ---<<
/>
{user && isOpen && ( <--- add the extra condition here ---<<
<div className="w-24 z-10 rounded-md bg-white py-1 shadow-lg flex flex-col absolute mt-36 right-0 ring-1 ring-black ring-opacity-5 focus:outline-none">
<p className="bg-gray-50 flex items-center gap-3 cursor-pointer hover:bg-slate-100 trainstion-all duration-100 ease-in-out px-4 py-2 text-sm text-gray-700">
Profile
</p>
<p className="bg-gray-50 flex items-center gap-3 cursor-pointer hover:bg-slate-100 trainstion-all duration-100 ease-in-out px-4 py-2 text-sm text-gray-700">
Settings
</p>
<p className="bg-gray-50 flex items-center gap-3 cursor-pointer hover:bg-slate-100 trainstion-all duration-100 ease-in-out px-4 py-2 text-sm text-gray-700">
Signout
</p>
</div>
)}
</div>
</div>
</div>
);
}

Related

How to pass data between components Laravel

Hello I'm confused on how should I pass collections/data to another component so I can display it in another component (in this case I'm showing discounts so it will be a section with popular discounts and another section showing latest discounts).
So i have this index.blade.php and i'm a bit confused on how to pass the data to x-discount-card component so I can access to $discount->title on the component. Right now I get "undefined variable $discount" so it seems i'm not accessing it right.
<x-layout>
<main>
#include('components.categories-row')
#include('discounts._best')
<section class="w-4/6 mx-auto my-8 font-bold flex flex-col items-center justify-center">
<h1 class="uppercase text-main-gray text-4xl bold self-start text-center mb-4 ">Últimos Descuentos</h1>
#if($discounts->count())
#foreach($discounts as $discount)
<x-discount-card :discount="$discount" />
#endforeach
#else
<p class="my-8 text-center">No hay descuentos disponibles. Por favor vuelva mas tarde. </p>
#endif
</section>
</main>
</x-layout>
discount-component
#props(['discount'])
<div
class="w-[20rem] h-auto bg-gradient-to-b from-card-light-gray to-card-main-gray p-6 rounded-2xl drop-shadow-xl md:w-[35rem] lg:w-[35rem] lg:w-[50rem] lg:h-auto mb-4">
<div class="flex-col items-center text-center justify-center h-full lg:grid lg:grid-cols-4 lg:grid-rows-4">
<a href="" class="flex justify-center lg:row-span-4 lg:-ml-10">
<img class="h-full " src="/images/pngwing.com.png" alt="" width="120" height="120">
</a>
<div
class="flex-col justify-center items-center text-center w-full lg:col-start-2 lg:col-span-full lg:row-start-1 lg:row-span-full lg:-ml-14">
<a href="/discount/1" class="lg:grid lg:row-start-2">
<h2 class="text-white font-bold text-lg md:text-2xl lg:my-2">{{ $discount->title }}</h2>
<div class="flex my-4 items-center text-center justify-center lg:my-2">
<p class="text-price-color font-bold mr-8 text-xl md:text-2xl">139,99€</p>
<p class="line-through text-white text-lg md:text-lg">149,99 (-10€)</p>
</div>
<p class="text-white my-8 text-sm md:text-lg lg:my-4">Bambas nike con 10€ de descuento y envio gratis.
Salen a
139,99€</p>
</a>
<div
class="flex-col items-center justify-center gap-2 lg:flex-row lg:inline-flex lg:col-start-2 lg:col-span-full lg:row-start-4 lg:row-end-5">
<a class="flex items-center justify-center my-2 " href="/user/1">
<img class="rounded-full mr-2" src="https://i.pravatar.cc/35"
alt="">
<p class="mr-4 text-white text-sm font-bold transition-transform hover:translate-x-0.5 md:text-lg">
JohnDoe</p>
</a>
<div class="flex items-center justify-center my-4 gap-4 lg:mx-4">
<div
class="border border-light-gray p-1 rounded-md transition-transform hover:-translate-y-0.5 cursor-pointer">
<img class="h-full" src="/images/heart.png" alt="" width="22" height="22">
</div>
<div
class="border border-light-gray p-1 rounded-md transition-transform hover:-translate-y-0.5 cursor-pointer">
<img class="h-full" src="/images/dislike.png" alt="" width="22" height="22">
</div>
<div
class="flex items-center border border-light-gray p-1 rounded-md transition-transform hover:-translate-y-0.5 cursor-pointer lg:px-4">
<img class="h-full" src="/images/comment.png" alt="" width="22" height="22">
<p class="text-light-gray ml-2">10</p>
</div>
</div>
<div class="flex justify-center lg:w-[250px] lg:-mr-14">
<button
class="flex my-4 items-center justify-center bg-gradient-to-b from-button-light-orange to-button-dark-orange p-2 rounded-md transition-transform hover:-translate-y-0.5 md:p-4 lg:w-full ">
<p class="text-light-gray mr-4 font-bold text-sm md:text-lg ">Ver Chollo</p>
<img src="/images/external-link.png" alt="" width="20" height="20">
</button>
</div>
</div>
</div>
</div>
<div
class="w-auto absolute top-5 left-5 flex-col items-center justify-center rounded-2xl bg-button-light-orange p-2 drop-shadow-lg md:flex-row md:inline-flex">
<p class="text-white text-xs uppercase font-bold mr-2 md:text-sm ">10%</p>
<p class="text-white text-xs uppercase font-bold md:text-sm ">descuento</p>
</div>
</div>
Also on the routes file i'm passing discounts but i'm a bit confused on how should I pass the data because right now i'm getting "undefined variable $discount error"
Route::get('/', function () {
return view('discounts.index', [
'discounts' => Discount::all(),
// ->where('premium','=',false)
'categories' => Category::all()
]);
})->name('home');
On the discounts._best component i'll display the discounts with more likes but this will be done in the future but I share the component too because maybe something there is what is causing the error.
<section class=" w-full text-center h-3/4 bg-light-gray flex flex-col items-center justify-center p-10">
<h1 class="uppercase text-main-gray mb-8 text-4xl font-bold">Descuentos Populares</h1>
<x-discount-card />
</section>
I searched on the Laravel Documentation about Component attributes but I dont know what i'm doing wrong and still confused.

nest and previous pagination inside div

So, I have created a Laravel controller with pagination. The code inside my Vue file is the following
<div class="flex justify-between flex-wrap">
<div v-for="kink in kinks.data" :key="kink.id" :value="kink.name">
<button type="button" class="m-1 my-2 inline-flex items-center px-3.5 py-2 border border-transparent text-sm leading-4 font-medium rounded-full shadow-sm text-white bg-gray-600 hover:bg-gray-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-gray-500"> {{ kink.name }}</button>
</div>
</div>
<div class="flex justify-between">
<ArrowLeftIcon type="button" :href="kinks.next_page_url" class="m-2 inline-flex items-center px-2 py-1 h-7 rounded-full border border-transparent text-sm leading-4 font-medium shadow-sm text-white bg-red-600 hover:bg-red-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-red-500 "></ArrowLeftIcon>
<ArrowRightIcon type="button" :href="kinks.last_page_url" class="m-2 inline-flex items-center px-2 py-1 h-7 rounded-full border border-transparent text-sm leading-4 font-medium shadow-sm text-white bg-red-600 hover:bg-red-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-red-500"></ArrowRightIcon>
</div>
The arrow buttons should change the v-for data based on the active page. I have NEVER done this before and I do not want to use some cheap npm package when I just need next and previous.
IDEALLY, if someone is on page 1, and they click previous, it should go to the last page and the same with last page next should go to page 1
How do you implement this?
The data object inside vue from the pagination provides the following:
kinks:Object
current_page:1
data:Array[13]
first_page_url:"http://localhost:8000/kinks?page=1"
from:1
last_page:5
last_page_url:"http://localhost:8000/kinks?page=5"
links:Array[7]
next_page_url:"http://localhost:8000/kinks?page=2"
path:"http://localhost:8000/kinks"
per_page:13
prev_page_url:null
to:13
total:63
ok so you will want to change the way your buttons work then. e.g. add a #click method to them instead of a href
example
<button #click="previousLink">
...icon
</button>
<script>
export default {
methods: {
previousLink() {
if (this.prev_page_url === null) {
window.location.href = this.last_page_url;
} else {
window.location.href = this.prev_page_url;
}
}
}
}
</script>

How to create a bar near to text in Tailwind CSS?

I am a beginner to Tailwind CSS. And I am finding it difficult to create a button design as shown in the picture in tailwind Css
So I want to display that little bar on the right when the individual section like eg. Home,Trends,Feed are pressed. So can you please help me out with it. I want the answer in tailwind css.
Here is the code:
<div className="flex">
<div className="w-72 bg-white text-gray-100 shadow-lg">
<div class="flex h-full flex-col p-7 pl-8 text-sm">
<div class="relative flex w-full items-center justify-between">
<a class="flex space-x-5 py-5 text-slate-800">
<MusicNoteIcon class="h-5 w-5"></MusicNoteIcon>
<p class="font-semibold">Sovereignty Kingdom</p>
</a>
<button class="delay-50 absolute right-[3px] h-10 cursor-pointer rounded-full p-2 text-slate-800 transition duration-200 ease-in-out hover:scale-110 hover:bg-black hover:text-white">
<SwitchHorizontalIcon class="h-5 w-5"></SwitchHorizontalIcon>
</button>
</div>
<nav className="delay-50 my-2 flex cursor-pointer items-center space-x-3 rounded-lg p-3 text-slate-800 transition duration-200 ease-in-out hover:-translate-y-1 hover:scale-110 hover:bg-black hover:text-white">
<HomeIcon className="h-5 w-5" />
<p>Home</p>
</nav>
<nav className="delay-50 my-2 flex cursor-pointer items-center space-x-3 rounded-lg p-3 text-slate-800 transition duration-200 ease-in-out hover:-translate-y-1 hover:scale-110 hover:bg-black hover:text-white">
<TrendingUpIcon className="h-5 w-5" />
<p>Trends</p>
</nav>
<nav className="delay-50 duration-2 cursor-pointer00 my-2 flex cursor-pointer items-center space-x-3 rounded-lg p-3 text-slate-800 transition ease-in-out hover:-translate-y-1 hover:scale-110 hover:bg-black hover:text-white">
<RssIcon className="h-5 w-5" />
<p>Feed</p>
</nav></div>
</div>
</div>```
I'm not sure if this is the only solution, but this worked for me:
The first thing I did was define an array for the buttons group :
const elements=[
{name:'Home',ico:faHome}, //I'm using font awesome Icons,you can use yours
{name:'Trend',ico:faFireAlt},
{name:'Feed',ico:faFeed}
]
then I defined a state so that I know which button was clicked like
//(-1) is the button id, default (-1) so that the bar won't render by default
const [clickedId, setClickedId] = useState(-1);
Here is the full code:
<div className='flex m-14 w-72 '>
<div className="w-full bg-white text-gray-100 shadow-lg">
<div className="flex h-full flex-col p-7 pl-8 text-sm">
<div className="relative flex w-full items-center justify-between">
<a className="flex space-x-5 py-5 text-slate-800">
<div className="h-5 w-5 bg-teal-800 rounded-full"></div>
<p className="font-semibold">Sovereignty Kingdom</p>
</a>
<button className="delay-50 absolute right-[3px] h-10 cursor-pointer rounded-full p-2 text-slate-800 transition duration-200 ease-in-out hover:scale-110 hover:bg-black hover:text-white">
<div className="h-5 w-5 bg-teal-800 rounded-full"></div>
</button>
</div>
{/* map over the buttons array to render them */}
{elements.map((el,id)=>(
{/* give every button a key */}
<button key={id} name={el.name}
{/* when a button is clicked assign clickedId to the current button id */}
onClick={() => setClickedId(id)}
{/* based on the button id append classes to make the sidebar */}
className={`relative delay-50 my-2 flex cursor-pointer items-center space-x-3 rounded-lg p-3
text-slate-800 transition duration-75 ease-in-out hover:-translate-y-1
hover:scale-110 hover:bg-black hover:text-white
${(id === clickedId) ?`after:w-1 after:h-12 after:bg-black after:rounded-full after:absolute after:-right-7
hover:after:transition hover:after:duration-75 hover:after:-right-4 hover:after:ease-in-out`:'' }`}>
<FontAwesomeIcon className='h-5 w-5 ' icon={el.ico}/>
<p>{el.name}</p>
</button>
))}
</div>
</div>
</div>
or check it Live on codesandbox.

Laravel Blade For each loop alternate display

I want to present my blade view in a nice way.
First id, it will show the image on the left, and the content on the right.
Second id, it will show the image on the right, and the content on the left. But I'm not sure how to loop this., 3rd id it will again show image on the left, and content on the right, and so on and so forth.
Anybody can help? Thanks
#foreach ($users as $user)
<div class="box-border flex flex-col items-center content-center px-8 mx-auto leading-6 text-black border-0 border-gray-300 border-solid md:flex-row max-w-7xl lg:px-16">
<!-- Image on the left-->
<div class="box-border relative w-full max-w-md px-4 mt-5 mb-4 -ml-5 text-center bg-no-repeat bg-contain border-solid md:ml-0 md:mt-0 md:max-w-none lg:mb-0 md:w-1/2 xl:pl-10">
<img src="{{$user->image}}" class="p-2 pl-6 pr-5 xl:pl-16 xl:pr-20 ">
</div>
<!-- Content on the right-->
<div class="box-border order-first w-full text-black border-solid md:w-1/2 md:pl-10 md:order-none">
<h2 class="m-0 text-xl font-semibold leading-tight border-0 border-gray-300 lg:text-3xl md:text-2xl">
{{$user->title}}
</h2>
<p class="pt-4 pb-8 m-0 leading-7 text-gray-700 border-0 border-gray-300 sm:pr-12 xl:pr-32 lg:text-lg">
{{$user->body}}
</p>
<ul class="p-0 m-0 leading-6 border-0 border-gray-300">
<li class="box-border relative py-1 pl-0 text-left text-gray-500 border-solid">
<span class="inline-flex items-center justify-center w-6 h-6 mr-2 text-white bg-yellow-300 rounded-full"><span class="text-sm font-bold">✓</span></span> {{$user->sub_1}}
</li>
<li class="box-border relative py-1 pl-0 text-left text-gray-500 border-solid">
<span class="inline-flex items-center justify-center w-6 h-6 mr-2 text-white bg-yellow-300 rounded-full"><span class="text-sm font-bold">✓</span></span> {{$user->sub_2}}
</li>
<li class="box-border relative py-1 pl-0 text-left text-gray-500 border-solid">
<span class="inline-flex items-center justify-center w-6 h-6 mr-2 text-white bg-yellow-300 rounded-full"><span class="text-sm font-bold">✓</span></span> {{$user->sub_2}}
</li>
</ul>
</div>
<!-- End Content -->
</div>
<div class="box-border flex flex-col items-center content-center px-8 mx-auto mt-2 leading-6 text-black border-0 border-gray-300 border-solid md:mt-20 xl:mt-0 md:flex-row max-w-7xl lg:px-16">
<!-- Content on left-->
<div class="box-border w-full text-black border-solid md:w-1/2 md:pl-6 xl:pl-32">
<h2 class="m-0 text-xl font-semibold leading-tight border-0 border-gray-300 lg:text-3xl md:text-2xl">
{{$user->title}}
</h2>
<p class="pt-4 pb-8 m-0 leading-7 text-gray-700 border-0 border-gray-300 sm:pr-10 lg:text-lg">
{{$user->body}}
</p>
<ul class="p-0 m-0 leading-6 border-0 border-gray-300">
<li class="box-border relative py-1 pl-0 text-left text-gray-500 border-solid">
<span class="inline-flex items-center justify-center w-6 h-6 mr-2 text-white bg-yellow-300 rounded-full"><span class="text-sm font-bold">✓</span></span> {{$user->sub_1}}
</li>
<li class="box-border relative py-1 pl-0 text-left text-gray-500 border-solid">
<span class="inline-flex items-center justify-center w-6 h-6 mr-2 text-white bg-yellow-300 rounded-full"><span class="text-sm font-bold">✓</span></span> {{$user->sub_2}}
</li>
<li class="box-border relative py-1 pl-0 text-left text-gray-500 border-solid">
<span class="inline-flex items-center justify-center w-6 h-6 mr-2 text-white bg-yellow-300 rounded-full"><span class="text-sm font-bold">✓</span></span> {{$user->sub_2}}
</li>
</ul>
</div>
<!-- End Content -->
<!-- Image on the right-->
<div class="box-border relative w-full max-w-md px-4 mt-10 mb-4 text-center bg-no-repeat bg-contain border-solid md:mt-0 md:max-w-none lg:mb-0 md:w-1/2">
<img src="{{$user->image}}" class="pl-4 sm:pr-10 xl:pl-10 lg:pr-32">
</div>
</div>
#endforeach
Inside of a blade loop you have access to the $loop variable which you can use to check if the current index is odd or even.
#foreach ($users as $user)
#if($loop->even)
{{-- The index is event --}}
#else
{{-- The index is odd --}}
#endif
#endforeach
or
#foreach ($users as $user)
#if($loop->odd)
{{-- The index is odd --}}
#else
{{-- The index is even --}}
#endif
#endforeach
It's easier than you think. You just have to use a variable that you may only access inside a #foreach that is $loop. This variable comes with many helpful properties you can read on here but the ones you need are $loop->even and $loop->odd. Coupled with the ternary operator to make a condition inside the class and tailwind's ability to reorder items, this should be easily manageable without having to duplicate any code.
#foreach($users as $user)
<div class="{{$loop->odd ? "order-first" : "order-last"}}">image</div>
<div class="{{$loop->even ? "order-first" : "order-last"}}">content</div>
#endforeach()
You can use condition in loop.
Like:
if($key % 2 == 0){
// show right image and left content part
} else {
// show left image and right content part
}
Updated file:
#foreach ($users as $key => $user)
#if($key % 2 == 0)
<div class="box-border flex flex-col items-center content-center px-8 mx-auto mt-2 leading-6 text-black border-0 border-gray-300 border-solid md:mt-20 xl:mt-0 md:flex-row max-w-7xl lg:px-16">
<!-- Content on left-->
<div class="box-border w-full text-black border-solid md:w-1/2 md:pl-6 xl:pl-32">
<h2 class="m-0 text-xl font-semibold leading-tight border-0 border-gray-300 lg:text-3xl md:text-2xl">
{{$user->title}}
</h2>
<p class="pt-4 pb-8 m-0 leading-7 text-gray-700 border-0 border-gray-300 sm:pr-10 lg:text-lg">
{{$user->body}}
</p>
<ul class="p-0 m-0 leading-6 border-0 border-gray-300">
<li class="box-border relative py-1 pl-0 text-left text-gray-500 border-solid">
<span class="inline-flex items-center justify-center w-6 h-6 mr-2 text-white bg-yellow-300 rounded-full"><span class="text-sm font-bold">✓</span></span> {{$user->sub_1}}
</li>
<li class="box-border relative py-1 pl-0 text-left text-gray-500 border-solid">
<span class="inline-flex items-center justify-center w-6 h-6 mr-2 text-white bg-yellow-300 rounded-full"><span class="text-sm font-bold">✓</span></span> {{$user->sub_2}}
</li>
<li class="box-border relative py-1 pl-0 text-left text-gray-500 border-solid">
<span class="inline-flex items-center justify-center w-6 h-6 mr-2 text-white bg-yellow-300 rounded-full"><span class="text-sm font-bold">✓</span></span> {{$user->sub_2}}
</li>
</ul>
</div>
<!-- End Content -->
<!-- Image on the right-->
<div class="box-border relative w-full max-w-md px-4 mt-10 mb-4 text-center bg-no-repeat bg-contain border-solid md:mt-0 md:max-w-none lg:mb-0 md:w-1/2">
<img src="{{$user->image}}" class="pl-4 sm:pr-10 xl:pl-10 lg:pr-32">
</div>
</div>
#else
<div class="box-border flex flex-col items-center content-center px-8 mx-auto leading-6 text-black border-0 border-gray-300 border-solid md:flex-row max-w-7xl lg:px-16">
<!-- Image on the left-->
<div class="box-border relative w-full max-w-md px-4 mt-5 mb-4 -ml-5 text-center bg-no-repeat bg-contain border-solid md:ml-0 md:mt-0 md:max-w-none lg:mb-0 md:w-1/2 xl:pl-10">
<img src="{{$user->image}}" class="p-2 pl-6 pr-5 xl:pl-16 xl:pr-20 ">
</div>
<!-- Content on the right-->
<div class="box-border order-first w-full text-black border-solid md:w-1/2 md:pl-10 md:order-none">
<h2 class="m-0 text-xl font-semibold leading-tight border-0 border-gray-300 lg:text-3xl md:text-2xl">
{{$user->title}}
</h2>
<p class="pt-4 pb-8 m-0 leading-7 text-gray-700 border-0 border-gray-300 sm:pr-12 xl:pr-32 lg:text-lg">
{{$user->body}}
</p>
<ul class="p-0 m-0 leading-6 border-0 border-gray-300">
<li class="box-border relative py-1 pl-0 text-left text-gray-500 border-solid">
<span class="inline-flex items-center justify-center w-6 h-6 mr-2 text-white bg-yellow-300 rounded-full"><span class="text-sm font-bold">✓</span></span> {{$user->sub_1}}
</li>
<li class="box-border relative py-1 pl-0 text-left text-gray-500 border-solid">
<span class="inline-flex items-center justify-center w-6 h-6 mr-2 text-white bg-yellow-300 rounded-full"><span class="text-sm font-bold">✓</span></span> {{$user->sub_2}}
</li>
<li class="box-border relative py-1 pl-0 text-left text-gray-500 border-solid">
<span class="inline-flex items-center justify-center w-6 h-6 mr-2 text-white bg-yellow-300 rounded-full"><span class="text-sm font-bold">✓</span></span> {{$user->sub_2}}
</li>
</ul>
</div>
<!-- End Content -->
</div>
#endforeach

Using a modal to delete a user

I'm trying to create a modal that when you click delete it will delete a record, but first I need to be able to have
that modal pop up.
I know that the reason is because of this line $('#delete_{{ $user->id }}').click(function(){ but I'm not sure how to add the user id without having to add the <script>...</script> to the foreach loop
Or do I need to do that?
Here is my code
<table class="min-w-full divide-y divide-gray-200">
<thead>
<tr>
<th class="px-6 py-3 bg-gray-50 text-left text-xs leading-4 font-medium text-gray-500 uppercase tracking-wider">
Name
</th>
<th class="px-6 py-3 bg-gray-50 text-left text-xs leading-4 font-medium text-gray-500 uppercase tracking-wider text-right">
Add User
</th>
</tr>
</thead>
<tbody class="bg-white divide-y divide-gray-200">
#foreach($users as $user)
<tr>
<td class="px-6 py-4 whitespace-no-wrap">
<div class="flex items-center">
<div class="ml-4">
<div class="text-sm leading-5 font-medium text-gray-900">
{{ $user->name }}
</div>
<div class="text-sm leading-5 text-gray-500">
{{ $user->email }}
</div>
</div>
</div>
</td>
<td class="px-6 py-4 whitespace-no-wrap text-right text-sm leading-5 font-medium">
<button id="delete_{{ $user->id }}" class="bg-red-400 hover:bg-red-300 text-red-800 font-bold py-2 px-4 rounded inline-flex items-center">
Delete
</button>
</td>
</tr>
#endforeach
</tbody>
</table>
<div class="modal-display-none">
<div class="fixed z-10 inset-0 overflow-y-auto">
<div class="flex items-end justify-center min-h-screen pt-4 px-4 pb-20 text-center sm:block sm:p-0">
<div class="fixed inset-0 transition-opacity">
<div class="absolute inset-0 bg-gray-500 opacity-75"></div>
</div>
<span class="hidden sm:inline-block sm:align-middle sm:h-screen"></span>​
<div class="inline-block align-bottom bg-white rounded-lg text-left overflow-hidden shadow-xl transform transition-all sm:my-8 sm:align-middle sm:max-w-lg sm:w-full"
role="dialog"
aria-modal="true"
aria-labelledby="modal-headline">
<div class="bg-white px-4 pt-5 pb-4 sm:p-6 sm:pb-4">
<div class="sm:flex sm:items-start">
<div class="mx-auto flex-shrink-0 flex items-center justify-center h-12 w-12 rounded-full bg-red-100 sm:mx-0 sm:h-10 sm:w-10">
<svg class="h-6 w-6 text-red-600" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z" />
</svg>
</div>
<div class="mt-3 text-center sm:mt-0 sm:ml-4 sm:text-left">
<h3 class="text-lg leading-6 font-medium text-gray-900" id="modal-headline">
Delete User
</h3>
<div class="mt-2">
<p class="text-sm leading-5 text-gray-500">
Are you sure you want to delete this user? <br>
This action cannot be undone.
</p>
</div>
</div>
</div>
</div>
<div class="bg-gray-50 px-4 py-3 sm:px-6 sm:flex sm:flex-row-reverse">
<span class="flex w-full rounded-md shadow-sm sm:ml-3 sm:w-auto">
<form action="{{ route('users.delete', ['user' => $user->id]) }}" method="POST">
#method('DELETE')
#csrf
<button type="button"
class="inline-flex justify-center w-full rounded-md border border-transparent px-4 py-2 bg-red-600 text-base leading-6 font-medium text-white shadow-sm hover:bg-red-500 focus:outline-none focus:border-red-700 focus:shadow-outline-red transition ease-in-out duration-150 sm:text-sm sm:leading-5"
id="confirm_delete"
>
Delete
</button>
</form>
</span>
<span class="mt-3 flex w-full rounded-md shadow-sm sm:mt-0 sm:w-auto">
<button type="button" id="close" class="inline-flex justify-center w-full rounded-md border border-gray-300 px-4 py-2 bg-white text-base leading-6 font-medium text-gray-700 shadow-sm hover:text-gray-500 focus:outline-none focus:border-blue-300 focus:shadow-outline-blue transition ease-in-out duration-150 sm:text-sm sm:leading-5">
Cancel
</button>
</span>
</div>
</div>
</div>
</div>
</div>
<style>
.modal-display-none{
display: none;
}
.modal-display{
display: block;
}
</style>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function(){
$('#delete_{{ $user->id }}').click(function(){
console.log('delete button');
$('.modal-display-none').removeClass('modal-display-none').addClass('modal-display');
});
$('#confirm_delete').click(function(){
$('.modal-display').removeClass('modal-display').addClass('modal-display-none');
});
$('#close').click(function(){
$('.modal-display').removeClass('modal-display').addClass('modal-display-none');
});
});
</script>
replace your syntax (inside foreach)
<button id="delete_{{ $user->id }}" class="bg-red-400 hover:bg-red-300 text-red-800 font-bold py-2 px-4 rounded inline-flex items-center">Delete</button>
replace with this syntax bellow, it will show an alert to confirm the Delete action
{!! Form::open(['route' => ['users.delete', $user->id], 'method' => 'delete']) !!}
{!! Form::button('<i class="fa fa-trash"></i>', [
'type' => 'submit',
'class' => 'bg-red-400 hover:bg-red-300 text-red-800 font-bold py-2 px-4 rounded inline-flex items-center',
'onclick' => "return confirm('Are you sure want to delete this item?')"
]) !!}
{!! Form::close() !!}

Resources