Skip to content
This repository has been archived by the owner on Jun 18, 2019. It is now read-only.

Questions regarding installation and setup #15

Closed
gerardnll opened this issue Mar 25, 2014 · 3 comments
Closed

Questions regarding installation and setup #15

gerardnll opened this issue Mar 25, 2014 · 3 comments

Comments

@gerardnll
Copy link

First of all, thanks for this bundle, which looks great.
After running composer update. do i need to set up the provider in app.php? should i put this 'Dimsav\Translatable\TranslatableServiceProvider'?

Second, if that's not necessary maybe the error (500) it's coming from another place. The 'php artisan serve' doesn't give me an error. But trying

dd($posts->first()->en->title);

in the controller makes laravel crash. it looks like it doesn't find the translated attributed.
I'm also using presenters so i have something like this, i don't know if this is the proper way of getting the translated and preformated properties:

class Post extends Eloquent implements PresentableInterface {
    use \Dimsav\Translatable\Translatable;

    public $translatedAttributes = ['title','excerpt','content','meta_title','meta_description','meta_keywords','header_image'];

     /**
     * Returns a formatted and localized post content entry,
     * this ensures that line breaks are returned.
     *
     * @return string
     */
    public function content()
    {
        return nl2br($this->content);
    }
}

on the post translation i have this:

class PostTranslation extends Eloquent {

    public $timestamps = false;
    protected $fillable = ['title','excerpt','content','meta_title','meta_description','meta_keywords','header_image']; 

}

This is my migration file:

// Create the `Posts` table
        Schema::create('posts', function($table)
        {
            $table->engine = 'InnoDB';
            $table->increments('id')->unsigned();
            $table->string('slug');
            $table->string('slug_es');
            $table->string('slug_ca');
            $table->integer('user_id')->unsigned();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->integer('category_id')->unsigned();
            $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
            // columns in the home (only journal)
            $table->smallInteger('importance')->nullable();
            // date of the party
            $table->dateTime('date')->nullable();
            // can this post be deleted?
            $table->boolean('deleteable')->default(true);
            // open to comments?
            $table->boolean('comment_status')->default(false);
            $table->boolean('published')->default(false);
            // type = Event or Journal
            $table->string('type', 30);

            $table->timestamps();
            $table->softDeletes();
        });

        Schema::create('posts_translations', function($table)
        {
            $table->increments('id');
            $table->integer('post_id')->unsigned();
            $table->string('title');
            $table->string('header_image');
            $table->text('excerpt')->nullable();
            $table->text('content');
            $table->string('meta_title');
            $table->string('meta_keywords');
            $table->string('meta_description');
            $table->string('locale')->index();

            $table->unique(['post_id','locale']);
            $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
        });

Is the presenter going to take the translated attributes on the view? how should i do this?

Also, what about if i want to use a slug for a post, so every language has it's own slug? how can i access the post by the translation? Currently i have the translated slugs on the Post table but i don't know if that's the best way.

@dimsav
Copy link
Owner

dimsav commented Mar 25, 2014

Hi @gerardnll,

using the service provider is not needed. It was added in an early version of the package and was forgotten since. Thanks for your notice, I will remove it in the next release.

But trying
dd($posts->first()->en->title);
in the controller makes laravel crash. it looks like it doesn't find the translated attributed.

Did you follow the installation instructions? Maybe you haven't done the step 4. Also make sure dd($posts->first()) displays a Post instance.

$table->string('slug');
$table->string('slug_es');
$table->string('slug_ca');

This is not a very good design for your tables. Save slug in your translation table and make sure it is unique. To find the post related to this slug you simply have to find the translation and then find the post that is being translated. Define a belongsTo relationship in the translation for that.

Is the presenter going to take the translated attributes on the view? how should i do this?

You have to add the presenters to your translation class. $post->en is an instance of the translation class, so something like $post->en->whatever() would work.

@gerardnll
Copy link
Author

ok i've moved the slug column.
i've added this on the postTranslation model:

    /**
     * Get the original post.
     *
     * @return Post
     */
    public function post()
    {
        return $this->belongsTo('Post', 'post_id');
    }

In the Post model i do not have to put a hasMany('PostTranslation') ? i supose this is done by the bundle.

What i'm not sure if it's good coded is this ($post):

    public function getView($slug)
    {
        // Get this blog post data
        $post = PostTranslation::where('slug', '=', $slug)->where('locale','=', App::getLocale() )->first()->post()->first(); // These first() look bad.

        // Check if the blog post exists
        if (is_null($post))
        {
            return App::abort(404);
        }

        // Get this post comments
        $comments = $post->comments()->orderBy('created_at', 'ASC')->get();

        // Get current user and check permission
        $user = $this->user->currentUser();
        $canComment = false;
        if(!empty($user)) {
            $canComment = $user->can('post_comment');
        }

        // Show the page
        return View::make('site/blog/view_post', compact('post', 'comments', 'canComment'));
    }

Thanks

@dimsav
Copy link
Owner

dimsav commented Mar 25, 2014

$post = PostTranslation::where('slug', '=', $slug)
    ->where('locale','=', App::getLocale() )->first()
    ->post()->first();
  1. This code will cause a fatal error if not a translation is not found.
  2. Avoid using eloquent queries in your controller, do this in your model of even better use a repository.
// PostTranslation.php

public function getBySlug($slug)
{
    return $this->where('slug', '=', $slug)->first();
}

// controller
$this->translations = new PostTranslation;

$translation = $this->translations->getBySlug($slug);

if ( ! $translation)
{
    return App::abort(404);
}

$post = $translation->post();

// ...

I haven't tested the code above, but you get the idea.

I am closing the issue since the problem is not related to the package.

@dimsav dimsav closed this as completed Mar 25, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants