Picture deletion for laravel-admin

There is a deep doubt about laravel-admin's picture upload mechanism. Deleting avatar pictures on the user information page will cause an error. It was 1.4 at that time, and later updated 1.5 found that the delete button disappeared directly. If you use image normally in the form during the process of use, the slightly more complex writing (for example, when I write $form->image in the tab) will not work.

Write a way to solve this problem. (I don't know if it's suitable or not.)


namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Carbon\Carbon;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Form\Field\File;
use Illuminate\Http\UploadedFile;

class FileController extends Controller
    use ModelForm;

    public function index($type,$file=null,$ajax=true,$file_name="")
        $file = $file ? $file : $_FILES['img'];

            $data = array('status'=>false,'msg'=>trans('admin::lang.Upload_error'));
            return $ajax ? json_encode($data) : $data;

        //Get the file name
        $name = $file['name'];
        $img_type = strtolower(substr($name,strrpos($name,'.')+1)); //Gets the file type and converts it to lowercase
        $allow_type = array('jpg','jpeg','gif','png'); //Define the type of upload allowed
//Determine if file type is allowed to upload
        if(!in_array($img_type, $allow_type)){
            $data = array('status'=>false,'msg'=>trans('admin::lang.imgtype_error').$img_type);
            return $ajax ? json_encode($data) : $data;
//Determine if uploaded via HTTP POST
            $data = array('status'=>false,'msg'=>trans('admin::lang.post_img'));
            return $ajax ? json_encode($data) : $data;
        $file_name = $file_name ? $file_name.'.'.$img_type : md5(uniqid()).Carbon::now()->timestamp.'.'.$img_type;

            $upload_path = public_path().'/upload/goods/attr_img/'; //Storage path of uploaded files
            $path = "goods/attr_img/";
            $upload_path = public_path().'/upload/goods/'; //Storage path of uploaded files
            $path = "goods/";
            $upload_path = public_path().'/upload/'.$type.'/'; //Storage path of uploaded files
            $path = $type."/";
//Start moving files to the appropriate folder
            $data['status'] = true;
            $data['path'] = $path.$file_name;
            $data['view_path'] = config('admin.upload.host').$path.$file_name;
            $data = array('status'=>false,'msg'=>trans('admin::lang.moveimg_error'));
            return $ajax ? json_encode($data) : $data;
            return json_encode($data);
            return $data;

    public function multipleImg($type,$files,$ajax=true){
        $imgs = array('status'=>true);
            $file['name'] = $files['name'][$i];
            $file['type'] = $files['type'][$i];
            $file['tmp_name'] = $files['tmp_name'][$i];
            $file['error'] = $files['error'][$i];
            $file['size'] = $files['size'][$i];
            $data = $this->index($type,$file,false);
                $imgs['path'][$i] = $data['path'];
                $imgs['view_path'][$i] = $data['view_path'];

                return $ajax ? json_encode(array('status'=>false,'msg'=>$data['msg'])) : array('status'=>false,'msg'=>$data['msg']);
        return $ajax ? json_encode($imgs) : $imgs;

Then write this in the form:


Create a new method after the form method to delete the data in the database

    public function deleteUrl($img){
        $mconfig = MConfigModel::where('img',$img)->first();
        $path = config('admin.upload.host').$mconfig->val;
            @unlink ($path);
        $mconfig->val = "";
        return array('status'=>true);

Finally, add the route:


Keywords: Laravel PHP Database

Added by sansoo on Sat, 18 Jul 2020 17:36:44 +0300