[Seri] - Website đồng hồ - Upload hình ảnh sản phẩm - Hiển thị ảnh sản phẩm

Bài này mình sẽ xử lý phần upload hình ảnh sản phẩm và hiển thị ảnh sản phẩm

Ở bài trước mình đã tạo hàm upload ảnh  và bài này mình sẽ xử lý nó để upload ảnh sản phẩm. Và mình cũng đã tạo thêm một hàm để path cái link ảnh thành một đường dẫn cho đúng với thư mục để hiển thị

Hàm xử lý hiển thị hình ảnh

if (!function_exists('pare_url_file')) {
    function pare_url_file($image,$folder = '')
    {
        if (!$image)
        {
            return'/images/no-image.jpg';
        }
        $explode = explode('__', $image);

        if (isset($explode[0])) {
            $time = str_replace('_', '/', $explode[0]);
            return '/uploads/'.$folder.'/' . date('Y/m/d', strtotime($time)) . '/' . $image;
        }
    }
}

Các bạn thêm hàm này vào file function.php nhé

Controller xử lý 

Các bạn mở AdminProductController lên và thêm phần xử lý sau 

Insert

public function store(AdminRequestProduct $request)
    {
        $data = $request->except('_token','pro_avatar');
        $data['pro_slug']     = Str::slug($request->pro_name);
        $data['created_at']   = Carbon::now();

        if ($request->pro_avatar) {
            $image = upload_image('pro_avatar');
            if ($image['code'] == 1) 
                $data['pro_avatar'] = $image['name'];
        } 

        $id = Product::insertGetId($data);

        return redirect()->back();
    }

Phần này để xử lý lúc insert nhé

Update

public function update(AdminRequestProduct $request, $id)
    {
        $product           = Product::find($id);
        $data               = $request->except('_token','pro_avatar');
        $data['pro_slug']     = Str::slug($request->pro_name);
        $data['updated_at'] = Carbon::now();

        if ($request->pro_avatar) {
            $image = upload_image('pro_avatar');
            if ($image['code'] == 1) 
                $data['pro_avatar'] = $image['name'];
        } 

        $product->update($data);
        return redirect()->back();
    }

Và phần này là phần update 

View

Phần hiển thị các bạn xem ở video nhé

Video

Để lại comment của bạn nếu gặp khó khăn

Bài viết liên quan