Laravel 8 ErrorException:尝试访问 null 类型值的数组偏移量
Laravel 8 ErrorException: Trying to access array offset on value of type null
我正在与 Laravel 一起开发我的项目,基本上我想根据我的主题从数据库中编辑一些 文章 ,所以我创建了一个 blade 称为 edit.blade.php
并且在此 blade 中,有一行获取文章的当前图像:
<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";
}
}
这是调用 blade:
的 ArticleController
edit 方法
public function edit(Article $article)
{
return view('website.backend.articles.edit',compact('article'));
}
现在的问题是,每当我想转到这个 blade 时,它 returns 这个错误:
ErrorException Trying to access array offset on value of type null
(View: edit.blade.php)
指的是blade这一行:
@foreach($article->images['images'] as $key => $image)
所以我不知道为什么会显示这个错误,如果你知道请告诉我,我非常感谢你们的任何想法...
提前致谢。
更新#2:
这是我 store 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);
的输出是:
App\Models\Article {#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:\xampp\tmp\php1825.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:\xampp\tmp\php1825.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()
包含一个设置为临时路径 (F:\xampp\tmp\php1825.tmp
) 的键 images
,在 array_merge 中,后面参数中的值会覆盖前面参数中的值。要修复它,只需像这样交换参数:
auth()->user()->article()->create(array_merge($request->all(), ['images' => $imageUrl]));
我正在与 Laravel 一起开发我的项目,基本上我想根据我的主题从数据库中编辑一些 文章 ,所以我创建了一个 blade 称为 edit.blade.php
并且在此 blade 中,有一行获取文章的当前图像:
<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";
}
}
这是调用 blade:
的ArticleController
edit 方法
public function edit(Article $article)
{
return view('website.backend.articles.edit',compact('article'));
}
现在的问题是,每当我想转到这个 blade 时,它 returns 这个错误:
ErrorException Trying to access array offset on value of type null (View: edit.blade.php)
指的是blade这一行:
@foreach($article->images['images'] as $key => $image)
所以我不知道为什么会显示这个错误,如果你知道请告诉我,我非常感谢你们的任何想法...
提前致谢。
更新#2:
这是我 store 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);
的输出是:
App\Models\Article {#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:\xampp\tmp\php1825.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:\xampp\tmp\php1825.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()
包含一个设置为临时路径 (F:\xampp\tmp\php1825.tmp
) 的键 images
,在 array_merge 中,后面参数中的值会覆盖前面参数中的值。要修复它,只需像这样交换参数:
auth()->user()->article()->create(array_merge($request->all(), ['images' => $imageUrl]));