0

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: []
}
6
  • I didn't see the dd output, but I think it would be @foreach($article->images as $key => $image) if above method fail, then post the output of dd($article); Commented Nov 9, 2020 at 14:23
  • @sta I just added it, see UPDATE #1 and thank u. Commented Nov 9, 2020 at 14:30
  • Please edit. Collapse it #attributes: array:12 [▶] update it Commented Nov 9, 2020 at 14:32
  • @sta I just collapsed it and edited the UPDATE #1. Commented Nov 9, 2020 at 14:35
  • 1
    According your code, images should be an array, but its astring "images" => "F:\xampp\tmp\php1825.tmp", it saved the tmp image instead of correct name Commented Nov 9, 2020 at 14:42

1 Answer 1

2

It's because of this line:

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

$request->all() contains a key images which is set to the temp path (F:\xampp\tmp\php1825.tmp) and in array_merge, values in later params override ones from earlier params. To fix it just swap the params like this:

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

Sign up to request clarification or add additional context in comments.

1 Comment

You're my HERO!, Thank u very much <3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.