Skip to content

tranducliem/0517e

Repository files navigation

Project hướng dẫn lớp PHP0517E

Hướng dẫn cài đặt project

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 migrate

Sau khi cài đặt xong truy cập localhost/demo0517e/public/index.php để kiểm tra kết quả.

Tạo file Migration Product

  • 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

Hướng dẫn tạo Model Product

  • 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

Hướng dẫn tạo Product Controller

  • 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 ]);
    }
    
    ....................
}

Hướng dẫn tạo Product View

  • 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>

Hướng dẫn tạo Route cho Product

  • 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'); 

About

Project for PHP0517E Class

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages