Comments (7)
@timganter Hello! Thank you :)
can you provide your current database structure?
from rutorika-sortable.
@boxfrommars Sure! I'm going to email it to you if you don't mind. Just don't want to post it here where it's public since it's client work. =)
from rutorika-sortable.
@timganter Got it. Yes, your structure is almost the same as in the demo. Do you need sorting both tags and posts? If you don't, solution is:
- Remove
BelongsToSortedManyTrait
and replace->belongsToSortedMany
with default->belongsToMany
in the yourPost
model - Add to the
Tag
model:
use BelongsToSortedManyTrait;
public function posts()
{
return $this->belongsToSortedMany('\App\Post');
}
Note, that now you should use
$tag->posts()->save($posts);
$tag->posts()->attach($posts->id);
$tag->posts()->sync([$postId1, $postId2, /* ...postIds */]);
instead of $post->tags()->...
from rutorika-sortable.
@boxfrommars Thanks Dmitry.
I gave that a shot. I'm using polymorphic relations. So, in my Post model I have...
public function tag()
{
return $this->morphedByMany(Tag::class, 'taggable')->withTimestamps();
}
In my Tag model I have...
public function post()
{
return $this->morphToSortedMany(Post::class, 'taggable')->withTimestamps();
}
However I'm getting an SQL error.
Column not found: 1054 Unknown column 'post_id' in 'field li
st' (SQL: select `post_id` from `taggables` where `taggable_id` = 2 and `taggable_type` = App\Tag)
Did I hook things up correctly?
Thanks again for your help!
from rutorika-sortable.
@timganter Sorry for waiting.
I've added ->morphedBySortedMany
relation and demo for it: http://sortable5.boxfrommars.ru/albums-and-videos
So you need add to your Tag
model MorphToSortedManyTrait
trait and this relation: (like in the https://github.com/boxfrommars/rutorika-sortable-demo5/blob/master/app/Author.php)
use Rutorika\Sortable\MorphToSortedManyTrait;
class Tag extends Model
{
use MorphToSortedManyTrait;
public function posts()
{
return $this->morphedBySortedMany('App\Post', 'taggable', 'position');
}
}
and then you can attach/sync/save posts to this tag (not vice versa):
$tag->posts()->save($posts);
$tag->posts()->attach($posts->id);
$tag->posts()->sync([$postId1, $postId2, /* ...postIds */]);
I'll update readme in a few days (I'm on vacation now)
from rutorika-sortable.
@timganter I've forgot to say, you should update the package to the version 3.1.0 for this functionality
from rutorika-sortable.
@boxfrommars So cool! Thanks again man! =)
from rutorika-sortable.
Related Issues (20)
- `byPosition` scope HOT 6
- It's not working in mobile. HOT 1
- Confused by Setup HOT 4
- General error: 1364 Field 'position' doesn't have a default value HOT 1
- The selected id is invalid. - Laravel 5.5 HOT 1
- any plan to support laravel 5.6? HOT 1
- Issue and Solution: Sort the second item to first item
- Position actual number HOT 1
- $entity->moveAfter($positionEntity);
- sorted scope should sort with respect to sortableGroupField
- working example?
- Laravel 5.7 support? HOT 5
- Strict equality of the sortable group field values that are cast is uncertain
- Call to undefined function Rutorika\Sortable\array_get() HOT 1
- Laravel 7 support? HOT 5
- Call to a member function getAttribute() on null HOT 1
- belongsToSortedMany relationships does't work with PivotEventTrait
- Is is possible in belongsToSortedMany set a new item position 1 and also reorder other item HOT 1
- Laravel 10 support HOT 2
- Use updated_at instead of position HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rutorika-sortable.