<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class PrimaryAccessories extends Model
{
    use HasFactory;

    private static $information, $image, $ImageNewName, $directory, $imgUrl;

    public static function saveNewData($request) // to save new information
    {
        self::$information = new PrimaryAccessories();

        self::$information->name = $request->name;
        self::$information->description = $request->description;
        self::$information->image = self::saveImage($request); // to save image
        self::$information->save();
    }
    public static function updateData($request)
    {
        self::$information = PrimaryAccessories::find($request->id);

        self::$information->name = $request->name;
        self::$information->description = $request->description;
        if ($request->image)
        {
            self::$information->image = self::saveImage($request);
        }
        self::$information->save();
    }

    private static function saveImage($request) // to save image
    {
        self::$image = $request->file('image'); // get only image form request.
        self::$ImageNewName = rand().".".self::$image->extension(); // make new name for image ("rand" use for random number and "extension" use for image extension)
        self::$directory ='image/';    // to make folder to contain image.
        self::$imgUrl = self::$directory.self::$ImageNewName; // concede image name and directory to make image path which i store "$imgUrl".
        self::$image -> move(self::$directory, self::$ImageNewName); // to move image
        return self::$imgUrl;
    }
}
