在空Laravel上调用成员函数save() [英] Call to a member function save() on null laravel
问题描述
大家好,我正在使用laravel 5多态关系将数据保存在数据库中,但是我遇到了一些问题.每当我尝试将数据保存在数据库中时,都会引发此错误
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
在null上调用成员函数save()
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.
like请求验证了如果我发送ID,则该请求应得到娱乐,否则
The like Request validates that if I'm sending a id then this request should be entertained otherwise not
类似控制器
<?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-> likes-> save($ like); 解决了这个问题.code>和他的PostModel我必须在CommentModel中类似地将
App \ Likes
更改为 App \ LikeModel
,但是现在它引发了这个错误 error:方法保存没有存在.
Update
I solved this problem by changing $result->Like->save($like);
to this $result->likes->save($like);
and int he PostModel I had to change App\Likes
to App\LikeModel
similarly in CommentModel but now it's throwing me this error error : Method save does not exist.
推荐答案
您在更新版本中遇到的问题是收到喜欢的集合,但没有获得需要运行 $ relation->的关系.保存($ like)
.
Your problem in updated version was getting collection of likes but not relation which you need to run $relation->save($like)
.
请参见下面的更新代码:
See updated code below:
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;
}
这篇关于在空Laravel上调用成员函数save()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!