在null laravel

时间:2016-03-14 13:16:31

标签: php laravel laravel-5

大家好我们使用laravel 5多态关系来保存数据库中的数据但是我遇到了一些问题。当我尝试将数据保存在数据库中时,它会给我带来这个错误

  

在null

上调用成员函数save()

我不知道为什么我会遇到这个错误。我按照本教程了解多态关系Creating Polymorphic Relations in Laravel 5

我正在制作这样的多态关系。帖子和评论可以有很多喜欢。

类似请求会验证如果我发送了ID,则应该接受此请求,否则

与控制器类似

<?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->dispatch(new LikeCommand("comment" , $data));
        }catch(Exception $e){
            $e->getMessage();
        }
    }

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

喜欢命令

<?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->action = $action;
        $this->postId = $request->id;
        $this->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->action );
        error_log("ID : ".$this->postId);

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


    }
}

与Repo一样

<?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->post_id = $comment;
        // $Comment->post_id = $this->post_id;
        $comment->save();

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

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


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

赞模特

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

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

发布模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class PostModel extends Model
{

    //
    public $table = "posts";

    public function likes()
    {
      return $this->morphMany('App\Like', 'likeable');
    }
}

评论模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

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

    public function likes()
    {
      return $this->morphMany('App\Like', 'likeable');
    }
}

更新 我通过将$result->Like->save($like);更改为此$result->likes->save($like);来解决了这个问题,而在PostModel中,我必须在CommentModel中将App\Likes更改为App\LikeModel,但现在却把它扔给了我错误error : Method save does not exist.

1 个答案:

答案 0 :(得分:1)

已更新版本中的问题是收集了喜欢但不需要运行$relation->save($like)的关系。

请参阅以下更新代码:

public function LikePost($id, PostRepository $postRepo){
    $result = $postRepo->getById($id);
    $Like = new likeModel;
    error_log("5");
    $result->likes()->save($Like);
    error_log("6");
    $Like->status="success";
    return $Like;
}