簡體   English   中英

在 null laravel 上調用成員函數 save()

[英]Call to a member function save() on null laravel

大家好,我正在使用 laravel 5 多態關系將數據保存在數據庫中,但我遇到了一些問題。 每當我嘗試將數據保存在數據庫中時,它都會向我拋出此錯誤

在 null 上調用成員函數 save()

我不知道為什么我面臨這個錯誤。 我正在關注關於多態關系在 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 , );
        }


    }
}

喜歡回購

<?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->Like->save($like); 到這個$result->likes->save($like); 在他的 PostModel 中,我不得不在App\\LikeModel類似地將App\\Likes更改為App\\LikeModel ,但現在它向我拋出了這個錯誤error : Method save does not exist.

您在更新版本中的問題是獲得喜歡的集合,但不是您需要運行$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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM