johnef_sh

johnef_sh

Member Since 3 Years Ago

Experience Points
7,350
Total
Experience

2,650 experience to go until the next level!

In case you were wondering, you earn Laracasts experience when you:

  • Complete a lesson — 100pts
  • Create a forum thread — 50pts
  • Reply to a thread — 10pts
  • Leave a reply that is liked — 50pts
  • Receive a "Best Reply" award — 500pts
Lessons Completed
47
Lessons
Completed
Best Reply Awards
0
Best Reply
Awards
  • start your engines Created with Sketch.

    Start Your Engines

    Earned once you have completed your first Laracasts lesson.

  • first-thousand Created with Sketch.

    First Thousand

    Earned once you have earned your first 1000 experience points.

  • 1-year Created with Sketch.

    One Year Member

    Earned when you have been with Laracasts for 1 year.

  • 2-years Created with Sketch.

    Two Year Member

    Earned when you have been with Laracasts for 2 years.

  • 3-years Created with Sketch.

    Three Year Member

    Earned when you have been with Laracasts for 3 years.

  • 4-years Created with Sketch.

    Four Year Member

    Earned when you have been with Laracasts for 4 years.

  • 5-years Created with Sketch.

    Five Year Member

    Earned when you have been with Laracasts for 5 years.

  • school-in-session Created with Sketch.

    School In Session

    Earned when at least one Laracasts series has been fully completed.

  • welcome-newcomer Created with Sketch.

    Welcome To The Community

    Earned after your first post on the Laracasts forum.

  • full-time-student Created with Sketch.

    Full Time Learner

    Earned once 100 Laracasts lessons have been completed.

  • pay-it-forward Created with Sketch.

    Pay It Forward

    Earned once you receive your first "Best Reply" award on the Laracasts forum.

  • subscriber Created with Sketch.

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • lifer Created with Sketch.

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • evangelist Created with Sketch.

    Laracasts Evangelist

    Earned if you share a link to Laracasts on social media. Please email [email protected] with your username and post URL to be awarded this badge.

  • chatty-cathy Created with Sketch.

    Chatty Cathy

    Earned once you have achieved 500 forum replies.

  • lara-veteran Created with Sketch.

    Laracasts Veteran

    Earned once your experience points passes 100,000.

  • 10k-strong Created with Sketch.

    Ten Thousand Strong

    Earned once your experience points hits 10,000.

  • lara-master Created with Sketch.

    Laracasts Master

    Earned once 1000 Laracasts lessons have been completed.

  • laracasts-tutor Created with Sketch.

    Laracasts Tutor

    Earned once your "Best Reply" award count is 100 or more.

  • laracasts-sensei Created with Sketch.

    Laracasts Sensei

    Earned once your experience points passes 1 million.

  • top-50 Created with Sketch.

    Top 50

    Earned once your experience points ranks in the top 50 of all Laracasts users.

Level 2
7,350 XP
Mar
05
2 months ago
Activity icon

Replied to Can't Change App Default Language

@khakan Thanks dear, this solved my issue

Feb
27
3 months ago
Activity icon

Awarded Best Reply on Foreach Inside Foreach And Out Put First Foreach In Array

Good morning dear all, I have got another solution that's more less complicated.

I added my array above, and then I was able to get it in my main array.. something like this

$packages = [];
foreach ( $products as $product ) {

     foreach ( $product->packageId as $package ) {
       $packages[] = [
            'package_id'    => $package['id'],
       ];
     }  

     $data[] = [
          'id'                      => $product->id,
          'packages '        => $packages,
     ]
}

it's working fine thought I will share it with y'all it can helps someone.

Regards

Activity icon

Replied to Foreach Inside Foreach And Out Put First Foreach In Array

I solved it thanks all for the efforts

Feb
26
3 months ago
Activity icon

Replied to Foreach Inside Foreach And Out Put First Foreach In Array

