Neeraj1005's avatar

Call to a member function addMedia() on array

Now In my project I am using the medialibrary package. when I store the image it throw an error. plz tell me how to fix this? This is my store function

public function store(Request $request)
    {
        
        request()->validate([
                   
            'category_id'=>'required|not_in:0',                     
            'show_date'=>'required',            
            'description'=>'required'

        ]);
 
         $records = $request->all();

          
         $categorySlug = Category::findOrFail($request->category_id);
        
        $user = Auth::user()->id;
/*
        if ($request->hasFile('image')) {
            if ($request->file('image')->isValid()) {
                
                $fileName=$request->file('image')->getClientOriginalName();
                $fileName =time()."_".$fileName;
                //upload
                $request->file('image')->move('uploads/BlogImage', $fileName);
                    
                    //column name 
                $records['image']=$fileName;
                
            }
        }*/
        
        if($request->hasFile('image') && $request->file('image')->isValid()){
                    $records->addMedia('image')->toMediaCollection('images');
                }

        
        $records['user_id'] = $user;
        $records['slug'] = $categorySlug->slug;
        $records['aslug'] = $this->createSlug($request->title);

        if ($request->has('save'))
        {
                    // draft
            $records['draft'] = 0;
        }
        else if ($request->has('publish'))
        {
                    // publish
            $records['draft'] = 1;
        } 

         
      $insertedid= Article::create($records);

        return redirect()->route('articles.index')
                        ->with('success','Article created successfully.');

    }

THis is my view blade code

                <div class="form-group">
                        <div>
            <small>Featured image</small>
                            <input id="image"  name="image"  type="file" class="form-control {{ $errors->has('image') ? ' is-invalid' : '' }}" value="{{ old('image') }}">
                           @if ($errors->has('image'))
                                <span class="invalid-feedback" role="alert">
                                    <strong>{{ $errors->first('image') }}</strong>
                                </span>
                            @endif
                        </div>
                </div>
0 likes
6 replies
Sti3bas's avatar
Sti3bas
Best Answer
Level 53

@neeraj1005 you have to add it after creating an article:

$article = Article::create($records);

if($request->hasFile('image') && $request->file('image')->isValid()) {
   $article->addMedia('image')->toMediaCollection('images');
}
1 like
Sti3bas's avatar

@neeraj1005 replace addMedia('image') with addMedia($request->file('image')), otherwise change it to addMediaFromRequest('image').

1 like
Neeraj1005's avatar

@sti3bas Now I have one simple doubt. I want to store this image in storage like this structure. storage/public/media/articleImage? So how can I store this?

Sti3bas's avatar

@neeraj1005 something like that should also work:

use Spatie\MediaLibrary\PathGenerator\BasePathGenerator;

class MediaLibraryPathGenerator extends BasePathGenerator
{
   protected function getBasePath(Media $media): string
   {
      return strtolower(class_basename($media->model)) . 'Image/' . $media->getKey();
   }
}

Please or to participate in this conversation.