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
Call to a member function save() on null
I don't know why I'm facing this error. I'm following this tutorial fot the polymorphic relation Creating Polymorphic Relations in Laravel 5.
I 'm making a polymorphic relation like this. A post and comment can have many likes.
The like Request validates that if I'm sending a id then this request should be entertained otherwise not
Like Controller
<?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());
}
}
}
Like Command
<?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 , );
}
}
}
Like 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;
}
}
Like Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class LikeModel extends Model
{
public $table = "likes";
//
public function likeable()
{
return $this->morphTo();
}
}
Post Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class PostModel extends Model
{
//
public $table = "posts";
public function likes()
{
return $this->morphMany('App\Like', 'likeable');
}
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire