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.');
}