OS : Mac
Runtime : Homestead
1. New project
A. If you create a new project
composer create-project laravel/laravel sample --prefer-dist -vvv
B. If you clone a project from git , Then install laravel
composer install
2. Set project
A. Edit .env file
if it is not exist , use command to generate it
cp simple.env .env
Edit it
DB_DATABASE=db_name
B. Edit hosts file
Add 192.168.10.10 project_name.test to etc\hosts
C. Edit Homestead.yaml file
Add
- map: project_name.test
to: /home/vagrant/Code/project_name/public
to sites
Add
- project_db_name
to databases
D. Then reload Homestead
vagrant provision && vagrant reload
3. Generate app key(Sometimes it will be init auto)
php artisan key:generate
4. Create db
//database/migrations/[timestamp]_create_users_table.php
php artisan make:migration create_users_table --create="users"
php artisan migrate
5. Create Model
mkdir app/Models
php artisan make:model Models/User
6. Create controller and create route action
php artisan make:controller UsersController
public function index()
{
$users = User::paginate(10);
//跳轉(zhuǎn)到resources/views/users/index_blade.php,參數(shù)users
return view('users.index', compact('users'));
}
7. Create route view
resources/views/users/create.blade.php
@foreach ($users as $user)
{{ $user->name }}
@endforeach
8. Optimize view style
1. Install bootstrap
yarn install --no-bin-links
2. Edit app.scss
@import "node_modules/bootstrap-sass/assets/stylesheets/bootstrap";
3. Build app.scss
npm run dev
npm run watch-poll
4. Import style
<link rel="stylesheet" href="/css/app.css">
9. Set routes
//顯示所有
//對應(yīng)controller方法:index()
Route::get('/users', 'UsersController@index')->name('users.index');
//顯示詳情
//對應(yīng)controller方法:show(User $user)
Route::get('/users/{user}', 'UsersController@show')->name('users.show');
//創(chuàng)建信息的頁面
//對應(yīng)Controller方法:create()
Route::get('/users/create', 'UsersController@create')->name('users.create');
//創(chuàng)建操作
//對應(yīng)Controller方法:store(Request $request)
Route::post('/users', 'UsersController@store')->name('users.store');
//編輯信息的頁面
//對應(yīng)Controller方法:edit(User $user)
Route::get('/users/{user}/edit', 'UsersController@edit')->name('users.edit');
//更新操作
//對應(yīng)Controller方法:update(User $user, Request $request)
Route::patch('/users/{user}', 'UsersController@update')->name('users.update');
//刪除操作
//對應(yīng)Controller方法:destroy(User $user)
Route::delete('/users/{user}', 'UsersController@destroy')->name('users.destroy');