vendredi 1 novembre 2019

Cart still showing the product which has been deleted Laravel

I have this function which allows user to add product to cart but if the product is deleted in admin panel it still shows in the cart, any idea on how to remove the product in cart which has been deleted in admin dashboard?

Controller

   public function addToCart($id)
  {
    $userId = Auth::id();
    $products = Product::with('images')->find($id);
    if(!$products) {

        abort(404);

    }

    $cart = session()->get('cart');

    // if cart is empty then this is the first product
    if(!$cart) {

        $cart = [
                $id => [
                    "name" => $products->pro_name,
                    "quantity" => 1,
                    "price" => $products->pro_price,

                ]
        ];

        session()->put('cart', $cart);

        return redirect()->back()
    }

    // if cart is not empty then check if this product exist then increment quantity
    if(isset($cart[$id])) {

        $cart[$id]['quantity']++;

        session()->put('cart', $cart);

        return redirect()->back()
    }

    // if item not exist in cart then add to cart with quantity = 1
    $cart[$id] = [
        "name" => $products->pro_name,
        "quantity" => 1,
        "price" => $products->pro_price,
    ];

    session()->put('cart', $cart);

    return redirect()->back()
}

Blade File

  @if(session('cart'))
  @foreach(session('cart') as $id => $details)
   <h5 class="px" ></h5>
   <h4 class="ft" ></h4>

    @endforeach
   @endif


via Chebli Mohamed

Laravel Application Response Time Slow on Kubernetes (EKS) vs. Baremetal

I am in the process of migrating our Laravel application to EKS Kubernetes which is currently running on Docker however the response time is significantly slower.

The current response time is roughly (Docker): 350-450 ms
The new response time is roughly (Kubernetes): 750-1100 ms

Notable Environment Differences:

  • Source code is mounted as a volume in the Docker environment vs. Kubernetes environment has the source code baked into the image (with PVC's for the storage folder)
  • Docker environment is running on a bare metal server vs. Kubernetes is on AWS's EKS

APM Findings:

I am running DataDog which shows that a lot of time is being spent on Laravel, rather than DB or Redis which doesn't give me much to work with.

enter image description here

At this point, I am thinking it is infrastructure related rather than an issue with Laravel as the Docker environment already preforms (decently).

I am running this as an init container (which occurs every deployment or pod restart):

php artisan opcache:clear
php artisan route:cache
php artisan config:clear
php artisan config:cache
php artisan view:clear
php artisan view:cache
php artisan opcache:compile --force
php artisan migrate --force
php artisan db:new_seed
php artisan queue:restart

I am unsure where to start troubleshooting. Any advice will be helpful.



via Chebli Mohamed

Return new collection without modifying original collection

I have a Laravel collection and I want to modify one of its property value to negative integer. For this I am using map function on collection but it also modifying the original collection.

Here is my code

$modified_revenue_data = $revenue_data->map(function ($item) {
            if ($item->is_claw_back == 1 && $item->claw_back_date != null)
            {
                return $item->revenue = $item->revenue * -1;
            }
            return $item;
        });

I want to store new collection into $modified_revenue_data but $revenue_data also being modified.

What is the correct way of doing it without changing anything from original collection?



via Chebli Mohamed

Laravel : file_get_contents(): SSL operation failed with code 1

file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

Getting this error in local server. Working fine in Production

OS: macOS Mojave



via Chebli Mohamed

How to retrieve only records that are not deleted from database in Laravel?

I have recommends table that has product_id, user_id and deleted_at columns and I want to display the most viewed products in the view and it works fine but the problems is when user deletes the product I get an error trying to get property of non object and the column deleted_at in recommends table still showing null even if the product is deleted. How can I make the product disappear in recommends table after the product has been deleted and the view should display only the products which are not deleted?

Controller

  $recommends = Recommends::with('product')
    ->select('product_id', DB::raw('COUNT(*) AS total'))
    ->whereNotNull('product_id')
    ->groupBy('product_id')
    ->orderby('total', 'DESC')
    ->take(12)
    ->get();

Recommends.php

 <?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Recommends extends Model
{
public function product()
{
    return $this->belongsTo('App\Product','product_id')
}
}

Product.php

 public function recommends()
 {
    return $this->hasMany('App\Recommends','product_id');
 }


via Chebli Mohamed

show the first image from image json object contains multiple image in laravel view

I am new to laravel and this image contains the json object which contains multiple image url in image string. I just need to show the first image to the view.



via Chebli Mohamed

How to fix a "redirecting to http" error on local host on MAC?

Am a frontend dev, and i just started using laravel. i get a redirect to http error any time i try to login or register on the local host of my webapp on my MAC. and i dont know why

Am using a MAC system for the project.



via Chebli Mohamed