Git Product home page Git Product logo

Comments (7)

boxfrommars avatar boxfrommars commented on August 25, 2024

@timganter Hello! Thank you :)
can you provide your current database structure?

from rutorika-sortable.

timganter avatar timganter commented on August 25, 2024

@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.

boxfrommars avatar boxfrommars commented on August 25, 2024

@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:

  1. Remove BelongsToSortedManyTrait and replace ->belongsToSortedMany with default ->belongsToMany in the your Post model
  2. 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.

timganter avatar timganter commented on August 25, 2024

@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.

boxfrommars avatar boxfrommars commented on August 25, 2024

@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.

boxfrommars avatar boxfrommars commented on August 25, 2024

@timganter I've forgot to say, you should update the package to the version 3.1.0 for this functionality

from rutorika-sortable.

timganter avatar timganter commented on August 25, 2024

@boxfrommars So cool! Thanks again man! =)

from rutorika-sortable.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.