Sau khi lấy code về các bạn bật Shell di chuyển đến project và chạy lệnh sau:
- Tải thư viện từ server composer:
composer install- Tạo một database mới tên là "demo0517e"
- Mở file ".env" sửa thông tin kết nối đến database
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=demo0517e
DB_USERNAME=root
DB_PASSWORD=- Sau đó chạy lệnh để hệ thống generate key:
php artisan key:generate- Tiếp theo chạy lệnh để hệ thống sinh ra database:
php artisan migrateSau khi cài đặt xong truy cập localhost/demo0517e/public/index.php để kiểm tra kết quả.
- Chạy lệnh:
php artisan make:migration create_products_table --create=products- Mở file "create_products_table" và sửa nội dung như sau:
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('description')->nullable();
$table->text('content')->nullable();
$table->string('thumbnail')->nullable();
$table->float('price')->default(0);
$table->string('status')->default('0');
$table->timestamps();
});
}- Tiếp theo chạy lệnh để hệ thống sinh ra database:
php artisan migrate- Chạy lệnh:
php artisan make:model Product- Sử dụng tinker để test, gõ lệnh sau:
php artisan tinker
$p = new App\Product();
$p->name = "San pham 1";
$p->save();
exit- Chạy lệnh:
php artisan make:controller ProductController --resource- Mở file "ProductController.php" và thêm nội dung function index
use Illuminate\Http\Request;
use App\Product; //Nhớ thêm thư viện
class ProductController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$products = Product::all();
return view('product.index', [ 'products' => $products ]);
}
....................
}- Tạo file view nằm trong đường dẫn "resources/views/product/index.blade.php" với nội dung như sau:
<table border="1">
<tr>
<th>ID</th>
<th>Name</th>
</tr>
@if(isset($products))
@foreach($products as $item)
<tr>
<td>{{ $item->id }}</td>
<td>{{ $item->name }}</td>
</tr>
@endforeach
@endif
</table>- Mở file "routes/web.php" thêm dòng sua:
Route::get('/', function () {
return view('welcome');
});
//thêm dòng này
Route::get('product', 'ProductController@index');