vendredi 5 août 2022

HTTP ERROR 500? is currently unable to handle this request? [closed]

/home/maxmpgdb/public_html/vendor/laravel/framework/src/Illuminate/View/Engines/CompilerEngine.php(61): Illuminate\View\Engines\PhpEngine->evaluatePa in /home/maxmpgdb/public_html/resources/views/themes/royalblue/partials/footer.blade.php on line 58enter image description here



via Chebli Mohamed

jeudi 4 août 2022

Apply model policy in a global scope query

I have a complexe user policy (extending the PolicyAbstract class) granting reading/editing access for a given user to other user based on several rules and other parameters of the current user.

I am currently applying the policy on each records and filtering out the user the given does not have access after the query has been performed by walking the collection. This has the adverse effect of messing up the pagination collection results as I remove records after the pagination was set.

I would like to apply the policy in a global scope so the forbiden user are already filtered from the paginated collection. Something like this:

public static function boot() {

    parent::boot();
            
    static::addGlobalScope('user_selection', function(Builder $builder) {
        $builder->[involve the user policy here]
    }

}

Is this possible in any way? Any help or pointer to achieve it would be greatly appreciated.



via Chebli Mohamed

how to parse db object in laravel - php

I am new to laravel and I get this results from the DB Query that I made.

[{
    
    "screen_type": "ctv",
    "resulte1": "9.00",
    "resulte2": "9.00",
    "created_at": "2022-06-23 12:03:54",
    "updated_at": null
}, {
    
    "screen_type": "app",
    "resulte1": "9.00",
    "resulte2": "9.00",
    "created_at": "2022-06-23 12:03:54",
    "updated_at": null
}, {
    
    "screen_type": "site",
    "resulte1": "9.00",
    "resulte2": "9.00",
    "created_at": "2022-06-23 12:03:54",
    "updated_at": null
}]

I want to put in variables the resulte1 of screen_type ctv and in another variable the resultes of screen type app

how in laravel\php I can parse this DB Object and get this values?



via Chebli Mohamed

Laravel permissions pivot table

I am making custom permissions,

I would like them to be divided into sections e.g. orders, customers etc. and each section would have some permission e.g. edit, delete, add. Just to check if a particular user has access I would have to start with the Section model?

Because I can't do something like Auth::user()->permissions()->with('sections')->contains('name','display')->contains('sections','order')

I would like to simply check if the user has access to, for example, order.display. I have a feeling that this section does not make sense here.

How to do it? I know there is a spatie(laravel-permission), but there is a role division there.

Example schema of the database (user_permission is a pivot): enter image description here



via Chebli Mohamed

How to export currently searched data in excel laravel?

I am trying to exceldownload only the data searched by the user but my code isnt working.

Controller

 public function get_data(Request $request, Covidrecord $covidrecord)
{
    $search = $request['search'] ?? '';
    if ($search != '') {
        $covidrecord = Covidrecord::where('fullname', 'LIKE', "%$search%")
            ->orWhere('gender', 'LIKE', "%$search%")
            ->orWhere('age', 'LIKE', "%$search%")
            ->orWhere('hospital_type', 'LIKE', "%$search%")
            ->orWhere('vaccine_status', 'LIKE', "%$search%")
            ->get();
    }
   
       Excel::create('covidrecord', function($excel) use($covidrecord) {
                   $excel->sheet('sheet 1', function($sheet) use($covidrecord){
                       $sheet->fromArray($covidrecord);
                   });
       })->download('xlsx');

}

Below is the Export.php..How can i filter search here??

CovidRecordExport.php

class CovidRecordExport implements FromCollection
{
   
    public function headings():array{
        return[
            'fullname',
            'age',
            'gender',
            'ethinicity',
            'religion',
            'occupation',
            'qualification',
            'financial_status',
            'marital_status',
            'infection_date',
            'covid_verficiation',
            'hospitalization_date',
            'hospital_type',
            'death_date',
            'death_reason',
            'after_covid_death',
            'death_location',
            'vaccine_status',
            
        ];
    }
    public function collection()
    {
        return CovidRecord::all();
    }
}

I tried dd() and it seems like my if condition is not working.



via Chebli Mohamed

mardi 2 août 2022

"Invalid argument supplied for foreach()", in laravel controller

I want to add coupon code system in e commerce website using laravel 5.8 when i click on apply coupon button it gives me error like "Invalid argument supplied for foreach()" the problem is with that foreach loop in my controller when i remove it then it shows 0 price can anybody help me to solve this problem

here is my controller

   public function applycoupon(Request $request){
    $coupon = $request->input('coupon_code');
    if(Coupons::where('coupon_code',$coupon)->exists())
    {
      $coupon = Coupons::where('coupon_code',$coupon)->first();
     if($coupon->start_date<=Carbon::today()&& Carbon::today()<= $coupon->end_date){
        $totalprice = "0";
        $cookie_data = stripslashes(Cookie::get('shoping_cart'));
        $cart_data = json_decode($cookie_data,true);
        foreach ($cart_data as $itemdata){
            $product = Product::find($itemdata['item_id']);
            $prod_price = $product->offer_price;
           // $prod_price_tax = ($prod_price*$product->tax)/100;
            $totalprice = $totalprice+($itemdata['item_quantity']*$prod_price);
        }

        //checking Type (percentage or amount)
       if($coupon->coupon_type=='Percentage')
       {
        $discount_price = ($totalprice/100)*$coupon->coupon_price;
       }
       elseif($coupon->coupon_price=='amount'||'Amount')
       {
         $discount_price = $coupon->coupon_price;
       }
       $grand_total = $totalprice-$discount_price;
       return response()->json([
         'discount_price' =>$discount_price,
          'grand_total' =>$grand_total,
       ]);
     }
     else
     {
     return response()->json([
        'status'=> 'coupon code has been expired',
        'error_status'=>'error'
      ]);
      }
    }
    else
    {
        return response()->json([
            'status'=> 'coupon code does not exists',
            'error_status'=>'error'
          ]); 
    }
}

views.blade file

 <div class="card-body">
                    <div class="d-flex justify-content-between mb-3 pt-1">
                        <h6 class="font-weight-medium">Subtotal</h6>
                        <h6 class="font-weight-medium">$</h6>
                    </div>
                    <div class="d-flex justify-content-between">
                        <h6 class="font-weight-medium">Discount</h6>
                        <h6 class="font-weight-medium discount_price">0.00</h6>
                    </div>
                </div>

Route

Route::post('apply-coupons','CouponController@applycoupon');

custom js file

custom js file

    //Apply coupons
$(document).ready(function () {
    $('.apply-coupon-btn').click(function (e) {
        e.preventDefault();
        var coupon_code = $('.coupon_code').val();
        if ($.trim(coupon_code).length == 0) {
            error_coupon = "please enter valid coupon";
            $('#error_coupon').text(error_coupon);
        } else {
            error_coupon = "";
            $('#error_coupon').text(error_coupon);
        }
        if (error_coupon != '') {
            return false;
        }
        $.ajax({
            method: "POST",
            url: "/apply-coupons",
            data: {
                'coupon_code': coupon_code,
            },
            success: function (response) {
                if (response.error_status == 'error') {
                    alertify.set('notifier', 'position', 'top-right');
                    alertify.success(response.status);
                    $('.coupon_code').val('');
                }
                else{
                    var discount_price = response.discount_price;
                    var grand_total = response.grand_total;
                    $('.discount_price').text(discount_price);
                    $('.grand_total').text(grand_total);
                }
            }
        });

    });
});


via Chebli Mohamed

lundi 1 août 2022

Laravel SwitMessage add Reply-path header

We have a Laravel application which sends newsletters to customers. As per the client's request, we would like to add a header in the email indicating that it's a newsletter or mail to a list to avoid out of office email bounces. Upon research, I found that one way to do so is to add Precedence: bulk header. But answer also says that RFC 2076 discourages adding Precedence header. Some post recommend adding Return-Path: <> (a NULL Return-Path). However, I'm not able to add this header. I have tried following codes to add such a header but not working.

$headers->addTextHeader('Return-Path', ''); - Uses the mail id defined in config file 
$headers->addTextHeader('Return-Path', '<>') - This generates a malformed header as in the attached image
$headers->addTextHeader('Return-Path', ' '); - Adds a header like Return-Path   
< > but not sure if this is correct or it will create other issues


via Chebli Mohamed