0

I have a problem with my function in laravel

I am trying to execute the following code:

public static function likePost($value)
{
   $post_id = $value['post_id']; 
   $user_id = $value['user_id']; 
   if($value['liked'] == "1")
   {
      $model = new Like;
      $model->fill($value);
      $model->created = time();
      if($model->save())
      {
        $post = Post::with('user')->where('id', '=', $value['post_id'])->get()->first();
        $text = $post['user']['name']." ".Notification::TEXT_LIKE_POST;
        Push::push($text, $value['post_id']);
      }
   }
   else
   {
      Like::where('user_id', '=', $user_id)->where('post_id', '=' $post_id)->get()->first()->delete();
   }
}

But I'm getting the following error: syntax error, unexpected '$post_id' (T_VARIABLE)

What is wrong with this function? Inside of the if the $post_id work fine, but in the else it don't work.

2 Answers2

1

As @andrewsi said you have missing comma after '='. Also you can just use this code which will do the same job:

Like::where('user_id', $user_id)->where('post_id', $post_id)->delete();
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
0

you are missing a , in Like::where('user_id', '=', $user_id)->where('post_id', '=' $post_id)->get()->first()->delete();

it should be Like::where('user_id', '=', $user_id)->where('post_id', '=', $post_id)->get()->first()->delete();

or Like::where('user_id', '=', $user_id)->where('post_id', $post_id)->get()->first()->delete();

Hanson
  • 99
  • 8