Results: 1580
Notes
  • Newest first
  • Oldest first
  • Newest first(All)
  • Oldest first(All)
Add FOREIGN KEY constraint after creating a table
Creates
FOREIGN KEY
constraint with
student_id
column and references it to
students.id
ALTER TABLE notes 
ADD CONSTRAINT fk_student_note
    FOREIGN KEY (student_id)  
    REFERENCES students(id) 
    ON UPDATE CASCADE 
    ON DELETE RESTRICT
We can omit
CONSTRAINT fk_student_note
sub-clause. The difference is that if we omit the sub-clause, MySQL names the constraint automatically
ALTER TABLE notes 
ADD FOREIGN KEY (student_id)  
    REFERENCES students(id) 
    ON UPDATE CASCADE 
    ON DELETE RESTRICT
by Valeri Tandilashvili
4 years ago
0
MySQL
ALTER TABLE
1
call JS function ("this" context)
Here
this
means the object being clicked
<a class="view_all" onclick="consoleLogMe(this);" href="javascript:">object being clicked</a>
this
means the global object
window
in this example:
<a class="view_all" href="javascript:alertMe(this)">window object</a> 
by Valeri Tandilashvili
4 years ago
1
JavaScript
DOM
0
Soft Deleting
In addition to actually removing records from your database, Eloquent can also "soft delete" models. When models are soft deleted, they are not actually removed from your database. Instead, a
deleted_at
attribute is set on the model indicating the date and time at which the model was "deleted". To enable soft deletes for a model, add the
 Illuminate\Database\Eloquent\SoftDeletes
trait to the model:
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Flight extends Model
{
    use SoftDeletes;
}
You should also add the
deleted_at 
column to your database table. The Laravel
schema builder
contains a helper method to create this column:
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Facades\Schema;

Schema::table('flights', function (Blueprint $table) {
    $table->softDeletes();
});

Schema::table('flights', function (Blueprint $table) {
    $table->dropSoftDeletes();
});
Now, when you call the
delete
method on the model, the
deleted_at 
column will be set to the current date and time. However, the model's database record will be left in the table. When querying a model that uses soft deletes, the soft deleted models will automatically be excluded from all query results. To determine if a given model instance has been soft deleted, you may use the
trashed
method:
if ($flight->trashed()) {
    //
}
Restoring Soft Deleted Models Sometimes you may wish to "un-delete" a soft deleted model. To restore a soft deleted model, you may call the
restore
method on a model instance. The restore method will set the model's
deleted_at 
column to
null
:
$flight->restore();
You may also use the
restore
method in a query to restore multiple models. Again, like other "mass" operations, this will not dispatch any model events for the models that are restored:
Flight::withTrashed()
        ->where('airline_id', 1)
        ->restore();
The
restore
method may also be used when building relationship queries:
$flight->history()->restore();
Permanently Deleting Models
Sometimes you may need to truly remove a model from your database. You may use the
forceDelete
method to permanently remove a soft deleted model from the database table:
$flight->forceDelete();
You may also use the forceDelete method when building Eloquent relationship queries:
$flight->history()->forceDelete();
Querying Soft Deleted Models As noted above, soft deleted models will automatically be excluded from query results. However, you may force soft deleted models to be included in a query's results by calling the withTrashed method on the query:
use App\Models\Flight;

$flights = Flight::withTrashed()
                ->where('account_id', 1)
                ->get();
The withTrashed method may also be called when building a relationship query:
$flight->history()->withTrashed()->get();
Retrieving Only Soft Deleted Models The
onlyTrashed
method will retrieve only soft deleted models:
$flights = Flight::onlyTrashed()
                ->where('airline_id', 1)
                ->get();
by გიორგი ბაკაშვილი
4 years ago
0
Laravel
0
Merge two arrays into one array:
$a1=["red","green"];
$a2=["blue","yellow"];
print_r(array_merge($a1,$a2));
Merge two associative arrays into one array:
$a3=["a"=>"red","b"=>"green"];
$a4=["c"=>"blue","b"=>"yellow"];
print_r(array_merge($a3,$a4));
Using only one array parameter with integer keys:
$a=array(3=>"red",4=>"green");
print_r(array_merge($a));
by გიორგი ბაკაშვილი
4 years ago
1
PHP
Array
PHP official doc
0
Assign variables as if they were an array:
<?php
$my_array = array("Dog","Cat","Horse");

list($a, $b, $c) = $my_array;
echo "I have several animals, a $a, a $b and a $c.";
?>
by გიორგი ბაკაშვილი
4 years ago
0
PHP
Array
PHP official doc
0
Remove duplicate values from an array:
<?php
$a=array("a"=>"red","b"=>"green","c"=>"red");
print_r(array_unique($a));
?>
by გიორგი ბაკაშვილი
4 years ago
0
PHP
Array
PHP official doc
0
Return the number of elements in an array:
<?php
$cars=array("Volvo","BMW","Toyota");
echo sizeof($cars);
?>
by გიორგი ბაკაშვილი
4 years ago
0
PHP
Array
PHP official doc
0
Replace the characters "world" in the string "Hello world!" with "Peter":
`<?php
echo str_replace("world","Peter","Hello world!");
?>
by გიორგი ბაკაშვილი
4 years ago
0
PHP
String
PHP official doc
0
Return the length of the string "Hello":
<?php
echo strlen("Hello");
?>
by გიორგი ბაკაშვილი
4 years ago
0
PHP
String
PHP official doc
0
Convert all characters to lowercase:
<?php
echo strtolower("Hello WORLD.");
?>
by გიორგი ბაკაშვილი
4 years ago
0
PHP
String
PHP official doc
0
Results: 1580