Hướng dẫn view detail php - xem chi tiết php

View detail database

  • Bài học trước chúng ta đã biết cách tạo một trang xem, liệt kê tất cả dữ liệu của một table như thế nào rồi, bài này sẽ giúp chúng ta xem [view] chi tiết của một dữ liệu cụ thể dựa theo id.
  • Kết quả bài trước ta có trang hiển thị bảng dữ liệu sau //localhost:82/admin/news://localhost:82/admin/news:

{{$row->title}} {{$row->email}} Edit | Delete @endforeach

  • Reload lại trang //localhost:82/admin/news ta sẽ nhận được kết quả sau://localhost:82/admin/news ta sẽ nhận được kết quả sau:

Thêm nội dung Controller

Thêm nội dung cho function show[$id] của Controller AdminNewsController để gọi trang view xem danh sách:

/**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show[$id]
    {
        $news = News::where['id', '=', $id]->select['*']->first[];
        $des = html_entity_decode[$news->description];
        return view['/admin/news_detail', compact['news', 'des']];
    }

  • $news = News::where['id', '=', $id]->select['*']->first[];: chọn dữ liệu đúng với điều kiện id bằng với id nhận được.
  • $des = html_entity_decode[$news->description];: decode cho dữ liệu description, tránh trường hợp dữ liệu có chữa mã html [rảnh quá viết thêm cho các bạn hình dung được cách viết thôi ^^].
  • return view['/admin/news_detail', compact['news', 'des']];: gom dữ liệu trả về trang news_detail.

Tạo trang chứa thông tin chi tiết /resources/views/admin/news_detail.blade.php/resources/views/admin/news_detail.blade.php

Trang này chứa các giá trị trả về từ Controller, có nội dung sau:

News

{{ $news->title }}

{{ $news->email }}

{!! $des !!}

Chú ý: {!! $des !!}: cách viết hơi khác biết so với thông thường, cách viết này có thể xuất ra nội dung có chứa các mã HTML hay JS.

Thêm nội dung Route

Ta thêm vào Route /routes/web.php nội dung sau:/routes/web.php nội dung sau:

Route::get['/admin/news/{id}', 'Admin\AdminNewsController@show'];

  • /**
         * Display the specified resource.
         *
         * @param  int  $id
         * @return \Illuminate\Http\Response
         */
        public function show[$id]
        {
            $news = News::where['id', '=', $id]->select['*']->first[];
            $des = html_entity_decode[$news->description];
            return view['/admin/news_detail', compact['news', 'des']];
        }
    0: đường dẫn trang detail có chứa id.
  • /**
         * Display the specified resource.
         *
         * @param  int  $id
         * @return \Illuminate\Http\Response
         */
        public function show[$id]
        {
            $news = News::where['id', '=', $id]->select['*']->first[];
            $des = html_entity_decode[$news->description];
            return view['/admin/news_detail', compact['news', 'des']];
        }
    1: thư mục
    /**
         * Display the specified resource.
         *
         * @param  int  $id
         * @return \Illuminate\Http\Response
         */
        public function show[$id]
        {
            $news = News::where['id', '=', $id]->select['*']->first[];
            $des = html_entity_decode[$news->description];
            return view['/admin/news_detail', compact['news', 'des']];
        }
    2 chứa Controller AdminNewsController.
  • /**
         * Display the specified resource.
         *
         * @param  int  $id
         * @return \Illuminate\Http\Response
         */
        public function show[$id]
        {
            $news = News::where['id', '=', $id]->select['*']->first[];
            $des = html_entity_decode[$news->description];
            return view['/admin/news_detail', compact['news', 'des']];
        }
    4: Đây là function
    /**
         * Display the specified resource.
         *
         * @param  int  $id
         * @return \Illuminate\Http\Response
         */
        public function show[$id]
        {
            $news = News::where['id', '=', $id]->select['*']->first[];
            $des = html_entity_decode[$news->description];
            return view['/admin/news_detail', compact['news', 'des']];
        }
    5 trong Controller AdminNewsController.

Hiển thị trang xem chi tiết dữ liệu:

Mở trang //localhost:82/admin/news lên trình duyệt, click vào link bất kỳ ta sẽ xem được kết quả sau://localhost:82/admin/news lên trình duyệt, click vào link bất kỳ ta sẽ xem được kết quả sau:

Tới đây ta đã hoàn thành tạo trang xem chi tiết dữ liệu, bài tiếp theo chúng ta sẽ học về cập nhật dữ liệu một sản phẩm bất kỳ.

Bài Viết Liên Quan

Chủ Đề