I updated my code it has pagination check my updated question please.

Activity icon

Replied to Foreach Inside Foreach And Out Put First Foreach In Array

Even here I have one issue last in this return JSON I can't return the paginator any idea how can I send the paginator to my array.

Activity icon

Replied to Foreach Inside Foreach And Out Put First Foreach In Array

Truly I didn't try the last update, thought I found this one is less complicated.

Activity icon

Replied to Foreach Inside Foreach And Out Put First Foreach In Array

Good morning dear all, I have got another solution that's more less complicated.

I added my array above, and then I was able to get it in my main array.. something like this

$packages = [];
foreach ( $products as $product ) {

     foreach ( $product->packageId as $package ) {
       $packages[] = [
            'package_id'    => $package['id'],
       ];
     }  

     $data[] = [
          'id'                      => $product->id,
          'packages '        => $packages,
     ]
}

it's working fine thought I will share it with y'all it can helps someone.

Regards

Feb
24
3 months ago
Activity icon

Replied to Foreach Inside Foreach And Out Put First Foreach In Array

@sinnbeck thanks, but still not as I expected this will overwrite the packages array and it will only output only the latest package in the loop. any other ideas

Activity icon

Replied to Foreach Inside Foreach And Out Put First Foreach In Array

@sinnbeck thanks for replay but what is $key can you please describe more

Activity icon

Started a new Conversation Foreach Inside Foreach And Out Put First Foreach In Array

assume I have an array called (data) and inside my array I have a foreach on products I need to get each of this product packages inside this (data) array.

here is what I try

foreach ( $products as $product ) {
     $data[] = [
          'id'                      => $product->id,
          'packages '        => [],
     ]

  foreach ( $product->packageId as $package ) {
       $data[]['packages'] = [
            'package_id'    => $package['id'],
       ];
  }
}

this returns

- 0 [   
            id  : 977
            packages :  []
]

- 1 [   
     packages   
          package_id    14
]

- 2 [
    packages    
        package_id  15
]

I need to return something like

- 0 [   
            id  : 977
            packages :  [
                 package_id 14,
                 package_id 15
            ]
]

Update to show my all code

Here is the code comes just before the foreach

$query = Product::where( 'product_category_id', '!=', null )
                        ->where( 'soft_delete', 0 )
                        ->whereBetween( 'low_price', [ $priceFrom, $priceTo ] )
                        ->with( [
                            'campaign' => function ( $q ) {
                                $q->select( [ 'id', 'product_id', 'price', 'purchasesLimits', 'stock' ] );
                                $q->where( 'stock', '>', 0 );
                                $q->where( 'expire', '>', Carbon::today()->toDateString() );
                            }
                        ] )
                        ->with( [
                            'supplier' => function ( $q ) {
                                $q->select( [ 'id', 'contact_name' ] );
                            }
                        ] )
                        ->with( [
                            'packageId' => function ( $query ) {
                                $query->with( [
                                    'pckInventories' => function ( $query ) {
                                        $query->where( 'expiry_date', '<', Carbon::today() );
                                        $query->where( 'type', 'existing' );
                                    }
                                ] );
                            }
                        ] );
        if ( $sort == 'desc' ) {
            $products = $query
                ->orderBy( 'low_price', 'desc' )
                ->paginate( 16 );
        } elseif ( $sort == 'asc' ) {
            $products = $query
                ->orderBy( 'low_price', 'asc' )
                ->paginate( 16 );
        } elseif ( $sort == 'popularity' ) {
            $products = $query
                ->orderBy( 'view_number', 'desc' )
                ->paginate( 16 );
        } elseif ( $sort == 'new_arrival' ) {
            $products = $query
                ->orderBy( 'created_at', 'desc' )
                ->paginate( 16 );
        } else {
            $products = $query
                ->paginate( 16 );
        }
Feb
16
3 months ago
Activity icon

