jeudi 3 mars 2022

How to display single image in slider using loop in Laravel 5.7?

I am trying to display the single image in the slider, but whenever the user will click on this then it's open a popup in data-src="multiple-images-here" where I am running the slider. But the main issue is with the single image display in for loop. It displays all the available images in the loop. Please suggest me the best solution for this.

Here is my index.blade.php file where I am trying to display the slider using this code.

<div class="container-fluid margin-gallery d-lg-block d-block position-relative">
     <div class="row">
        <div class="col-lg-12 p-0">
           <div class="demo-gallery">
              @php
              if($propertys->propertyImage!=null){
              $images =  json_decode($propertys->propertyImage->image_path, true);
              }
              else{
              $images=null;
              }
              @endphp
              <?php
              $images_prop = collect($images['property_images']);
              ?>
              <ul id="lightgallery">
              @if($images['property_images']!=null)
                 @forelse($images_prop as $img)
                 <li class="sizing-li position-relative" data-src="">
                    <a href="javascript:void()">
                       <img class="img-responsive" src="" style="width:929px; height:495px;">
                    </a>
                 </li>
                 @empty
                 @endforelse
                 @endif
              </ul>
           </div>
        </div>
     </div>
  </div>

I want to display the first image from for loop here

<img class="img-responsive" src="" style="width:929px; height:495px;">


via Chebli Mohamed

Laravel - validate input to be in the interval [-1, 10] without 0. not_in not working properly?

As the title says, I'm having trouble validating an input field like I said in the title.

I tried it this way:

$request->validate([
            'nota' => 'min:-1|not_in:0|max:10',
        ]);

Basically, I want this "nota" field to have values in the interval [-1, 10] without 0.

But I can still enter 0.

Why doesn't it work and how can I fix it?



via Chebli Mohamed

How to create a record in the database base on email received to my site?

I have Laravel web application for my portfolio. https://www.bunlongheng.com

Under the portfolio section, you will see this

enter image description here

Right now, I have a CRUD for admin only to create them, but a thought come to my mind ...

I would like to create this automated email to just email myself to create a new record to the database.

Example

Anyone email to : portfolio@bunlongheng.com

Subject: Title of portfolio Body:

  1. Task 1
  2. Task 2
  3. Task 3

With attachments : 2 images (example)

Goal:

I want to accept and parse that email, and create a new portfolio record

  • Subject = Title
  • Body = Description
  • Attachments = Portfolio Images

enter image description here

How do I start ?



via Chebli Mohamed

How can I optimize or reduce the time complexity of the following code in Laravel 5.2.45?

In users table I have following data.

id   name    parent_id 
1     A           0
2     B           1
3     C           2
4     D           3
5     E           4
6     F           1

A have four level children. E is also children of A. E is the last, it has not any children. Now I can fetch all the children of any parent like the following code.

function getChildren($Id){
   $cache = [];
   $chList = [];
   $allUser = User::orderBy('parent_id', 'asc')
        ->orderBy('parent_id', 'asc')
        ->get();
   foreach($allUser as $user) {
    $cache [$user->parent_id][] = $user;        
   }
  $children0 = empty($cache [$Id]) ? [] : $cache [$Id];
  #first level child
  foreach($children0 as $child1) {

    $chList[$child1->id] = $child1;

    if(!empty($cache [$child1->id])) {

        #Second level child
        foreach($cache [$child1->id] as $child2) {

            $chList[$child2->id] = $child2;

            if(!empty($cache [$child2->id])) {

                #third level child
                foreach($cache [$child2->id] as $child3) {

                    $chList[$child3->id] = $child3;

                    if(!empty($cache [$child3->id])) {

                        #fourth level child
                        foreach($cache [$child3->id] as $child4) {
                            #can not be parent
                            $chList[$child4->id] = $child4;
                            
                        }
                    }
                }
            }
        }
    }
}

return $chList;
}

I can get the actual result by this code. But for huge data it is time consuming. Can any one help me to reduce time complexity or optimize the code?



via Chebli Mohamed

mercredi 2 mars 2022

Laravel 5.7 Chunk Exception: Cannot traverse an already closed generator

I am trying to get my models as generators so I can then loop over them 1 after the other, I am getting Exception: Cannot traverse an already closed generator.

 $chunk = MyModel::where('processed', '=', false)->cursor();
 foreach ($chunk as $c) {
  dd($c);
}


via Chebli Mohamed

laravel fetching data how to aligned in single line

I'm new in laravel and html im able to get data from table using foreach. but i get data not aliened as I required help me.

my required alignment enter image description here now I'm getting alignment enter image description here

@foreach ($Subject_Of_Examination as $user)
                                       
   <td style=" text-align: center; position: absolute; " > <font size="3"><p>,</p></font></td>
                                       
@endforeach


via Chebli Mohamed

mardi 1 mars 2022

How to print custom message in try catch - php laravel

I need to write to log some data when exception occurred, since it is dynamic data and I need the message I tried with simple code, just to write 'Boom' if exception occurred. However just the automatic exception data written in logs (as before the try catch). can someone advise how to print to log in the catch? I just need to add additional text to the exception, just to be more specific

try {
    $sheet->setCellValue($cell,$innervalue);
} catch(Exception $e) {
    $message = 'Can not set value: '.$innervalue .' in cell ' .$cell .$headerArray[$headerIndex];
    \Log::info('boom');
}

and in the logs nothing displayed enter image description here



via Chebli Mohamed