I have three models. One is post the other one is image and the last one user. I have an polymorphic relation between these those.
posts
post_id - integer
title - string
description - text
.....
users
id - integer
name - string
password - string
.....
images
image_id - integer
file_id - text
file_name - string
image_type - string ['poster', 'thumbnail']
imageable_id - integer
imageable_type - string
An image in a post can either be a poster or a thumbnail. How can I display both using crud?
I tried the following, but I only receive the last one.
PostCrudController
$this->crud->addField([
'name' => 'image_id',
'label' => 'Poster',
'type' => 'text',
'name' => 'image_id', // the column that contains the ID of that connected entity;
'entity' => 'images', // the method that defines the relationship in your Model
'attribute' => "file_name", // foreign key attribute that is shown to user
'model' => 'App\Models\Image',
], 'update/create/both');
$this->crud->addField([
'name' => 'image_id',
'label' => 'Thumbnail',
'type' => 'image_text',
'name' => 'image_id', // the column that contains the ID of that connected entity;
'entity' => 'images', // the method that defines the relationship in your Model
'attribute' => "file_name", // foreign key attribute that is shown to user
'model' => 'App\Models\Image',
], 'update/create/both');
I also tried to create a function poster() where i tried to filter it with ->where('image_type', 'poster') and a counterpart for thumbnail
Post
class Post extends Model
{
use CrudTrait;
protected $table = 'posts';
protected $primaryKey = 'post_id';
protected $fillable = [];
/**
* A post has many images
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function images()
{
return $this->morphMany(Image::class, 'imageable');
}
}
Image
class Image extends Model
{
use CrudTrait;
protected $table = 'images';
protected $primaryKey = 'image_id';
protected $fillable = [];
public function imageable()
{
return $this->morphTo();
}
}
If I do {{ dd($fields) }} in edit.blade.php there is also only the last Field available.
Hi @marleybobby ,
Sorry for not answering sooner - I expect you've found your answer by now? If so, you can help others that search the same thing by sharing the knowledge below. This is more of a stack overflow question, so I'll close it.
Cheers!
@marleybobby
It seems like your polymorphic relationship isn't setup correctly. Where your images / posts need a connecting table, i.e. imageables.
@tabacitu
I am having a similar issue with polymorphic relationships with the below:
movie:
id,
title
categories:
id,
name,
color
categorizables:
category_id,
categorizable_id,
categorizable_type
my MovieCrudController.php:
$this->crud->addField([
'label' => 'Categories',
'name' => 'categorizable_id',
'entity' => 'categories',
'attribute' => 'categorizable_id',
'model' => 'App\Models\Category',
'pivot' => 'true',
'type' => 'select2_multiple'
]);
This returns an empty set... any help would be appreciated
Hello people,
i have a similar issue but with 1:1 polymophic relationship
I would like to control the meta_description for each models (movement, group, artist, album, track)
So i created a polymophic relationship one to one to do that.
But inside the CRUD controller of backpack, when i define a textarea to create or edit my meta_description, the 'attribute' is not working properly. Here is my code
META.php
<?php
namespace App\Models;
use Backpack\CRUD\app\Models\Traits\CrudTrait;
use Illuminate\Database\Eloquent\Model;
class Meta extends Model
{
protected $table = 'metas';
// protected $primaryKey = 'id';
// public $timestamps = false;
protected $guarded = ['id'];
protected $fillable = ['description', 'metable_id', 'metable_type'];
// protected $hidden = [];
// protected $dates = [];
public function metable()
{
return $this->morphTo();
}
}
MOVEMENT.php
<?php
namespace App\Models;
use Backpack\CRUD\app\Models\Traits\CrudTrait;
use Illuminate\Database\Eloquent\Model;
class Meta extends Model
{
protected $table = 'metas';
// protected $primaryKey = 'id';
// public $timestamps = false;
protected $guarded = ['id'];
protected $fillable = ['description', 'metable_id', 'metable_type'];
// protected $hidden = [];
// protected $dates = [];
public function metable()
{
return $this->morphTo();
}
}
MovementCrudController.php
// GESTION DE LA META_DESCRIPTION POUR LE MOVEMENT
$this->crud->addField([
'name' => 'meta', // the relationship name in your Model
'type' => 'text',
'label' => 'meta description',
'entity' => 'meta', // the relationship name in your Model
// NOT WORKING T_T
'attribute' => 'description', // attribute on Meta that is shown to admin
'tab' => 'Ses relations',
]);
Do you know if backpack is able to manage one to one polymorphic relation ? i don't see any information about these specific case in the official documentation.
Thanks in advance
@marleybobby , I know @pxpm is just now working on adding examples for ALL relationship types inside the demo, including polymorphic relationships. @pxpm could you please post your experience here too?
@pxpm I would love an example on a Polymorph type, any idea when we can check it?
Most helpful comment
@marleybobby , I know @pxpm is just now working on adding examples for ALL relationship types inside the demo, including polymorphic relationships. @pxpm could you please post your experience here too?