lundi 16 novembre 2020

my project laravel when i click submit in form with errors in input my view refresh multiple time so i can't see message errors

this my view code here i try to do form with validate and div with type of errors:

@if(count($errors) > 0)
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li></li>
            @endforeach
        </ul>
    </div>
@endif
<form action="add" method="POST">
 <!--Securite-->
    Product name <input type="text" value="" class="form-control " name="name" placeholder="enter product">
<br>
    Product Price <input type="text"  class="form-control " value="" name="price" placeholder="enter price">
<br>
<input type="submit" value="Add Product">
</form>
@endsection


via Chebli Mohamed

laravel validation, how to add validation rule on client side?

//html code
<form>
<input type="radio" id="defult" name="price_type" value="default">
<label for="defult">Default Price</label><br>
<input type="radio" id="custom" name="price_type" value="custom">
<label for="custom">Custom Price</label><be>

<input placeholder="Custom Price" class="form-control" name="custom_price">
</form>

$('input[type=radio][name=price_type]').change(function() {
    if (this.value == 'default') {
        //make custom_price optional
    }
    else if (this.value == 'custom') {
        //make custom_price required
    }

});

Actually, I have a radio box for the custom price or default price, if the user selects the custom price then want to make input[name=custom_price] required or if the user selects default price then make input[name=custom_price] optional.



via Chebli Mohamed

Laravel \Illuminate\Filesystem\Filesystem File:files() count only the files that have specific extension e.g jpg png in all subfolders

i can count files in the folder with this :

 \Illuminate\Filesystem\Filesystem\File::files($path)

then i can count all files in all subfolders (with specific storage)

 foreach ($directories as $directory) {

     $path = Storage::disk($this->disk)->path($directory);

     $files = \Illuminate\Filesystem\Filesystem\File::files($path);
            if ($files) {
                $count += count($files);
            }
     }
}

i can count all files in this folder

How can i count only png or jpeg files with minimum overload?

thanks in advanced!



via Chebli Mohamed

get page number in object of laravel

i have object given below and i wanted to pagination in this how can i get

$productListArray = array();
 $productListObject = ((object)[
                "id"=>$productList->id, 
                "title"=>$productList->title,
                "slug"=>$productList->slug,
                'categoryName'=>$categoryName[0]->cat_title,
                'brand'=>$brandName[0]->brandname,
                'minMrp'=>$minMrp,
                'maxMrp' =>$maxMrp,
                'minSellingPrice' => $minSellingPrice,
                'maxSellingPrice' => $maxSellingPrice,
                'rating'=>$productList->rating,
                'rating_count' => $productList->rating_count,
                'image' => $img[0]
            ])->paginate();
       array_push($productListArray, $productListObject);
    }
    return response()->json($productListArray, 200);


via Chebli Mohamed

Search in the related model in Laravel

I have post table like this :

id | post_title | category_id

and this is category table

id | category_title

this is the relation between these two which is inside post model :

  public function category()
    {
        return $this->belongsTo(Category::class, 'category_id', 'id');
    }

I want to get the record of post table where category_title or post_title matches the keyword entered by user.

I'm retrieving data something like:

Post::where(['title'=>$request->title])->with('category')->paginate(10);

but here it is only fetching Post title but i also want it to search it in category title.

Any help is highly appreciated.



via Chebli Mohamed

How to add watermark to image on upload in laravel 5.8

I want to add watermark when a user uploads an image on the system. My current code works fine but I need it to work on upload. What do I add to my code?

My WatermarkController file

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Image;

class WaterMarkController extends Controller
{
  public function imageWatermark()
  {
 $img = Image::make(public_path('images/background.png'));

 /* insert watermark at bottom-right corner with 10px offset */
 $img->insert(public_path('images/watermark.png'), 'bottom-right', 10, 10);

 $img->save(public_path('images/background.png'));

 $img->encode('png');
 $type = 'png';
 $new_image = 'data:image/' . $type . ';base64,' . base64_encode($img);

 return view('show_watermark', compact('new_image'));
 }

 public function textWatermark()
 {
 $img = Image::make(public_path('images/background.jpg'));

 $img->text('MyNotePaper', 710, 370, function ($font) {
     $font->file(public_path('font/amandasignature.ttf'));
     $font->size(30);
     $font->color('#f4d442');
     $font->align('center');
     $font->valign('top');
     $font->angle(0);
 });

 $img->save(public_path('images/new-image.png'));

 $img->encode('png');
 $type = 'png';
 $new_image = 'data:image/' . $type . ';base64,' . base64_encode($img);

 return view('show_watermark', compact('new_image'));
 }

 }

web.php file

Route::get('watermark-image', 'WaterMarkController@imageWatermark');
Route::get('watermark-text', 'WaterMarkController@textWatermark');

Like I said my current code works fine but I need it to work dynamically when a user uploads an image to the system, with this code you would have to configure the watermark.png and the backgroud.jpg to the same directory then access the route watermark-image. please help me on this I am very new to Laravel. Thanks



via Chebli Mohamed

dimanche 15 novembre 2020

Larave 5.4 Bootstrap cache force delete

Hello I have some question. In my case AM 1:00 some-laravel-project/bootstrap/cache/services.php was force deleted but i don't know reason... our code don't have any call artisan clear cache command and anyone don't call manually artisan command anybody has same case like my case? if you have same case like my case and find cause plz comment to my question cause our enviroment

  1. AWS EC2
  2. laravel version : laravel 5.4.33
  3. php version : PHP 7.1.4 (cli) (built: Oct 26 2017 15:49:30) ( ZTS )
  4. Apache version: Apache/2.2.15 (Unix)
  5. OS : CentOS release 6.9 (Final)
  6. dependency
{
    "php": ">=5.6.4",
    "ammadeuss/laravel-html-dom-parser": "^1.0",
    "aws/aws-sdk-php": "^3.160",
    "barryvdh/laravel-debugbar": "^2.3",
    "caouecs/laravel-lang": "~3.0",
    "doctrine/dbal": "^2.5",
    "fightbulc/moment": "^1.26",
    "graham-campbell/exceptions": "^9.3",
    "ixudra/curl": "^6.10",
    "laracasts/utilities": "^3.0",
    "laravel/framework": "5.4.*",
    "laravel/tinker": "~1.0",
    "laravelcollective/html": "^5.4",
    "php-imap/php-imap": "^3.0",
    "phpoffice/phpexcel": "^1.8",
    "phpoffice/phpspreadsheet": "^1.2",
    "predis/predis": "^1.1",
    "pusher/pusher-php-server": "^2.6",
    "rap2hpoutre/laravel-log-viewer": "^0.10.0",
    "yajra/laravel-oci8": "5.4.*"
}


via Chebli Mohamed