下面是我的控制器Admin/MoviesController的'store'方法示例.它看起来已经很大了,"更新"方法会更大.
算法是:
我的问题:
有一些最佳做法吗?我说的不是MVC,而是Laravel的功能.目前为了保持一些逻辑,我只使用Request for validation.
public function store(CreateMovieRequest $request) {
$movie = Movies::create($request->except('poster'));
/* Uploading poster */
if ($request->hasFile('poster')) {
$poster = \Image::make($request->file('poster'));
$poster->fit(250, 360, function ($constraint) {
$constraint->upsize();
});
$path = storage_path() . '/images/movies/'.$movie->id.'/';
if(! \File::exists($path)) {
\File::makeDirectory($path);
}
$filename = time() . '.' . $request->file('poster')->getClientOriginalExtension();
$poster->save($path . $filename);
$movie->poster = $filename;
}
/* If 'Meta Title' is empty, then fill it with the name of the movie */ …Run Code Online (Sandbox Code Playgroud)在我的用户模型中,我有这样的规则:
protected $fillable = ['name', 'email', 'password'];
Run Code Online (Sandbox Code Playgroud)
我想在我的模型中添加一个字段"role_id",它只能在控制器或模型中分配,但在其他情况下应该被忽略.
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'role_id' => $this->role_id
]);
}
Run Code Online (Sandbox Code Playgroud)