Code Monkey home page Code Monkey logo

Comments (7)

dimsav avatar dimsav commented on May 31, 2024

Hi @wisepotato.

I am not sure I understand. Can you post your classes below?

Thanks

from laravel-translatable.

wisepotato avatar wisepotato commented on May 31, 2024

alright, lets say I am showing multiple models, and lets say author() is a relationship on the main model, when I am working with a translated model, I cant use the function author()

from laravel-translatable.

dimsav avatar dimsav commented on May 31, 2024

Would you like to present a specific example with code samples?

from laravel-translatable.

wisepotato avatar wisepotato commented on May 31, 2024

Main model

<?php


class PostModel extends Eloquent
{
    use Translatable;
    public $translationForeignKey = 'post_id';
    public $localeKey = 'language_id';
    public $translatedAttributes = array('title', 'slug', 'content');
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'blog_posts';

    public function categories()
    {
        return $this->belongsToMany('Hostbrute\Blog\Models\CategoryModel', 'blog_categories_posts', 'post_id', 'category_id');
    }
    /**
     * Get the attributes that should be converted to dates.
     *
     * @return array
     */
    public function getDates()
    {
        return array_merge(parent::getDates(), array(static::PUBLISHED_AT));
    }

    /**
     * Belongs to relationship with User.
     */
    public function author()
    {
        return $this->belongsTo(Config::get('auth.model', 'User'), 'user_id');
    }


}

PostModelTranslation.php:

<?php namespace Hostbrute\Blog\Models;

use \Config;
use Illuminate\Database\Eloquent\Model as Eloquent;

class PostModelTranslation extends Eloquent{

    public $table = 'blog_posts_translated';
    public $timestamps = false;
    protected $fillable = ['title', 'slug', 'content'];
    /**
     * Accessor for link.
     *
     * @return string
     */
    public function getLinkAttribute($lang = null)
    {
        if($lang !== null)
        {
            return \Localization::getLocalizedURL($lang,\URL::route('post-view', ['id' => $this->id, 'slug' => $this->slug]));
        }
        return \URL::route('post-view', ['id' => $this->id, 'slug' => $this->slug]);
    }
}

now when I have a translated model in for example a view, I have to keep the original PostModel to have the ->author() function

Or should I also implement this in the translated model? That would be strange right? Against everything DRY stands for...

from laravel-translatable.

dimsav avatar dimsav commented on May 31, 2024

I don't see a reason to duplicate the Post-Auhor relationship in the translation. A translation is just a translation of the post.

from laravel-translatable.

wisepotato avatar wisepotato commented on May 31, 2024

why not? The post is translated but the author COULD (or a function in general) also be language specific.

Maybe I have two implementations of the model, one for the translated type and non-translated?

lets say I have a blogpost list, I have to give the view both the translated models and normal models to competely output everything...

from laravel-translatable.

dimsav avatar dimsav commented on May 31, 2024

It is up to you how to structure your relationships to avoid duplicate code. Your question doesn't seem to me to be related to the package.

from laravel-translatable.

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.