mardi 9 mars 2021

Error when updating a blog post in laravel

This is the error in the log:

production.ERROR: count(): Parameter must be an array or an object that implements Countable {"userId":7,"exception":"[object] (ErrorException(code: 0): count(): Parameter must be an array or an object that implements Countable at /var/www/new_site/app/Http/Controllers/postsController.php:515)

When I update a post, it gives me a 500 server error. When I create a post, the post is created with no problems but I get redirected to a blank screen. But I can't seem to update a post.

Here's the actual code for post updating:

//update post in DB

public function updatepost(Request $request){

    $this->validate($request, [

    'thumbnail' => 'mimes:jpg,jpeg,png|max:800',

    'file1' => 'mimes:rar,zip|max:10000',

    ]);



    $file1=$request->file('file1');

    if(is_null($request->file('file1'))){

      $p=posts::where('id', '=', $request['id'])->first();

      $attmt1=$p->attachment;

    }

    else

    {

    $upload_dir='uploads';

    $attmt1=$file1->getClientOriginalName();

    $move=$file1->move($upload_dir, $attmt1);

    }



    if(is_null($request->file('thumbnail'))){

      $p=posts::where('id', '=', $request['id'])->first();

      $image=$p->thumbnail;

    }

    else

    {

      $img=$request->file('thumbnail');

      $upload_dir='new_site/thumbnails';

      $image=$img->getClientOriginalName();

      $move=$img->move($upload_dir, $image);

      //end thumbnail process 

    }

    $posts = new posts();

    $title=$request['title'];

    $content=htmlentities($request['editor1']);

    $keywords=$request['keywords'];

    $video=$request['video'];

    $category=$request['category'];

    $type=$request['type'];
$slug = str_slug($request->slug, '-');

    $school=$request['school'];



    //process url

    $find=array(",","."," ");

    $replace=array("","","-");

    $titleurl = str_replace($find,$replace, $title);

   $url='articles/'.strtolower($titleurl);

    $check=posts::where('url', '=', $url)->first();

    if(count($check)>1){

      return redirect()->back()

        ->with('message', 'Looks like there is a similar topic. please review post');

    }

    //end url process



    $check=posts::where('id', $request['id'])

    ->update([

      'title' => $title,

      'content' =>$content, 

      'keywords' =>$keywords, 

      'video' =>$video, 

      'category' =>$category, 

      'thumbnail' =>$image, 

      'post_type' =>$type, 

      'url' =>$url, 
      'slug' =>$slug,

      'school' =>$school, 

      'attachment' =>$attmt1, 

      ]);

    return redirect()->back()

    ->with('message', 'Post updated Sucessful');

}

This is line 515:

if(count($check)>1){

Any ideas on what problem may be?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire