当前位置:网站首页>Splicing full paths and uploading multiple pictures of laravel admin when laravel uses OSS

Splicing full paths and uploading multiple pictures of laravel admin when laravel uses OSS

2022-06-26 11:06:00 siner. li

Add... To the model ( Method 1 )


	protected static function boot()
    {
    
        parent::boot();
        static::created(function ($modal) {
    
            //  Get the field value after adding 
            $url = $modal->getAttribute('gwt');
            $str = "https://www.baidu.com";
            $new_url = $str . '/' . $url;
            $modal->gwt = $new_url;
            $modal->save();
        });
    }

stay form Add ( Method 2 )

 // hold id Incoming via hidden domain 
 $form->hidden('id');
 $form->image('img', ' picture ')
 
 $form->saved(function (Form $form){
    
 	 //  To determine if there is  https://www.baidu.com  Field 
 	 if(strpos($form->model()->img, 'https://www.baidu.com') === false){
    
	     $url = $form->model()->img;
	     $img = "https://www.baidu.com/".$url;
	     //  adopt  use  introduce  Contracts  Model 
	     $info = Contracts::find($form->model()->id);
	     $info->img = $img;
	     $info->save();
     }
 });

Refer to other methods above , But it didn't work , And then add some code .


Use laravel-admin More pictures / Upload files

Splicing path – ( Do not delete pictures )

	//  Add... To the model , setImgAttribute In the middle of the Img This is the field name 
	public function setImgAttribute($value)
    {
    
    	//  Get the last element of the array 
        $value[count($value)-1] = 'https://www.baidu.com/' . $value[count($value)-1];
        //  Limit  3  A picture 
        if (count($value) > 3) {
    
            array_shift($value);
        }
        $this->attributes['img'] = implode(',', $value);
    }

	// getImgAttribute In the middle of the Img This is the field name 
    public function getImgAttribute($value)
    {
    
        return explode(',', $value);
    }

	//  stay  form  Use in  ,  there  img  The value is the field name 
	$form->multipleImage('img', ' Thumbnail upload ');
	
	//  Picture carousel 
	$grid->column('img', ' picture ')->carousel();

( Delete pictures )

	//  Add... To the model , setImgAttribute In the middle of the Img This is the field name 
	public function setImgAttribute($value)
    {
    
    	if (count($value) == 0) {
    
            $this->attributes['img'] = '';
            return;
        }
        //  Limit  3  A picture 
        if (count($value) > 3) {
    
            array_shift($value);
        }
        //  Get each element of the array 
        foreach ($value as &$v) {
    
            if (strpos($v, 'https://www.baidu.com/')) === false) {
    
                $v = 'https://www.baidu.com/' . $v;
            }
        }
        $this->attributes['img'] = implode(',', $value);
    }

	// getImgAttribute In the middle of the Img This is the field name 
    public function getImgAttribute($value)
    {
    
        return explode(',', $value);
    }

	//  stay  form  Use in  ,  there  img  The value is the field name 
	$form->multipleImage('img', ' Thumbnail upload ')->removable();
	
	//  Picture carousel 
	$grid->column('img', ' picture ')->carousel();
原网站

版权声明
本文为[siner. li]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206261004261898.html