Debug School

rakesh kumar
rakesh kumar

Posted on

How to create Role-Specific conditional action using button in Laravel

Today ,i am getting how to display button based on some condition
where i am extracting word from current url based on word i am creating button
step1: extracting word from current url in laravel controller function

 $currentURL = url()->current();
        $dataAfterLastSlash = Str::after($currentURL, 'https://www.wizbrand.com/');  
        log::info($dataAfterLastSlash); 
        $desiredSegment = Str::before($dataAfterLastSlash, '/');
        log::info($desiredSegment);
        return view('admin.publisher', compact('a_user_api_bearer_token','desiredSegment', 'total_order', 'pending_order', 'complete_order', 'order_sum', 'currentURL', 'getting_notification', 'notificationCount', 'get_auth_email', 'get_taskboard', 'taskboard_notificationCount', 'get_influencer', 'influencers_notificationcount', 'totalNotificationCount','get_total_value'));
Enter fullscreen mode Exit fullscreen mode
https://www.wizbrand.com/influencer-dashboard/profile/rakesh
log::info($desiredSegment);
influencer-dashboard
==============
https://www.wizbrand.com/publisher/rakesh
log::info($desiredSegment);
publisher
=====================================
https://www.wizbrand.com/orgs/my/dashboard
log::info($desiredSegment);
orgs
Enter fullscreen mode Exit fullscreen mode

step2 In head.blade file create conditional button based on desiredSegment

     <div class="mdc-top-app-bar__section mdc-top-app-bar__section--align-end mdc-top-app-bar__section-left">      
      <div class="menu-button-container menu-profile d-none d-md-block">
      <div class="btn-group" role="group" aria-label="Button group with space">
      @if(isset($desiredSegment) && $desiredSegment === 'organization')
      <div class="btn-group mr-2" role="group" aria-label="Button group with space">
    <button type="button" name="seo_button" id="seo_button" class="btn btn-success" disabled>You are  SEO</button>
</div>
@else
<a href="{{ route('orgspages') }}"><div class="btn-group mr-2" role="group" aria-label="Button group with space">
<button type="button" name="seo_button" id="seo_button" class="btn btn-primary btn-info" >Become Seo</button>
</div></a>
@endif
@if(isset($desiredSegment) && $desiredSegment === 'influencer-dashboard')
<div class="btn-group mr-2" role="group" aria-label="Button group with space">
    <button type="button" name="seo_button" id="seo_button" class="btn btn-success" disabled>You are Influencer</button>
</div>
@else
<a href="{{ url('influencer-dashboard/' . Str::slug(Auth::user()->name)) }}"><div class="btn-group mr-2" role="group" aria-label="Button group with space">
<button type="button" name="influencer_button" id="influencer_button" class="btn btn-primary btn-info">Become Influencer</button>
</div></a>
@endif

@if(isset($desiredSegment) && $desiredSegment === 'publisher')
<div class="btn-group mr-2" role="group" aria-label="Button group with space">
    <button type="button" name="seo_button" id="seo_button" class="btn btn-success" disabled>You are publisher</button>
</div>
@else
<a href="{{ url('publisher/' . Str::slug(Auth::user()->name)) }}"><div class="btn-group" role="group" aria-label="Button group with space">
<button type="button" name="publisher_button" id="publisher_button" class="btn btn-primary btn-info">Become Publisher</button>
</div></a>
@endif
</div>
</div></div>
Enter fullscreen mode Exit fullscreen mode

above code This code is essentially creating a set of buttons or links based on the value of $desiredSegment. If $desiredSegment matches a certain role, it displays a disabled button indicating the user's current role; otherwise, it provides an option to become an SEO, Influencer, or Publisher, depending on the value of $desiredSegment.

OUTPUT

Image description

Image description

Top comments (0)