Comments (3)
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.
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.
$post = PostTranslation::where('slug', '=', $slug) ->where('locale','=', App::getLocale() )->first() ->post()->first();
- This code will cause a fatal error if not a translation is not found.
- 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)
- Add Laravel 5.8 support HOT 1
- Saving in translated model instead of saving in original model HOT 4
- Fetching Model by translated field and determine locale HOT 3
- Extra Fields or Properties For Translated Text HOT 2
- Can't translate to Portuguese HOT 3
- Display translated attribute even if no translation for current locale and fallback locale? HOT 3
- Has this package a possibility to get translations of related models HOT 2
- Ability to save in both languages HOT 8
- ID coming back with different data HOT 4
- Query where locale not working properly HOT 2
- What would be the best way to handle admin panel with translations? HOT 4
- how i can custom $translatedAttributes from outside model HOT 2
- Multiple Databases HOT 2
- Fallback to default locale with numeric field is not working as expected HOT 3
- get all and Fallback if translation is missing #23 HOT 2
- Wrong column name HOT 2
- Problem with relations HOT 3
- Prefix for default language HOT 6
- When I change local I don't get the translation HOT 7
- how to change the category translation on the fly? HOT 6
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 laravel-translatable.