composer create-project laravel/laravel ${PROJECT_NAME} 5.6
cd ${PROJECT_NAME}
npm install
# example with ownership
# SERVER_USER=http
chown -R ${USER_NAME}:${SERVER_USER}
# use `find` to change permissions
find . -type d -exec chmod 775 {} \;
find . -type f -exec chmod 664 {} \;
# create database
CREATE DATABASE `${PROJECT_NAME}`;
# create user
CREATE USER '${PROJECT_NAME}' IDENTIFIED BY '${PROJECT_NAME}';
# assign permission
GRANT ALL PRIVILEGES ON ${PROJECT_NAME}.* TO '${PROJECT_NAME}'@'localhost';
# dont forget
FLUSH PRIVILEGES;
APP_NAME=${PROJECT_NAME}
APP_URL=${PROJECT_URL} # only do this if you use a custom /etc/hosts file or have a domain for it
# don't ever do this in production (or anything exposed to the internet really)
DB_DATABASE=${PROJECT_NAME}
DB_USERNAME=${PROJECT_NAME}
DB_PASSWORD=${PROJECT_NAME}
php artisan make:auth
to remove default registration change /routes/web.php
to
<?php
// remove the lines:
/*
Route::get('/', function() {
return view('welcome');
});
Auth::routes();
*/
// keep the line (or change the path to '/')
Route::get('/home', 'HomeController@index')->name('home');
// Authentication
Route::get('login', 'Auth\LoginController@showLoginForm')->name('login');
Route::post('login', 'Auth\LoginController@login');
Route::post('logout', 'Auth\LoginController@logout')->name('logout');
// Password Reset Routes
Route::get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.request');
Route::post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('password.email');
Route::get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset');
Route::post('password/reset', 'Auth\ResetPasswordController@reset');
# also see https://laravel.com/docs/5.6/migrations#generating-migrations
# example:
php artisan make:model ${MODEL_NAME} -m
setup the models
<?php
// see https://laravel.com/docs/5.6/eloquent-relationships#querying-relationship-existence for more
class User extends Model {
protected $fillable = [
// use this for auto assignable properties (ussualy all column names)
'email', 'password'
];
// OR (NOT BOTH!)
protected $guarded = [
// use this for the reverse of $fillable. These properties will not be auto assigned
'stuff_that_does_not_matter_for_the_applicatoin'
];
protected $hidden = [
// properties that should never be exposed (they will never be found in serialized output)
'password'
];
// to setup relations:
// one to one
public function type() {
// this declares that this model has a relation with ONE App\Type model. This is inferred by the 'type_id' column in the App\User table
return $this->hasOne('App\Type');
}
// one to many
public function posts() {
// this declares that this model has a relation with MANY App\Post model. This is inferred by the 'user_id' column ON the App\Post table!
return $this->hasMany('App\Post');
}
// one to many (inversed)
/*
ON THE App\Post model
public function user() {
// this declares that this model has a ownership relation with ONE App\User model. This is inferred by the 'user_id' coluiimn on the App\Post table!
return $this->belongsTo('App\User');
}
*/
// REMEMBER that these relations can be used as properties (getters)
/*
$user = User::find(1);
foreach($user->posts as $post) {
echo $post;
}
*/
// many to many
public function roles() {
// this is inferedd by a (alphabetically sorted -> 'role_user' table) relation table that maps 'role_id' to 'user_id'.
return $this->belongsToMany('App\Roles');
}
// many to many (inverse)
/*
// ON THE App\Role model
public function users() {
return $this->belongsToMany('App\User');
}
*/
// to create computed/proxied properties: https://laravel.com/docs/5.6/eloquent-mutators
public function getPeriod() {
// assume this model has 'period_start' DateTime and 'period_end' DateTime
return $this->period_start->diff($this->period_end);
}
}
setup the migrations
- create the correct table name for each model (remember the naming conventions and use
php artisan make:model -m
where possible) - create the relation tables in between (remember the alphabetical sorted name 'a_z') to define relations in migrations use
<?php
class ExampleTable extends Migration
{
// ...
public function up()
{
Schema::create('examples', function (Blueprint $table) {
$table->increments('id'); // primary key
$table->string('some_column');
$table->date('some_date'); // see https://laravel.com/docs/5.6/migrations#Available-Column-Types
// how to setup a foreign key:
$table->unsignedInteger('other_id')->references('id')->on('others');
$table->unsignedInteger('unique_other_id')->unique->references('id')->on('others');
// relation table:
// REMEMBER NO UNIQUE
$table->unsigendInteger('thing_a_id')->references('id')->on('thing_a');
$table->unsigendInteger('thing_b_id')->references('id')->on('thing_b');
$table->timestamps(); // created_at & updated_at timestamps
});
}
// ...
}
see https://laravel.com/docs/5.6/routing#named-routes
see https://laravel.com/docs/5.6/authorization this should be really easy for basic CRUD on models
php artisan make:policy UserPolicy --model=User
to generate data see: https://laravel.com/docs/5.6/database-testing#writing-factories
to seed the database with that data see: https://laravel.com/docs/5.6/seeding