Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 重命名图像,然后将图像名称保存到数据库中_Php_Laravel 5.2_Intervention - Fatal编程技术网

Php 重命名图像,然后将图像名称保存到数据库中

Php 重命名图像,然后将图像名称保存到数据库中,php,laravel-5.2,intervention,Php,Laravel 5.2,Intervention,我想从用户那里获取图像,然后重命名它,然后我想将重命名后的图像名称保存到数据库中。这是我的控制器代码。我正在使用干预软件包。重命名后,我可以将照片正确保存到目标文件夹,但重命名后,我无法将照片的名称保存到数据库中。代码是什么 public function store(UserRequest $request) { $farmer = User::create([ 'name' => $request->name

我想从用户那里获取图像,然后重命名它,然后我想将重命名后的图像名称保存到数据库中。这是我的控制器代码。我正在使用干预软件包。重命名后,我可以将照片正确保存到目标文件夹,但重命名后,我无法将照片的名称保存到数据库中。代码是什么

public function store(UserRequest $request)
    {
        $farmer = User::create([
            'name'            =>  $request->name,
            'phone'           =>  $request->phone,
            'address'         =>  $request->address,
            'nid'             =>  $request->nid,
            'dob'             =>  $request->dob,
            'remarks'         =>  $request->remarks,
            'division_id'     =>  $request->division_id,
            'district_id'     =>  $request->district_id,
            'upazila_id'      =>  $request->upazila_id,
            'farmer_point_id' =>  $request->farmer_point_id,
            'user_type_id'    =>  3   // 3 is for farmer
        ]);
        $image = Image::make($request->profile_picture);
        $image->resize(250, 272);
        $image->save(public_path("uploads/Farmers/farmer_$farmer->id.jpg"));

        return redirect("farmer/{$farmer->id}");
    }

理想的做法是先上传图像,然后将文件路径保存到数据库

 $data['phone'] = $request->name,
    $data['address'] = $request->address
    // Add other data then pass it into User::create()
理想情况下,最好将上传逻辑提取到单独的独立类中。您可以使用以下内容作为指导

<?php
Class UploadImage
{
 /**
     * UploadPostImage constructor.
     * .
     * @param UploadedFile $postedImage
     *
     */
    public function __construct(UploadedFile $postedImage)
    {
        $this->postedImage = $postedImage;
    }

 /**
     * Create the filename
     *
     */
    public function getFilename()
    {
        $dt = Carbon::now();
        $timestamp = $dt->getTimestamp();

        $this->filename = $timestamp . '_' . $this->postedImage->getClientOriginalName();
    }

 /**
     * Create the image and return the path
     *
     * @param $path
     * @param int $width
     * @return mixed
     */
    public function createImage($path, $width = 400)
    {
        // Upload the image
        $image = Image::make($this->postedImage)
            ->resize(250, 272);

        $image->save(public_path($path . $this->filename, 60));

        return $path . $this->filename;
    }
}
//将其他数据添加到$data数组中,然后保存到数据库

 $data['phone'] = $request->name,
    $data['address'] = $request->address
    // Add other data then pass it into User::create()
在控制器中调用createImage()时,路径将返回给您,您可以将其保存在数据库中。 我希望这有帮助