Call to a member function save() on null laravel
<p>Hello guys I'm using laravel 5 polymorphic relation to save the data in the database but I'm facing some problem. When ever I try to save the data in the database it is throwing me this error </p>

<blockquote>
  <p>Call to a member function save() on null</p>
</blockquote>

<p>I don't know why I'm facing this error. I'm following this tutorial fot the polymorphic relation <a href="http://www.easylaravelbook.com/blog/2015/01/21/creating-polymorphic-relations-in-laravel-5/" rel="nofollow">Creating Polymorphic Relations in Laravel 5</a>.</p>

<p>I 'm making a polymorphic relation like this. A post and comment can have many likes. </p>

<p>The like Request validates that if I'm sending a id then this request should be entertained otherwise not</p>

<p><strong>Like Controller</strong></p>

<pre><code>&lt;?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Requests\LikeRequest;
use App\Commands\LikeCommand;
// use App\Commands\ReadLikeCommand;
use App\Http\Controllers\Controller;



use Illuminate\Bus\Dispatcher;
// use App\Transformers\PostTransformer;
use Exception;
// use App\Events\TestEvent;
use App\Exceptions\Custom\NotPermissionException;
use App\Exceptions\Custom\NameNotFound;
class LikeController extends Controller
{

    public function likeComment(LikeRequest $data){
        try{
            $render = $this-&gt;dispatch(new LikeCommand("comment" , $data));
        }catch(Exception $e){
            $e-&gt;getMessage();
        }
    }

    public function likePost(LikeRequest $data){
        error_log("1");
        try{
            $render = $this-&gt;dispatch(new LikeCommand("post" , $data));
        }catch(Exception $e){
            error_log("error : ".$e-&gt;getMessage());
        }
    }
}
</code></pre>

<p><strong>Like Command</strong></p>

<pre><code>&lt;?php

namespace App\Commands;

use App\Commands\Command;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Foundation\Bus\DispatchesJobs;
use App\Repositories\LikeRepository;
use Exception;
use DB;
use App\LikeModel;
use Artisan;


use App\Repositories\PostRepository;

class LikeCommand extends Command implements SelfHandling
{

    use DispatchesJobs;


    private $postId;
    private $action;
    private $data;




    /**
     * Create a new command instance.
     *
     * @param $request
     *
     * @internal param $email
     * @internal param $phone
     * @internal param $comments
     * @internal param $name
     */

    public function __construct($action,$request)
    {
        $this-&gt;action = $action;
        $this-&gt;postId = $request-&gt;id;
        $this-&gt;data = $request;
        error_log("Hello in the construct");
    }

    /**
     * Execute the command.
     *
     * @param TestRepository $TestRepository
     */
    public function handle(LikeRepository $LikeRepo, PostRepository $postRepo )
    {  

        error_log("Hello");
        error_log("A : ".$this-&gt;action );
        error_log("ID : ".$this-&gt;postId);

        if($this-&gt;action =="comment"){
            error_log("in like comment");
          return  $LikeRepo-&gt;LikeComment( $this-&gt;postId);
        }else if ($this-&gt;action == "post"){ 
            error_log("2");
           return $LikeRepo-&gt;LikePost( $this-&gt;postId, $postRepo , );
        }


    }
}
</code></pre>

<p><strong>Like Repo</strong></p>

<pre><code>&lt;?php

namespace App\Repositories;

use App\Repositories\CommentRepository;
use App\Repositories\PostRepository;
use App\CommentModel;
use App\LikeModel;
class LikeRepository
{
    public function create($comment)
    {
        // error_log("Trying to save now. Let's see");
         $Like = new LikeModel;
        $Like-&gt;post_id = $comment;
        // $Comment-&gt;post_id = $this-&gt;post_id;
        $comment-&gt;save();

    }   
    public function LikeComment($id) {
        // return CommentModel::where('post_id' , $id)-&gt;get();
        // return CommentModel::get();
    }

    public function LikePost($id, PostRepository $postRepo){
        error_log("3");
        $result = $postRepo-&gt;getById($id);
        error_log("4");


        // $Like = DB::transaction(function () use ($LikeRepository) {
        $Like = new likeModel;
        error_log("5");
        $result-&gt;Like-&gt;save($like);
        error_log("6");
        $Like-&gt;status="success";
        // });
        return $Like;
    }
}
</code></pre>

<p><strong>Like Model</strong></p>

<pre><code>&lt;?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class LikeModel extends Model
{
    public $table = "likes";
    //
    public function likeable()
    {
      return $this-&gt;morphTo();
    }
}
</code></pre>

<p><strong>Post Model</strong></p>

<pre><code>&lt;?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class PostModel extends Model
{

    //
    public $table = "posts";

    public function likes()
    {
      return $this-&gt;morphMany('App\Like', 'likeable');
    }
}
</code></pre>

<p><strong>Comment Model</strong></p>

<pre><code>&lt;?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class CommentModel extends Model
{
    //
    public $table = "comments";

    public function likes()
    {
      return $this-&gt;morphMany('App\Like', 'likeable');
    }
}
</code></pre>

<p><strong>Update</strong>
I solved this problem by changing <code>$result-&gt;Like-&gt;save($like);</code> to this <code>$result-&gt;likes-&gt;save($like);</code> and int he PostModel I had to change <code>App\Likes</code> to <code>App\LikeModel</code> similarly in CommentModel but now it's throwing me this error <code>error : Method save does not exist.</code> </p>