Neeraj1005's avatar

ErrorException (E_DEPRECATED) Non-static method App\Article::getMedia() should not be called statically

Now I am using the media library package... the problem is: This store the file in media table it's fine, but the problem is reterving the image...can anyone tell me how to retrive image? and below I have paste the index function. where I have add addMedia() can anyone tell me? This is my article index function

    public function index()
    {

            
        if(Auth::user()->privilege  === "administrator"){
             
            $articles = Article::getMedia()->with('category','user')->where('delete_status','=','1')->orderBy('id','DESC')->get();
            $articles->toArray();
            $trashArticle = Article::where('delete_status','=',0)->count();
             $usertype = 'administrator';
            return view('articles.index',compact('articles','trashArticle','usertype'));
        }else{
              
              $whereArticle = [
                ['delete_status','=','1'],
                ['user_id','=',Auth::user()->id]
              ];
            $articles = Article::with('category','user')->where($whereArticle)->orderBy('id','DESC')->get();
            $articles->toArray();
            $trashArticle = Article::where('delete_status','=',0)->count();
            $usertype = 'agent';
            return view('articles.index',compact('articles','trashArticle','usertype'));
        }    
    }

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

        ]);
 
         $records = $request->all();
          //dd($records);
         //find category slug

          
         $categorySlug = Category::findOrFail($request->category_id);
        
        $user = Auth::user()->id;



        // dd($records);
        
        $records['user_id'] = $user;
        $records['slug'] = $categorySlug->slug;
        //$records['subcategory_id'] = $request->subcategory_id;
        $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);

//Here is the code for upload image in media
       if($request->hasFile('image') && $request->file('image')->isValid()){
           $insertedid->addMediaFromRequest('image')->toMediaCollection('blogimages');
       }

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

and this is my blade file

 @foreach($articles as $val)
        <tr>
        
<td>{{$val->getUrl()}}</td>
</tr>
0 likes
1 reply
Sergiu17's avatar

I never used this library, but the error is quite clear, Non-static method, should not be called statically

https://github.com/spatie/laravel-medialibrary/blob/master/src/HasMedia/HasMediaTrait.php#L235 - here's the method, the solution is to call the method on the Model instance

$articles = Article::with('category','user')
    ->where('delete_status','=','1')
    ->orderBy('id','DESC')
    ->get();

foreach($articles as $article) {
    var_dump($article->getMedia());
}
1 like

Please or to participate in this conversation.