- email:
nur@gmail.com
password:secret
- email:
mahibur@gmail.com
password:secret
- email:
farhad@gmail.com
password:secret
- email:
majedul@gmail.com
password:secret
- email:
tahmina@gmail.com
password:secret
function is_email($email) {
$pattern = '/.+@.+\..+/';
return preg_match($pattern, $email);
}
function is_authenticate() {
return isset($_SESSION['user']);
}
function is_email_taken($email) {
$user = User::where('email', $email)->first();
return empty($user) ? false : true;
}
php part
$errors = [];
$old_values = [
'name' => '',
'email' => '',
'password' => '',
];
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];
$old_values = [
'name' => $name,
'email' => $email,
'password' => $password,
];
if (strlen($name) < 4) {
$errors['name'] = "name value can't be less than 4 character";
}
if (strlen($password) < 4) {
$errors['password'] = "password value can't be less than 4 character";
}
if (!is_email($email)) {
$errors['email'] = "please pass a valid email address";
} else if (is_email_taken($email)) {
$errors['email'] = "email address already taken";
}
if (empty($errors)) {
$user = new User();
$user->name = $name;
$user->email = $email;
$user->password = password_hash( $password, PASSWORD_BCRYPT);
$user->save();
$_SESSION['user'] = $user;
header('Location: /');
}
}
html part
<form method="post">
<div class="form-group">
<label for="name">Name</label>
<input value="<?php echo $old_values['name'] ?>" type="text" name="name" id="name" class="form-control">
</div>
<?php if (isset($errors['name'])): ?>
<p class="text-danger"><?php echo $errors['name'] ?></p>
<?php endif ?>
<div class="form-group">
<label for="email">Email</label>
<input value="<?php echo $old_values['email'] ?>" type="text" name="email" id="email" class="form-control">
</div>
<?php if (isset($errors['email'])): ?>
<p class="text-danger"><?php echo $errors['email'] ?></p>
<?php endif ?>
<div class="form-group">
<label for="password">Password</label>
<input value="<?php echo $old_values['password'] ?>" type="password" name="password" id="password" class="form-control">
</div>
<?php if (isset($errors['password'])): ?>
<p class="text-danger"><?php echo $errors['password'] ?></p>
<?php endif ?>
<div class="form-group">
<p>
Already have account <a href="/login">Login here</a>
</p>
<button class="btn btn-outline-primary" type="submit">Register</button>
</div>
</form>
$user = User::find($_SESSION['user']->id);
$id = $_GET['id'];
$post = Post::find($id);
$post->delete();
header('location: /dashboard/post?delete=true');
<a onclick="return confirm('Are you sure you want to delete this entry')" href="/dashboard/post/delete?id=<?php echo $post->id ?>" class="btn btn-outline-danger"><i class="fa fa-trash"></i> Delete</a>
========================================
Model::all();
Model::find(id);
// relation between User and Post Model
// inside User model
public function posts()
{
return $this->hasMany(Post::class);
}
// inside Post model
public function user () {
return $this->belongsTo(User::class);
}
__DIR__
table order is important
some table will depend on other table so when we create table dependent table will be created first
When we drop table order will be reverse
$t->integer('post_id')->unsigned();
$t->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
range function will give a array in between range
range(1, 5) // [1, 2, 3, 4, 5];
rand function give us random number in between 2 numbers
rand(10, 15) // it will give a number in between 10 and 15
$path = parse_url( trim( $_SERVER['REQUEST_URI'], '/' ), PHP_URL_PATH);
to generate fake data we are using faker
Install faker
composer require fzaninotto/faker
use Faker
use Faker\Factory;
$faker = Factory::create();
Carbon date library has lot of helper function to work with data
Installing Carbon
composer require nesbot/carbon
Using carbon
use Carbon\Carbon
Carbon::now()->format("Y-m-d H:i:s")
using carbon method with created_at field
$post->created_at->diffForHumans()