Laravel 8 ErrorException:试图访问类型为null的值的数组偏移量



我正在与Laravel合作开发我的项目,基本上我想在我的主题中编辑数据库中的一些文章,所以我创建了一个名为edit.blade.php的刀片,在这个刀片中,有一行可以获得文章的当前图像:

<div class="row">
@foreach($article->images['images'] as $key => $image) 
<div class="col-sm-2">
<label class="control-label">
{{$key}}
<input type="radio" name="imagesThumb" value="{{ $image }}" {{ $article->images['thumb'] ? 'checked' : '' }} />
<a href="{{$image}}"><img src="{{$image}}" width="100%"></a>
</label>
</div>
@endforeach
</div>

文章模型也是这样的:

class Article extends Model
{
use HasFactory;
use Sluggable;
protected $guarded = [];
protected $casts = [
'images' => 'array'
];
public function sluggable()
{
return [
'slug' => [
'source' => 'title'
]
];
}
public function path()
{
return "/article/$this->slug";
}
}

这里是ArticleController编辑方法,它调用刀片:

public function edit(Article $article)
{
return view('website.backend.articles.edit',compact('article'));
}

现在这个问题是,每当我想去这个刀片时,它都会返回这个错误:

ErrorException试图访问类型为null的值的数组偏移量(视图:edit.blade.php(

它指的是这一行刀片:

@foreach($article->images['images'] as $key => $image)

所以我不知道为什么它会显示我这个错误,如果你知道的话,请告诉我,我真的很感激你们的任何想法。。。

提前谢谢。


更新#2:

这是我的存储ArticleController:方法

public function store(ArticleRequest $request)
{
//auth()->loginUsingId(1);
$imageUrl = $this->uploadImages($request->file('images'));
auth()->user()->article()->create(array_merge(['images' => $imageUrl], $request->all()));
return redirect(route('articles.index'));
}

这是UploadImages方法附带的AdminController,它由ArticleController:扩展

class AdminController extends Controller
{
protected function uploadImages($file)
{
$year = Carbon::now()->year;
$imagePath = "/upload/images/{$year}/";
$filename = $file->getClientOriginalName();
$file = $file->move(public_path($imagePath), $filename);
$sizes = ["300","600","900"];
$url['images'] = $this->resize($file->getRealPath(), $sizes, $imagePath, $filename);
$url['thumb'] = $url['images'][$sizes[0]];
return $url;
}
private function resize($path, $sizes, $imagePath, $filename)
{
$images['original'] = $imagePath . $filename;
foreach($sizes as $size)
{
$images[$size] = $imagePath . "{$size}" . $filename;
Image::make($path)->resize($size, null, function($constraint){
$constraint->aspectRatio();
})->save(public_path($images[$size]));
}
return $images;
}
}

更新#1:

dd($article);的输出为:

AppModelsArticle {#1234 ▼
#guarded: []
#casts: array:1 [▼
"images" => "array"
]
#connection: "mysql"
#table: "articles"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:12 [▼
"id" => 2
"user_id" => 1
"title" => "asdasd"
"slug" => "asdasd"
"description" => "asdsadas"
"body" => "asdsada"
"images" => "F:xampptmpphp1825.tmp"
"tags" => "asdsad"
"viewCount" => 0
"commentCount" => 0
"created_at" => "2020-11-09 12:24:33"
"updated_at" => "2020-11-09 12:24:33"
]
#original: array:12 [▼
"id" => 2
"user_id" => 1
"title" => "asdasd"
"slug" => "asdasd"
"description" => "asdsadas"
"body" => "asdsada"
"images" => "F:xampptmpphp1825.tmp"
"tags" => "asdsad"
"viewCount" => 0
"commentCount" => 0
"created_at" => "2020-11-09 12:24:33"
"updated_at" => "2020-11-09 12:24:33"
]
#changes: []
#classCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
}

这是因为这行:

auth()->user()->article()->create(array_merge(['images' => $imageUrl], $request->all()));

$request->all()包含一个键images,该键被设置为临时路径(F:xampptmpphp1825.tmp(,并且在array_merge中,后面的参数中的值将覆盖前面的参数。要修复它,只需像这样交换参数:

auth()->user()->article()->create(array_merge($request->all(), ['images' => $imageUrl]));

最新更新