Replied to Laravel Eloquent GroupBy() AND Also Return Count Of Each Group Issue

My bad the relations was on the package_id and I forgot to add this to the ->selectRaw( 'count(*) as total, product_id' ) as it should be something like this

->selectRaw( 'count(*) as total, product_id', 'package_id' ) and it works.
Activity icon

Started a new Conversation Laravel Eloquent GroupBy() AND Also Return Count Of Each Group Issue

I am trying to git the count of each group

$packageInventory = PackageInventory::whereStoreId( $packageInventory )
                              ->selectRaw( 'count(*) as total, product_id' )
                              ->groupBy( 'product_id' )
                              ->get();

This working fine but the issue here when try to get related models it dose not work

in my blade I doing something like this

@foreach($packageInventory as $pInvent)
    <tr>
       <td>{{ $pInvent->pckInventory->productId->name }}</td>
       <td>{!! $pInvent->total !!}</td>
       <td class="text-center"><a class="btn btn-info" href="#"></a>
       </td>
    </tr>
 @endforeach

if I try to add this count as total ->selectRaw( 'count(*) as total, product_id' ) the relations here will not work $pInvent->pckInventory->productId->name

here is the model

public function pckInventory() {
        return $this->belongsTo( Package::class, 'package_id' );
}

in this case how can I count the products_id groups.

Feb
12
3 months ago
Activity icon

Replied to Increment Inside Foreach Loop.

OK Forget about the event imagine what in the event is under code. my problem is I need to insert new row called reserved and add the amount that is token from the inventories.

Activity icon

Started a new Conversation Increment Inside Foreach Loop.

describe case : I have multi inventories for ex.

-------------------------------------------------------
|id|product_id|quantity|expiry_date|
-------------------------------------------------------
| 1 | 1 | 20 | 2020-03-29 |
-------------------------------------------------------
| 2 | 1 | 20 | 2020-02-29 |
-------------------------------------------------------

now when customer do orders I want to decries from each inventory the amount coming from order. For ex. Order Quantity is 50 now each inventory will turn to 0.

All this is fine but there is one final thing here is the reserved quantity. I want to take from each inventory the quantity that has been taken from it. so the total will be 40

Now comes the code in my controller.

$reserved = 0;
    foreach ( $sortedInventories as $sortedInventory ) {
        $existsVirtual = $packageInventory->contains( 'type', 'virtual' ) ? 'yes' : 'no';
        $totalExists   = $packageInventory->where( 'type', 'existing' )->sum( 'amount' );

            $package_inventory = [
                 'orderId'           => $orderList->id,
                 'sortedInventoryId' => $sortedInventory->id,
                 'orderQuantity'     => $orderQuantity,
                 'existVirtual'      => $existsVirtual,
                 'totalExists'       => $reserved,
                 'packageId'         => $sortedInventory->package_id,
                 'productId'         => $sortedInventory->product_id,
                 'expiryDate'        => $sortedInventory->expiry_date,
                 'storeId'           => $sortedInventory->store_id,
            ];
            $newQuantity       = $sortedInventory->amount - $orderQuantity;
            if ( $sortedInventory->amount >= $orderQuantity ) {
                 PackageInventory::whereId( $sortedInventory->id )->update( [ 'amount' => $newQuantity ] );
                event( new DoReserved( $package_inventory ) );
                break;
            } else {
               $orderQuantity = $orderQuantity - $sortedInventory->amount;
               PackageInventory::whereId( $sortedInventory->id )->update( [ 'amount' => 0 ] );
               event( new DoReserved( $package_inventory ) );
            }
        } 

and here is what the event listener do

PackageInventory::updateOrCreate(
            [
                'order_id'   => $event->packageInventory['orderId'],
                'package_id' => $event->packageInventory['packageId'],
                'product_id' => $event->packageInventory['productId'],
                'store_id'   => $event->packageInventory['storeId'],
                'type'       => 'reserved'
            ],
            [
                'amount'      => $event->packageInventory['totalExists'],
                'expiry_date' => $event->packageInventory['expiryDate']
            ]
        );

