Skip to content

Commit 8ec1fc6

Browse files
author
tbanov
committed
[Convenience Issue 2.0.1] Trying to access attribute of object null, fix config publishing, update readme
1 parent 5cc89e8 commit 8ec1fc6

File tree

3 files changed

+37
-29
lines changed

3 files changed

+37
-29
lines changed

README.md

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
# Laravel DB Localization
1+
# Laravel DB Localization for laravel 5.1
22

33
## Installation
44

55
Open `composer.json` file of your project and add the following to the require array:
66
```json
7-
"despark/laravel-db-localization": "1.2.*"
7+
"despark/laravel-db-localization": "2.0.*"
88
```
99

1010
Now run `composer update` to install the new requirement.
1111

12-
Once it's installed, you need to register the service provider in `app/config/app.php` in the providers array:
12+
Once it's installed, you need to register the service provider in `config/app.php` in the providers array:
1313
```php
1414
'providers' => array(
1515
...
16-
'Despark\LaravelDbLocalization\LaravelDbLocalizationServiceProvider',
16+
Despark\LaravelDbLocalization\LaravelDbLocalizationServiceProvider::class,
1717
);
1818
```
1919

2020
Publish the config file:
21-
`php artisan config:publish despark/laravel-db-localization`
21+
`php artisan vendor:publish --provider="Despark\LaravelDbLocalization\LaravelDbLocalizationServiceProvider" --tag="config"`
2222

2323
# How to use it
2424

@@ -68,9 +68,12 @@ Schema::create('contacts_i18n', function (Blueprint $table) {
6868
```
6969
## Model Example
7070
```php
71+
72+
use Despark\LaravelDbLocalization\i18nModelTrait;
73+
7174
class Contacts extends Eloquent
7275
{
73-
use Despark\LaravelDbLocalization\i18nModelTrait; // You must use i18nModelTrait
76+
use i18nModelTrait; // You must use i18nModelTrait
7477

7578
protected $fillable = [
7679
'fax',
@@ -89,20 +92,18 @@ class Contacts extends Eloquent
8992
class ContactsI18n extends Eloquent
9093
{
9194
protected $table = 'contacts_i18n';
92-
93-
protected $fillable = ['contact_id', 'i18n_id', 'name', 'location'];
9495
}
9596
```
9697
## View example
9798

9899
Create
99100
```php
100-
{{ Form::text("fax", null) }}
101-
{{ Form::text("phone", null) }}
101+
{!! Form::text("fax", null) !!}
102+
{!! Form::text("phone", null) !!}
102103

103104
@foreach($languages as $language)
104-
{{ Form::text("name[name_$language->id]", null) }} // Follow this convention array( fieldname_languageId );
105-
{{ Form::text("location[location_$language->id]", null) }}
105+
{!! Form::text("name[name_$language->id]", null) !!} // Follow this convention array( fieldname_languageId );
106+
{!! Form::text("location[location_$language->id]", null) !!}
106107
@endforeach
107108
```
108109
Retrieve
@@ -117,23 +118,10 @@ Retrieve
117118
$contacts->translate($i18nId)->location; // specific field
118119
```
119120

120-
121121
## Config Example
122122
```php
123-
app/config/packages/despark/laravel-db-localization/config.php
123+
config/laravel-db-localization.php
124124
'locale_class' => 'Despark\LaravelDbLocalization\I18n',
125125
```
126126

127-
## If you want to checkout our example you need to follow this commands:
128-
129-
Execute migrations with the following command
130-
`php artisan migrate --package="despark/laravel-db-localization"`
131-
132-
This will create tables `i18n`, `contacts`, `contacts_i18n`.
133-
134-
Now you must seed `i18n` table:
135-
`php artisan db:seed --class="Despark\LaravelDbLocalization\DatabaseSeeder"`
136-
137-
Now you can check how it works:
138-
http://yourdomain.name/localization_example/
139127

src/LaravelDbLocalizationServiceProvider.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,21 @@ public function register()
1414
$this->app->bind('laravel-db-localization', function ($app) {
1515
return new LaravelDbLocalization();
1616
});
17+
18+
// Load the config file
19+
$this->mergeConfigFrom(__DIR__.'/config/laravel-db-localization.php', 'laravel-db-localization');
1720
}
1821
/**
1922
* Bootstrap the application events.
2023
*/
2124
public function boot()
2225
{
26+
// publish config
2327
$this->publishes([
2428
__DIR__.'/config/laravel-db-localization.php' => config_path('laravel-db-localization.php'),
2529
], 'config');
2630

31+
// publish migrations
2732
$this->publishes([
2833
__DIR__.'/migrations/' => base_path('/database/migrations'),
2934
], 'migrations');

src/i18nModelTrait.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace Despark\LaravelDbLocalization;
44

5-
use Illuminate\Support\Facades\Config;
6-
75
trait i18nModelTrait
86
{
97
/**
@@ -76,7 +74,8 @@ public function getI18nId($locale = null)
7674
if (!$locale) {
7775
$locale = \App::getLocale();
7876
}
79-
$localeModel = Config::get('laravel-db-localization::locale_class');
77+
78+
$localeModel = config('laravel-db-localization.locale_class');
8079
$i18n = $localeModel::select('id')->where('locale', $locale)->first();
8180

8281
$i18nId = null;
@@ -105,6 +104,10 @@ public function translate($locale = false, $alowRevision = false)
105104
if (isset($this->id) && $locale) {
106105
$translation = $translationModel::where($this->translatorField, $this->id)
107106
->where($this->localeField, $locale)->first();
107+
108+
if (!$translation) {
109+
$translation = $translationModel;
110+
}
108111
}
109112

110113
if ($alowRevision == true) {
@@ -234,4 +237,16 @@ public function saveTranslations($translatableId, $options)
234237
}
235238
}
236239
}
240+
241+
public function getTranslatedNoIdAttributes()
242+
{
243+
$array = [];
244+
foreach ($this->translatedAttributes as $attr) {
245+
if (strpos($attr, '_id') === false) {
246+
array_push($array, $attr);
247+
}
248+
}
249+
250+
return $array;
251+
}
237252
}

0 commit comments

Comments
 (0)