Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.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_Eloquent_Laravel 5_Mutators - Fatal编程技术网

Php 如何在存储到数据库之前自定义模型的字段

Php 如何在存储到数据库之前自定义模型的字段,php,laravel,eloquent,laravel-5,mutators,Php,Laravel,Eloquent,Laravel 5,Mutators,我有一个这样的模型: class Article extends Model { protected $fillable = [ 'title', 'body', 'img_profile', // store name of image ]; } 还有这样一个控制器: public function store(ArticleRequest $request){ $article = Auth::user()->articles()->cr

我有一个这样的模型:

class Article extends Model {
protected $fillable = [
    'title',
    'body',
    'img_profile', // store name of image
    ];
}
还有这样一个控制器:

public function store(ArticleRequest $request){
    $article = Auth::user()->articles()->create($request->all());
    return redirect('articles');
}
{!! Form::model($article = new \App\Article,['url' => 'articles','files' => true ]) !!}
{!! Form::text('title', null ) !!}
{!! Form::textarea('body', null) !!}
{!! Form::file ('img_profile', '') !!}
{!! Form::submit('Submit') !!}
{!! Form::close() !!}
还有这样一种形式:

public function store(ArticleRequest $request){
    $article = Auth::user()->articles()->create($request->all());
    return redirect('articles');
}
{!! Form::model($article = new \App\Article,['url' => 'articles','files' => true ]) !!}
{!! Form::text('title', null ) !!}
{!! Form::textarea('body', null) !!}
{!! Form::file ('img_profile', '') !!}
{!! Form::submit('Submit') !!}
{!! Form::close() !!}
当我提交表单时,img_profile字段存储在默认的缓存文件中,即/private/var/tmp/phpceBMP2。但是我只想更新img_profile中的文件名

在将img_配置文件请求存储到数据库中之前,我如何更改其值?

您应该使用以下方式来创建文章模型:

public function setImgProfileAttribute($value)
{
    $this->attributes['img_profile'] = '/private/var/tmp/phpceBMP2/'. $value;
}
但是,如果您真的希望以这种方式在DB中存储路径,您应该考虑。如果你将来想改变它呢?您必须更新所有记录?

默认情况下,Laravel会将img_配置文件值保存为/private/var/tmp/phpceBMP2,现在我想将其自定义为图像的名称,如img_文件名。