Neeraj1005's avatar

Non-static method Illuminate\Database\Eloquent\Model::update() should not be called statically

This is my post update function where I want to update the media table row. The problem is when I uploads image it create the new row in media table but I want to update the row in update section if when is previous image is selected but when No image is selected previously then create a row in media table. can anyone help me out this situation how to update another table row with having id?

public function update(Request $request, $id)
    {
        request()->validate([
            'title'=>'required|max:220',
            'category_id'=>'required',            
            'show_date'=>'required',      
            'description'=>'required'
        ]);

        $records = Article::findOrFail($id);
                 
        if(Auth::user()->user_type != 'Admin_User'){

            $user = Auth::user()->id;
            $records->user_id = $user;
        }
    
        if ($request->hasFile('image')) {
            if ($request->file('image')->isValid()) {
                $image = $request->file('image');
                $extension = $image->getClientOriginalExtension();//Getting extension
                $originalname = $image->getClientOriginalName();//Getting original name
                                
                $path = $image->move('uploads/media/', $originalname);
                $imgsizes = $path->getSize();
                $size = getimagesize($path);
                $width = $size[0]; 
                $height = $size[1];
                $mimetype = $image->getClientMimeType();//Get MIME type


if($records['image']===Null){
            $photo = mediaLibrary::create(['filename'=>$path,'mime'=>$mimetype,'extension'=>$extension,'original_filename'=>$originalname,'imgsize'=>$imgsizes,'width'=>$width,'height'=>$height]);
}
else{
  $photo = mediaLibrary::update(['filename'=>$path,'mime'=>$mimetype,'extension'=>$extension,'original_filename'=>$originalname,'imgsize'=>$imgsizes,'width'=>$width,'height'=>$height]);
}

$records['image'] = $photo->id;
            }
        }
        $records->category_id = $request->category_id;   
        $records->title = $request->title;   
        $records->show_date = $request->show_date;   
        // $records->image = $request->image;   
        $records->description = $request->description;   
        $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;
        }   
        $records->save();
         
                
           

        return redirect()->route('articles.index')
                        ->with('success','Article created successfully.');
    }
0 likes
10 replies
Sinnbeck's avatar

To update something it needs to know what. Replace with real values used to identify the row

mediaLibrary::where('something', $var) ->update(['filename'=>$path,'mime'=>$mimetype,'extension'=>$extension,'original_filename'=>$originalname,'imgsize'=>$imgsizes,'width'=>$width,'height'=>$height]);
Neeraj1005's avatar

@sinnbeck I want to update image column in article table and update the image in mediLibrary table with the id. if I do like this i am getting error

Trying to get property 'id' of non-object

code is:


if($records['image']===Null){
            $photo = mediaLibrary::create(['filename'=>$path,'mime'=>$mimetype,'extension'=>$extension,'original_filename'=>$originalname,'imgsize'=>$imgsizes,'width'=>$width,'height'=>$height]);
}
else{
  $photo = DB::table('media_libraries')->where('id',$records['image'])->update(['filename'=>$path,'mime'=>$mimetype,'extension'=>$extension,'original_filename'=>$originalname,'imgsize'=>$imgsizes,'width'=>$width,'height'=>$height]);
}
$records['image'] = $photo->id;
Sinnbeck's avatar

Ok so I assume the image column points to media library? Is it an ID?

mediaLibrary::where('id', $records['image'])
Neeraj1005's avatar

@sinnbeck yes in articles table image column points to an id. basically in previous I have only used a create method in update section, where it upload new image in media table and store with new id... But I want to when I edit the image it unlink previous one and update new image with having same id.

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Well did you try my version out?

$photo = mediaLibrary::where('id', $records['image'])->pdate(['filename'=>$path,'mime'=>$mimetype,'extension'=>$extension,'original_filename'=>$originalname,'imgsize'=>$imgsizes,'width'=>$width,'height'=>$height]);
1 like
Neeraj1005's avatar

@sinnbeck yes I did...same error occured in this point.

Trying to get property 'id' of non-object

for this line error show========> $records['image'] = $photo->id;

Sinnbeck's avatar
$records['image'] = $photo->id ?? $records['image'];

Or move your code up just below create (cleaner)

1 like
Neeraj1005's avatar

@sinnbeck can you please what this condition actually doing?

$records['image'] = $photo->id ?? $records['image'];

It's working... and now I have new query I want to unlink the image from directory how can I do this?

Neeraj1005's avatar

@sinnbeck Now updating is working in starting image is selected. But in initially when image column is empty it not uploaded any images...only empty data is showing in table

        if ($request->hasFile('image')) {
            if ($request->file('image')->isValid()) {
                $image = $request->file('image');
                $extension = $image->getClientOriginalExtension();//Getting extension
                $originalname = $image->getClientOriginalName();//Getting original name

                if(file_exists($records['mediaLibrary']['filename'])) {
                    unlink($records['mediaLibrary']['filename']);
                }
                                
                $path = $image->move('uploads/media/', $originalname);
                $imgsizes = $path->getSize();
                $size = getimagesize($path);
                $width = $size[0]; 
                $height = $size[1];
                $mimetype = $image->getClientMimeType();//Get MIME type

if($records['image']===Null){
$photo = mediaLibrary::create([
                                'filename'=>$path,
                                'mime'=>$mimetype,
                                'extension'=>$extension,
                                'original_filename'=>$originalname,
                                'imgsize'=>$imgsizes,
                                'width'=>$width,
                                'height'=>$height
                              ]);
}
else{
  $photo = mediaLibrary::where('id',$records['image'])->update([
    'filename'=>$path,
    'mime'=>$mimetype,
    'extension'=>$extension,
    'original_filename'=>$originalname,
    'imgsize'=>$imgsizes,
    'width'=>$width,
    'height'=>$height
  ]);

}
$records['image'] = $photo->id ?? $records['image'];
// $records['image'] = $photo->id;
            }
        }
Sinnbeck's avatar

Updated the last answer with a better solution.

To unlink

  1. Get path from database
  2. Get full storage_path
  3. Use unlink on the path

Please or to participate in this conversation.