Git Product home page Git Product logo

Comments (3)

dimsav avatar dimsav commented on July 26, 2024

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.

from laravel-translatable.

gerardnll avatar gerardnll commented on July 26, 2024

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

from laravel-translatable.

dimsav avatar dimsav commented on July 26, 2024
$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.

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.