Cache
class you need to import it use Illuminate\Support\Facades\Cache;
$postCount = Cache::remember(
'count.posts.' . $user->id,
now()->addSeconds(30),
function () use ($user) {
return $user->posts->count();
});
in this case the post count will be cached with different key on every user and will be retrieved by the authorized users id
2nd argument is expiration date, in this case 30 seconds in the future
3rd argument is a callback of what to do and store if cache is not found (is not set yet or 30 seconds has passed)boot
method, but you still need to call it insideprotected static function boot()
{
parent::boot();
static::created(function ($user) {
//do something with created model
});
}
$table->bigIncrements('id'); Incrementing ID (primary key) using a "UNSIGNED BIG INTEGER" equivalent.
$table->bigInteger('votes'); BIGINT equivalent for the database.
$table->binary('data'); BLOB equivalent for the database.
$table->boolean('confirmed'); BOOLEAN equivalent for the database.
$table->char('name', 4); CHAR equivalent with a length.
$table->date('created_at'); DATE equivalent for the database.
$table->dateTime('created_at'); DATETIME equivalent for the database.
$table->decimal('amount', 5, 2); DECIMAL equivalent with a precision and scale.
$table->double('column', 15, 8); DOUBLE equivalent with precision, 15 digits in total and 8 after the decimal point.
$table->enum('choices', ['foo', 'bar']); ENUM equivalent for the database.
$table->float('amount'); FLOAT equivalent for the database.
$table->increments('id'); Incrementing ID (primary key) using a "UNSIGNED INTEGER" equivalent.
$table->integer('votes'); INTEGER equivalent for the database.
$table->json('options'); JSON equivalent for the database.
$table->jsonb('options'); JSONB equivalent for the database.
$table->longText('description'); LONGTEXT equivalent for the database.
$table->mediumInteger('numbers'); MEDIUMINT equivalent for the database.
$table->mediumText('description'); MEDIUMTEXT equivalent for the database.
$table->morphs('taggable'); Adds INTEGER taggable_id and STRING taggable_type.
$table->nullableTimestamps(); Same as timestamps(), except allows NULLs.
$table->rememberToken(); Adds remember_token as VARCHAR(100) NULL.
$table->smallInteger('votes'); SMALLINT equivalent for the database.
$table->softDeletes(); Adds deleted_at column for soft deletes.
$table->string('email'); VARCHAR equivalent column.
$table->string('name', 100); VARCHAR equivalent with a length.
$table->text('description'); TEXT equivalent for the database.
$table->time('sunrise'); TIME equivalent for the database.
$table->tinyInteger('numbers'); TINYINT equivalent for the database.
$table->timestamp('added_on'); TIMESTAMP equivalent for the database.
$table->timestamps(); Adds created_at and updated_at columns.
$table->uuid('id'); UUID equivalent for the database.
@if (Auth::check())
//show logged in content
@else
//show logged out content
@endif
INSERT INTO `t_cal_mention` VALUES
(1, 4, 1426, '0000-00-00 00:00:00', 0)
The insert query will produce the following MySQL errorError Code: 1292. Incorrect datetime value: '0000-00-00 00:00:00' for column inserted_at at row 14
Row with date value '0000-00-00' is not allowedINSERT INTO `t_design` VALUES (5,0,'წმ. დიდმოწამე მარინე','წმ. დიდმოწამე მარინე','წმ. დიდმოწამე მარინე','oi8io_104355.jpg','0000-00-00','')
The insert query will produce the following MySQL errorError Code: 1292. Incorrect date value: '0000-00-00' for column 'date' at row 14
VALUES
clause we list the values in the same order as the columns are defined in the first clauseINSERT INTO `students` (
`id`,
`first_name`,
`last_name`,
`points`,
`mail`,
`santa_id`
)
VALUES (
NULL,
'დავით',
'ბერიძე',
'54',
'david@gmail.com',
'10'
);
Inserts several rows with one INSERT statementINSERT INTO `students` (
`id`,
`first_name`,
`last_name`,
`points`,
`mail`,
`santa_id`
)
VALUES
(NULL, 'დავით', 'ბერიძე', '54', 'david@gmail.com', '10'),
(NULL, 'გელა', 'თავაძე', '54', 'david@gmail.com', '10'),
(NULL, 'თამარ', 'დავითაშვილი', '54', 'david@gmail.com', '10')
;
We can exclude nullable fields and pass only the necessary fieldsINSERT INTO `students` (
`first_name`,
`last_name`,
`points`
)
VALUES (
'დავით',
'ბერიძე',
'54'
)
INSERT & SELECT statementINSERT INTO `students` (`first_name`, `last_name`, `points`)
SELECT first_name, last_name, points * 1.2 FROM students WHERE id = 3
We can use sub-query
in INSERT statement.
Before inserting the record, sub-query
gets gender_id
based on the provided gender nameINSERT INTO students (
first_name,
last_name,
points,
gender_id
)
VALUES (
'ილია',
'დავითაშვილი',
'84',
(SELECT id FROM genders WHERE name = 'Male')
)
```setUrlParameter({'param_name': 'value1'})
set multiple parameters setUrlParameter({'param_name': 'value1', 'param_name2': 'value2'})
open the altered url in new tab: setUrlParameter({'param_name': 'value1'}, true)
get string of updated url instead of redirecting: let url = setUrlParameter({'param_name': 'value1'}, null, true)
update custom url string instead of using current page url: setUrlParameter({'param_name': 'value1'}, null, null, 'https://youtube.com')
function setUrlParameter(NameValuePairs, newTab = false, get_string = false, custom_url = false)
{
let url;
if(custom_url){
url = custom_url;
}else{
url = window.location.href;
}
let hash = location.hash;
url = url.replace(hash, '');
for(const ParamNameKey in NameValuePairs){
if(NameValuePairs.hasOwnProperty(ParamNameKey)){
let paramName = encodeURIComponent(ParamNameKey);
if (url.indexOf(paramName + "=") >= 0)
{
let prefix = url.substring(0, url.indexOf(paramName + "="));
let suffix = url.substring(url.indexOf(paramName + "="));
suffix = suffix.substring(suffix.indexOf("=") + 1);
suffix = (suffix.indexOf("&") >= 0) ? suffix.substring(suffix.indexOf("&")) : "";
url = prefix + paramName + "=" + NameValuePairs[ParamNameKey] + suffix;
}
else
{
if (url.indexOf("?") < 0)
url += "?" + paramName + "=" + NameValuePairs[ParamNameKey];
else
url += "&" + paramName + "=" + NameValuePairs[ParamNameKey];
}
}
}
if(newTab == true){
window.open((url + hash), '_blank');
}else if(get_string === true){
return (url + hash);
}else{
window.location.href = url + hash;
}
}
SELECT
email,
COUNT(email) AS cnt
FROM
contacts
GROUP BY email
HAVING cnt > 1;
Find duplicate values in multiple columns:
SELECT
first_name, COUNT(first_name) as cnt_fn,
last_name, COUNT(last_name) as cnt_ln,
email, COUNT(email) as cnt_m
FROM
users
GROUP BY
first_name ,
last_name ,
email
HAVING cnt_fn > 1
AND cnt_ln > 1
AND cnt_m > 1;