laravel (1/1) ErrorException 未定义偏移量:1
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45657637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
(1/1) ErrorException Undefined offset: 1
提问by User57
I was trying to update my userprofile with the following controller but the problem is if i update only profile picture it shows the above error..But if i update every value it update successfully. How do i update the userProfile without updating every value :
我试图用以下控制器更新我的用户配置文件,但问题是如果我只更新配置文件图片,它会显示上述错误..但如果我更新每个值,它会成功更新。如何在不更新每个值的情况下更新 userProfile:
public function updateUser(Request $request)
{
$this->validate($request, [
'profile_picture' => 'dimensions:width=400,height=400',
'cover_picture' => 'dimensions:width=800,height=400',
'avatar' => 'dimensions:width=80,height=80',
]);
if (\Auth::check())
{
$user= User::find(\Auth::id());
}
$files= [];
if($request->file('profile_picture')) $files[] = $request->file('profile_picture');
if($request->file('cover_picture')) $files[] = $request->file('cover_picture');
if($request->file('avatar')) $files[] = $request->file('avatar');
foreach($files as $file)
{
if(!empty($file))
{
$filename = time().str_random(20). '.' . $file->getClientOriginalExtension();
$file->move('users/',$filename);
$filenames[]=$filename;
}
}
$user->profile_picture = $filenames[0];
$user->cover_picture = $filenames[1];
$user->avatar = $filenames[2];
$user->save();
return redirect::back()->with('Warning',"Profile Updated Successfully");
}
回答by Scuzzy
I don't think it's wise using a positional array like this, As you've discovered, what if someone only wants to update their avatar. I feel your assignment into $files[]
is redundant and you could go straight into your processing code.
我认为使用这样的位置数组是不明智的,正如您所发现的,如果有人只想更新他们的头像怎么办。我觉得你的赋值$files[]
是多余的,你可以直接进入你的处理代码。
Basically your current implementation means $files
can be of a variable length, how do you know which is 0, 1 or 2 etc ?
基本上,您当前的实现方式$files
可以是可变长度,您怎么知道哪个是 0、1 或 2 等?
With my approach, the code is now looping over each type of picture, and assigns it into the user with $user->$type
directly by the same matching type property.
使用我的方法,代码现在循环遍历每种类型的图片,并$user->$type
通过相同的匹配类型属性直接将其分配给用户。
foreach( array( 'profile_picture', 'cover_picture', 'avatar' ) as $type)
{
if( $request->file( $type ) )
{
$filename = time() . str_random(20) . '.' . $request->file( $type )->getClientOriginalExtension();
$request->file( $type )->move( 'users/', $filename );
$user->$type = $filename;
}
}
If you find you need to map a different $source to the $type variable, you could do this with an additional array index...
如果您发现需要将不同的 $source 映射到 $type 变量,则可以使用额外的数组索引来执行此操作...
foreach( array(
'profile_picture' => 'profile_picture',
'cover_picture' => 'cover_picture',
'avatar' => 'avatar'
) as $source => $type)
{
if( $request->file( $source ) )
{
$filename = time() . str_random(20) . '.' . $request->file( $source )->getClientOriginalExtension();
$request->file( $source )->move( 'users/', $filename );
$user->$type = $filename;
}
}
回答by ASK Arjun
I finally came up with a solution mate.
我终于想出了一个解决方案的伙伴。
You can try to Include a var_dump of $filenames. I suppose that $filenames[1] doesn't exist at all.
您可以尝试包含 $filenames 的 var_dump。我想 $filenames[1] 根本不存在。