多态关系不在数据库中插入模型类型
Polymorphic relation not inserting model type in database
在我的 Laravel 应用程序中,我有不同的模型可以报告,所以我使用了多态关系。帖子、主题和解决方案是可报告的,并且可以是 MorphedToMany。当我尝试在我的控制器中创建一个新报告时,我收到 General error: 1364 Field 'reportable_id' doesn't have a default value (SQL: insert into `reports` (`channel`, `updated_at`, `created_at`) values ({"id":18,"created_at":"2020-08-20T09:37:06.000000Z","updated_at":"2020-08-20T09:37:06.000000Z","name":"marketing"}, 2020-08-21 09:13:45, 2020-08-21 09:13:45))
作为错误
我按照教程中的文档进行操作,但我无法发现错误
这是我的代码,以 post class 为例
post
use Illuminate\Database\Eloquent\Model;
use Cog\Contracts\Love\Reactable\Models\Reactable as ReactableContract;
use Cog\Laravel\Love\Reactable\Models\Traits\Reactable;
class Post extends Model implements ReactableContract
{
/**
* @var mixed
*/
use Reactable;
protected $fillable=['title','body'];
protected $guarded=[];
public function user(){
return $this->belongsTo('App\User');
}
public function tags(){
return $this->belongsToMany('App\Tag');
}
public function channel(){
return $this->belongsTo('App\Channel');
}
public function comments(){
return $this->hasMany('App\Comment');
}
public function reports(){
return $this->morphToMany('App\Report','reportable');
}
}
举报
use Illuminate\Database\Eloquent\Model;
class Report extends Model
{
protected $guarded=[];
public function reportable(){
return $this->morphTo();
}
}
报告控制器
use App\Comment;
use App\Post;
use App\Report;
use App\Solution;
use App\Thread;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class ReportController extends Controller
{
public function create(){
switch (\request('table')){
case 'posts':
$model=Post::find(\request('model'));
break;
case 'threads':
$model=Thread::find(\request('model'));
break;
case 'solutions':
$model=Solution::find(\request('model'));
//for now we are not implementing comments since is gonna be more complicated
}
//save model into database
$report=new Report();
$report->channel=$model->channel->name;
$model->reports()->create(['channel'=>$model->channel]);
//notify user
//redirect to channel
}
}
已解决 我用 MorphToMany
而不是 MorphMany
在我的 Laravel 应用程序中,我有不同的模型可以报告,所以我使用了多态关系。帖子、主题和解决方案是可报告的,并且可以是 MorphedToMany。当我尝试在我的控制器中创建一个新报告时,我收到 General error: 1364 Field 'reportable_id' doesn't have a default value (SQL: insert into `reports` (`channel`, `updated_at`, `created_at`) values ({"id":18,"created_at":"2020-08-20T09:37:06.000000Z","updated_at":"2020-08-20T09:37:06.000000Z","name":"marketing"}, 2020-08-21 09:13:45, 2020-08-21 09:13:45))
作为错误
我按照教程中的文档进行操作,但我无法发现错误
这是我的代码,以 post class 为例
post
use Illuminate\Database\Eloquent\Model;
use Cog\Contracts\Love\Reactable\Models\Reactable as ReactableContract;
use Cog\Laravel\Love\Reactable\Models\Traits\Reactable;
class Post extends Model implements ReactableContract
{
/**
* @var mixed
*/
use Reactable;
protected $fillable=['title','body'];
protected $guarded=[];
public function user(){
return $this->belongsTo('App\User');
}
public function tags(){
return $this->belongsToMany('App\Tag');
}
public function channel(){
return $this->belongsTo('App\Channel');
}
public function comments(){
return $this->hasMany('App\Comment');
}
public function reports(){
return $this->morphToMany('App\Report','reportable');
}
}
举报
use Illuminate\Database\Eloquent\Model;
class Report extends Model
{
protected $guarded=[];
public function reportable(){
return $this->morphTo();
}
}
报告控制器
use App\Comment;
use App\Post;
use App\Report;
use App\Solution;
use App\Thread;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class ReportController extends Controller
{
public function create(){
switch (\request('table')){
case 'posts':
$model=Post::find(\request('model'));
break;
case 'threads':
$model=Thread::find(\request('model'));
break;
case 'solutions':
$model=Solution::find(\request('model'));
//for now we are not implementing comments since is gonna be more complicated
}
//save model into database
$report=new Report();
$report->channel=$model->channel->name;
$model->reports()->create(['channel'=>$model->channel]);
//notify user
//redirect to channel
}
}
已解决 我用 MorphToMany
而不是 MorphMany