Validate checkbox array and prefill with selected value on validation error in laravel 5

Say for example you have your checkbox field code in your twig file as below:-

@foreach ($serviceList as $servloop)
<div class=”checkbox”>
<label>
<input type=”checkbox” name=”services[]” value=”{{$servloop->service_id}}” {{ ( is_array(old(‘services’)) && in_array($servloop->service_id, old(‘services’)) ) ? ‘checked ‘ : ” }} />
{{$servloop->service_name}}
</label>
</div>
@endforeach

Controller file code

Validation Rule will be as below:-

$rules = [
‘services’ => ‘required|min:1’,  // min:1 means atleast one should be selected
];

$customMessages = [
‘services.required’ => ‘Select the service you will need from us, for and during the Expo.’,
];

 

 

 

how to set public permission to image uploaded on s3 aws in laravel 5.5

First set your s3 bucket configuration in your .env file as below.

S3_AWS_KEY=AKIAIDTXXXHUR7ABCDEF
S3_AWS_SECRET=2XTTy0q8xxdaddffrtsfsdfdfgfhvcdsfsf
S3_AWS_REGION=us-east-2
S3_AWS_BUCKET=BUCKET_NAME

In your controller file add the following code to set the public property to the uploaded image.

if (request()->hasFile(‘image_field_name_inyour_form’)) {

$folderPath = ‘profile/thumbnailfolder/’; // Set the folder path where you want to upload on s3 bucket
$imageName = time().rand(0,999999).’.’.request()->file(‘main_image’)->getClientOriginalExtension(); // Set name for the uploaded image with extension
$imageObj = request()->file(‘main_image’); // This will retrieve the uploaded images attributes. in short the file objects.
$uploadResponse = Storage::disk(‘s3’)->put($folderPath.$imageName, file_get_contents($imageObj), ‘public’); // here if you dont set the “public” attributes, when you access the file in your browser it will display access permission denied error.
$imageNameToStoreInYourDbOrCode = $imageName;

}

Hope you like the code. Please write back for any queries.

how to get sql query with data binding from facade DB::table in laravel 5

We can get the SQL query from DB sql query in Laravel 5

$dataQuery = DB::table(‘sometable’)
->select(‘sometable.column1′, ‘sometable.column2′, ‘sometable.column3′, ‘sometable.column4′, ‘sometable.column5′);

$dataQuery->toSql(); // this will give you the actual query but this will not give the result with actual data values.

Now What ??

To get the Data binded with the sql statements , you should use the below function

$dataQuery->getBindings(); // This will return an array of data binded to the sql statement.

Now to get the final sql statement, you can use the below function and pass $dataQuery->toSql() and $dataQuery->getBindings() to the functions written below , which you can define it in your helper class or common functions class file.

function getSqlWithBinding($sql,$bindDataArr){
foreach($bindDataArr as $binding)
{
$value = is_numeric($binding) ? $binding : “‘”.$binding.”‘”;
$sql = preg_replace(‘/\?/’, $value, $sql, 1);
}
return $sql;
}

$sqlwithData = getSqlWithBinding($dataQuery->toSql(),$dataQuery->getBindings());
or something like below if you have defined getSqlWithBinding function in common class.

$sql = CommonFunctionClass::getSqlWithBinding($dataQuery->toSql(),$dataQuery->getBindings());

how to get sql query from facade DB::table in laravel 5

Normally we fetch the data as below

$dataresult = DB::table(‘sometable’)
->select(‘sometable.column1’, ‘sometable.column2’, ‘sometable.column3’, ‘sometable.column4’, ‘sometable.column5’)
->get();

Now if we want to get the actaul query from $dataresult using $dataresult->toSql(); // it will not work

The solution for this is first prepare the query without the get() method and then call the toSql() method as below.

$dataQuery = DB::table(‘sometable’)
->select(‘sometable.column1’, ‘sometable.column2’, ‘sometable.column3’, ‘sometable.column4’, ‘sometable.column5’);

$dataQuery->toSql(); // this will give you the actual query

After that you can fetch the result as below.
$dataresult = $dataQuery->get();

Read the next article to know how to get the sql with data binding