PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, October 19, 2022

[FIXED] How to make button visible once to admin-type user in Laravel?

 October 19, 2022     admin, button, laravel, validation     No comments   

Issue

I want to make ADD button only visible to admin-type user. My code looks like this:

@foreach($users as $user)
  @if(Auth::user()->type=='admin')
    <a href="{{ route('User.create', ['id'=>$user->id ]) }}" class="btn btn-default</i> ADD</a>
  @endif
@endforeach

But, it returns lot of ADD button according to number of all users because of foreach loop. If I remove foreach loop, it will show error:

Undefined variable: user

How can I solve this problem?


Solution

It's because of you are removing the foreach , but using the variable $user again inside the routes . Please remove the $user->id and instead, use Auth::user()->id .

  @if(Auth::user()->type=='admin')
    <a href="{{ route('User.create', ['id'=>Auth::user()->id ]) }}" class="btn btn-default</i> ADD</a>
  @endif


Answered By - farooq
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing