Results: 1024
upload multiple files at once and validate file mimes on front / back
In order to upload several files at once we need to specify
multiple
attribute and append
[]
to input name
<input type="file" multiple="multiple" name="files[]" placeholder="Attach file"/>
To validate show errors on client side we have to append input name with
.*
Complete example of the
form-group
<div class="form-group">
    <label for="files">Attach File</label> &nbsp;
    <i id="add_new_file" class="ki ki-plus text-success pointer" title="Add New File"></i>
    <input type="file" class="form-control mb-2 {{ $errors->has('files.*') ? 'is-invalid' : '' }}" multiple="multiple" name="files[]" placeholder="Attach file"/>
    @if($errors->has('files.*'))
        <div class="invalid-feedback">
            <strong>{{ $errors->first('files.*') }}</strong>
        </div>
    @endif
</div>
Server side validation of mime types
public function rules()
{
    return [
        'comment' => 'sometimes|nullable|min:2|max:255',
        'files.*' => 'sometimes|nullable|mimes:pdf,docx,xlsx,pptx,rar,zip,png,jpg',
    ];
}
It's necessary the validator key
files
to be appended with the same symbols
.*
copy text to clipboard with break lines
If we need to copy text to clipboard with break lines, than we need to use
textarea
instead of using
input
element
function copy(part1, part2) {

    // Creates textarea element 
    var textarea = document.createElement('textarea');

    // Puts the text into the textarea with breaklines
    textarea.value = part1 + "\n\n" + part2

    // Adds the element to the DOM
    document.body.appendChild(textarea);

    // Selects the text that we want to copy
    textarea.select();

    // Copies the selected text clipboard
    var result = document.execCommand('copy');

    // Removes the element from the DOM because we no longer need it
    document.body.removeChild(textarea);
    return result;
}
class Say {
    public function let() {
        echo 'Let' . " ";
        return $this;
    }
    public function me() {
        echo 'Me' . " ";
        return $this;
    }
    public function tell() {
        echo 'Tell' . " ";
        return $this;
    }
    public function you() {
        echo 'You' . " ";
        return $this;
    }
    public function something() {
        echo 'Something' . " ";
    }
}
$phrase = new Say;
$phrase->let()->me()->tell()->you()->something();
__get
and
__set
methods are called when there is an attempt either to use or to set a new value to any of the class property
class Person{
    public $firstName;
 
    public function __get($propertyName){
        echo "attempted to read property: $propertyName" . PHP_EOL;
    } 
    public function __set($propertyNane, $propertyValue){
        echo "attempted to set new value to property: $propertyNane" . PHP_EOL;
    } 
}
 
$p = new Person();
 
$p->firstName = 'Doe';
echo $p->firstName . PHP_EOL;
 
$p->lastName = 'John';
echo $p->lastName;
If the property is
public
, the methods are not going to be called (if the property does not exist, or exists but is not public, the methods will get called) If we make
$firstName
property
private
or
protected
, the methods will be called for the
$firstName
property
__get
,
__set
,
__call
and
__callStatic
are invoked when the method or property is inaccessible
we can not use $this (object properties) inside static methods
class Wheather {
    public $nonStatic = 0;
    public static $tempConditions = ['cold', 'mild', 'warm'];
    public static $someProperty = 5;

    static function celsiusToFarenheit($celsius) {
        return $celsius * 9 / 5 + 32 + $this->nonStatic;
    }
}

echo Wheather::celsiusToFarenheit(0);
echo "\n";
In this case we will get the error:
Fatal error:  Uncaught Error: Using $this when not in object context
Lists logged in user's posts on dashboard
$user_id = auth()->user()->id;
$user = User::find($user_id);
return view('dashboard')->with('posts', $user->posts);
After the command
git commit
->
i
- to jump into insert mode ->
esc
- takes us out of insert mode ->
:w
- to save written comment ->
:q
- to quit ------------------------
:w
and
:p
can be combined into
:wq
PHP For loop CODE
if
has only one section:
Condition
if (Condition) {
    // Code to be executed
}
for
loop has three sections divided by
;
for (Initialization;   Condition;   Increment/Decrement) {
    // Code to be executed
}
The loop prints numbers from 0 to 10
for ($i = 0;  $i < 10;  $i ++) {
	echo $i . "\n";
}
User-defined functions CODE
Function that does something
function printGreeting() {
	print "Prints: Hello world!\n";
}
printGreeting();
Function that returns something
function returnGreeting() {
	return "Returns: Hello world!\n";
}
echo returnGreeting();
Parameterized function that doubles the number
function doubleMe($number) {
	$result = "$number times 2 is: " . $number * 2 . "\n";
	return $result;
}
echo doubleMe(4);
Function calculates the sum of two numbers
function add($num1, $num2) {
	$sum = $num1 + $num2;
	$result = "Sum of $num1 and $num2 is: $sum";
	return $result;
}
echo add(4, 5);
PHP if-else statement CODE
The code will output "Have a nice weekend!" if the current day is Friday, otherwise - "Have a nice day!"
$d = date("D");
if($d == "Fri"){
    echo "Have a nice weekend!";
} else{
    echo "Have a nice day!";
}
echo "\n";
Output "Have a good day!" if the current time is less than 20, and "Have a good night!" otherwise:
$t = date("H");
if ($t < "20") {
    echo "Have a good day!";
} else {
    echo "Have a good night!";
}
If
$age
is more than
18
, prints
Adult
otherwise prints
Child
$age = 20;
if ($age > 18) {
    echo 'Adult';	
} else {
    echo 'Child';	
}
Results: 1024