I'm working with Laravel to develop my project and basically I want to edit some Articles from database at my theme, so I created a blade called edit.blade.php and in this blade, there's a row that gets the current image of an Article:
<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>
And the Article model also goes like this:
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";
}
}
And here is the ArticleController edit method which calls the blade:
public function edit(Article $article)
{
return view('website.backend.articles.edit',compact('article'));
}
Now the problem with this is that, whenever I want to go to this blade, it returns this error:
ErrorException Trying to access array offset on value of type null (View: edit.blade.php)
And it is referring to this line of blade:
@foreach($article->images['images'] as $key => $image)
So I don't know why it shows me this error, if you know please let me know, I would really appreciate any idea from you guys...
Thanks in advance.
UPDATE #2:
Here is my store method of 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'));
}
And here is the AdminController that comes with the UploadImages method and is extended by 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;
}
}
UPDATE #1:
Output of dd($article); is:
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: []
}
@foreach($article->images as $key => $image)if above method fail, then post the output ofdd($article);#attributes: array:12 [▶]update itimagesshould be an array, but its astring"images" => "F:\xampp\tmp\php1825.tmp", it saved the tmp image instead of correct name