Debug School

rakesh kumar
rakesh kumar

Posted on • Updated on

How php directive is used in laravel

How to use count in php directive

In controller use the variable

 $profiles=Addprofile::where('user_id',$id)->first();
Enter fullscreen mode Exit fullscreen mode

Image description

In blade file

      <div class="mdc-layout-grid__cell stretch-card mdc-layout-grid__cell--span-3-desktop mdc-layout-grid__cell--span-4-tablet">
              <div class="mdc-card info-card info-card--info">
                <div class="card-inner">
                  <h5 class="card-title">Profile Completeness</h5>
                  <h5 class="font-weight-light pb-2 mb-1 border-bottom"></h5>
                  <p class="tx-12 text-muted">
                    <span class="text-danger display-4">
                    @php 
                $parts = explode(':', $profiles->profile_status);   
                $totalWords = count($parts);
                $completionPercentage = $totalWords * 20; 
            @endphp

            {{{ $completionPercentage }}}%                         

                  </p>
                  <div class="card-icon-wrapper">

                    <i class="material-icons">dvr</i>
                  </div>
                </div>
              </div>
            </div>
Enter fullscreen mode Exit fullscreen mode

output

Image description

How to use custom index in blade file

<div class="col-md-12 col-lg-12" id="name_form">
    <div class="container ml-5">
      <div class="card h-100">
        <div class="card-body">
          <table class="table">
            <thead>
              <tr>
                <th>Order Id</th>
                <th>Influencer Names</th>                
                <th>Payment Id</th>
                <th>Cart Id</th>
                <th>Amount</th>
                <th>Order Status</th>
                <th>Order Date</th>
                <th>Action</th>
              </tr>
            </thead>
            <tbody>
                  @php
         $customIndex = 1; // Initialize the custom index variable
                @endphp
              @foreach($users as $user)
              @foreach(json_decode($user->influencer_name) as $influencer)
              @foreach ($user->otherTableData as $data) 
              <tr>
                <td>{{$customIndex}}</td>
                <td>              
                {{ $influencer }}            
                </td>                
                <td>{{$user->payment_id}}</td>
                <td>{{ $data->id}}</td>

                <td>{{$user->amount}} </td>
                <td>{{$user->payment_status}}</td>
                <td>{{ \Illuminate\Support\Str::limit($user->created_at, 10) }}</td>
                <td>
                  <button type="button" class="btn btn-sm btn-secondary view-button" data-id="{{$user->id}}">
                    Share Data
                  </button>
                </td>
              </tr>
              @php
        $customIndex++; // Increment the custom index
           @endphp
           @endforeach
           @endforeach
              @endforeach

            </tbody>
          </table>
        </div>
      </div>
    </div>
  </div>
Enter fullscreen mode Exit fullscreen mode

output

Image description

how to apply condition on button using php directive

In phpmyadmin

Image description

in blade file

    @php
                        $parts = explode(':', $item->addcarts);
                        $valueAfterColon = isset($parts[1]) ? $parts[1] : null;
                          @endphp
                          @if (($parts[1] == 'yes') && (auth()->user()->email == $parts[0]))  

                            <button type="submit"  
                             class="btn btn-sm  btn-primary" style="position: relative;float:right">Added</button> 
                            @else
                            <button type="submit"  
                             class="btn btn-sm  btn-primary addToCartBtn" data-item-id="{{ $item->id }}"  style="position: relative;float:right">Add To Cart</button> 
                            @endif 
Enter fullscreen mode Exit fullscreen mode

output

Image description

If initially no data is there in $item->addcarts

Image description

       @php
   $valueAfterColon = isset($item->addcarts) ? explode(':', $item->addcarts)[1] ?? null : null;
                        @endphp
                       @if ($valueAfterColon == 'yes' && auth()->user()->email == $parts[0])
                           <button type="submit"  
                            class="btn btn-sm  btn-primary" style="position: relative;float:right">Added</button> 
                           @else
                           <button id="callmyAjaxButton" class="btn btn-sm  btn-primary mr-2"   data-id="{{ $item->id }}"   style="position: relative;float:right" onclick="mySocialClick(this, event)">Add cartss</button>                                                 
                           @endif     
                           <button id="callAjaxButton" class="btn btn-sm  btn-primary mr-2"   data-id="{{ $item->id }}"   style="position: relative;float:right" onclick="myButtonClick(this, event)">View Pricess</button>          

Enter fullscreen mode Exit fullscreen mode

Summary

How to use count in php directive
split the string based on : **
**count the words

then multiply by 20

Top comments (0)