No all I need is to increment the $reserved with the amount that has been taken from the inventory each time in the loop.

Jan
29
3 months ago
Activity icon

Replied to Laravel : Return Json Data From An Array

I didn't check this documentations yet thanks will review it.

Activity icon

Replied to Laravel : Return Json Data From An Array

Thanks this was very helpful

Jan
28
3 months ago
Activity icon

Started a new Conversation Laravel : Return Json Data From An Array

I have foreach I need return each object in this array in JSON

Let me present my controller first

$childCategory = ChildCategory::whereProductCategoryId( $catId )->get();
                $data          = array();
                foreach ( $childCategory as $childCat ) {
                    $checkChildes = ChildCategoryTwo::whereChildCategoryId( $childCat->id )->count();
                    $hasChildes   = $checkChildes > 0 ? "Yes" : "No";

                    $data[] = $childCat->id;
                    $data[] = $childCat->image;
                    $data[] = $childCat->bg_color;
                    $data[] = $childCat->product_category_id;
                    $data[] = $childCat->translations[0]->name;
                    $data[] = $childCat->translations[1]->name;
                    $data[] = $hasChildes;
                }

                return response()->json( [$data] );

the output of data comes like this

    [
        1,
        "1561015312.png",
        "#a9dabf",
        1,
        "Drinks",
        "No",
        2,
        "1562500737.jpg",
        null,
        1,
        "Drinks",
        "Yes"
    ]

the output is right but the format is wrong, I need it to output something like this

[
      {
            id : 1,
            image : "1561015312.png",
            color : "#a9dabf",
            parent_cat : 1,
            name : "Drinks",
            has_child : "No",
      },
      {
            id : 2,
            image : "1562500737.jpg",
            color : null,
            parent_cat : 1,
            name : "Soda Drinks",
            has_child : "Yes"
      }
]
Jan
09
4 months ago
Activity icon

Replied to Laravel-5.8 Validation Required Field If A Certain Radio Button Is Checked

Thanks, but try required_if didn't do it here is what I try

return [
            'title'        => 'required',
            'details'      => 'required',
            'web'          => 'required',
            'route'        => 'required_if:web, web',
            'clients_view' => 'required_if:web, clients',
        ];

Still when I submit the validation don't work!

Activity icon

Started a new Conversation Laravel-5.8 Validation Required Field If A Certain Radio Button Is Checked

I have this tow radio buttons and they are required

<div class="form-group">
    {!! Form::label('wc', trans('interface.notificationForWebMobile'), ['class'=>'col-sm-3 control-label']) !!}
<div class="col-sm-9">
     <label><input type="radio" name="web" value="web"> {{trans('interface.Website')}}</label>
     <label><input type="radio" name="web" value="clients"> {{trans('interface.mobApp')}}</label>
 </div>

and I have this two fields.

{!! Form::text('route', null, ['class'=>'form-control', 'placeholder'=>trans('interface.notificationRoute')]) !!}

{!! Form::select('clients_view', [
                            null => 'Please select',
                            'Home' => 'Home',
                            'Search'=>'Search',
                            'Registration'=>'Registration',
                            'Login'=>'Login',], ['class'=>'form-control', 'placeholder'=>trans('interface.notificationRoute')]) !!}

I need to validate this two fields as required depending on checked radio button

as ifradio button value is web then the route field is required and if the radio value is clients then the clients_view menu is required.

here is my current Request

public function rules() {
        return [
            'title'   => 'required',
            'details' => 'required',
            'web'     => 'required',
            'route'     => 'required_with:',
        ];
    }
Jan
08
4 months ago
Activity icon

Replied to How To Flash Toastr Error Message If Validation Fails When Using FormRequest?

I have try this solution but no luck didn't work for me.