iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 6
1
Software Development

PHP新手30天實戰金流系列 第 6

[Day6] Shopping Cart - Views - Controller - DB

[Day6] Shopping Cart - Views - Controller - DB

tags: PHP新手30天實戰金流, Laravel6

Step 1 DB

  • create products table

    $table->string('name');
    $table->string('description');
    $table->float('price');
    $table->string('imageurl');
    $table->string('file_id');
    
  • create files table

    $table->string('filename');
    $table->string('mime');
    $table->string('original_filename');
    
  • update users table

    1. 新增 migration table: php artisan make:migration add_admin_to_user_table --table="users"
    2. 增加 admin 欄位
    $table->boolean('admin');
    
    1. 在 mysql中:Update users set admin=1 where name='sarah'; 將管理者的 admin 欄位設為 1

Step 2

  • 讓存取管理頁面之前,先通過身份驗證的中介層。我們在這裡做不同使用者的分流。
    • routes/web.php 中的幾個路由放進中介層的群組
    Route::get('/admin/product/new', function () {
        $user = auth()->user();
        if ($user->admin ==true){
            return redirect()->action('ProductController@newProduct');
        }else{
            return redirect('customer/shopping_mall');
        }
    });
    
    Route::get('/admin/product/new/test', 'ProductController@newproduct');
    

Step 3 管理者如何新增商品

  • Product Controller

    namespace App\Http\Controllers;
    
    use App\Product;
    use Request;
    use App\Http\Controllers\Controller;
    
    use Illuminate\Support\Facades\Storage;
    use Illuminate\Support\Facades\File;
    

    class ProductController extends Controller {} 中, 加入以下方法:

        public function index(){
            $products = Product::all();
            return view('admin.products',['products' => $products]);
        }
    
        public function destroy($id){
            Product::destroy($id);
            return redirect('/admin/products');
        }
    
        public function newProduct(){
            return view('admin.new');
        }
    
        public function add() {
    
            $file = Request::file('file');
            $extension = $file->getClientOriginalExtension();
            Storage::disk('local')->put($file->getFilename().'.'.$extension,  File::get($file));
    
            $entry = new \App\File();
            $entry->mime = $file->getClientMimeType();
            $entry->original_filename = $file->getClientOriginalName();
            $entry->filename = $file->getFilename().'.'.$extension;
    
            $entry->save();
    
            $product  = new Product();
            $product->file_id=$entry->id;
            $product->name =Request::input('name');
            $product->description =Request::input('description');
            $product->price =Request::input('price');
            $product->imageurl =Request::input('imageurl');
    
            $product->save();
    
            return redirect('/admin/products');
    
        }
    

    上面所使用的 Request 是我們在 /config/app.php 中設定 aliases 的路徑:'Request' => Illuminate\Support\Facades\Request::class,, 非是 use Illuminate\Http\Request;

  • 第六天網站成果是

  1. 管理者可以新增商品並瀏覽
  2. 管理者和顧客登入後會分流到不同頁面
  • 這幾天稍微玩過之後,明天要認真學習框架用法了!(這樣順序好像有點怪XD)

  • 和大家分享一句很棒的話^^
    "你不需要很厲害才能開始,但你需要開始才會很厲害"
    明天要

晚生學習分享所學經驗,若內容有誤或不清楚,煩請不吝指教!更是歡迎各位大神多多補充,感謝萬分!


上一篇
[Day5] FRONTEND(blade) + Route name
下一篇
[Day7] Laravel 架構理解( IoC, Service Provider)
系列文
PHP新手30天實戰金流34
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
linteddy
iT邦新手 5 級 ‧ 2021-02-21 15:47:15

不好意思請問可以請教您幾個問題嗎?
1.function newProduct 裡面的 admin.new 在哪?
2.function destroy 裡面的 /admin/products 在哪?
3.想了解一下為啥我的 function add 裡面的 save 跟 input 都會報錯誤
圖片:https://ithelp.ithome.com.tw/upload/images/20210221/20135157UKD2jj0AK5.jpg
4.請問有github 或是範例檔案嗎?
謝謝您囉 對這系列很有興趣 但是是新手 QAQ
看到您說的 不用等很厲害在學 要學了才會厲害哈哈

我要留言

立即登入留言