-
Notifications
You must be signed in to change notification settings - Fork 320
Updated_at fields not changing correctly #239
Comments
Hi, can you share some code? |
Article model code: <?php namespace App\Models;
use Dimsav\Translatable\Translatable;
use Illuminate\Database\Eloquent\Model;
class Article extends Model
{
use Translatable;
public $translatedAttributes = ['title', 'content'];
protected $table = 'articles';
protected $fillable = ['published'];
public function articleCategory()
{
return $this->belongsTo(\App\Models\ArticleCategory::class, 'article_categorie_id', 'id');
}
} Article Translation code: <?php namespace App\Models;
use Cviebrock\EloquentSluggable\SluggableTrait;
use Illuminate\Database\Eloquent\Model;
class ArticleTranslation extends Model
{
use SluggableTrait;
public $timestamps = false;
protected $sluggable = [
'build_from' => 'title',
'save_to' => 'slug'
];
protected $table = 'article_translations';
protected $fillable = ['title', 'content'];
} And the controller updating my stuffs : <?php
namespace App\Http\Controllers\Backend;
use App\Models\Article;
use App\Models\ArticleCategory;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
class ArticlesController extends BackendBaseController
{
public function index()
{
$articles = Article::paginate(10);
return view('backend.cms.articles.index')->with('articles',$articles);
}
public function add(Request $request) {
if($request->isMethod('get'))
return view('backend.cms.articles.add')->with('categories', ArticleCategory::all());
$this->validate($request, [
'title' => 'required|max:255',
'content' => 'required',
]);
$article = new Article();
$article->title = $request->input('title');
$article->content = $request->input('content');
$article->articleCategory()->associate($request->input('article_categorie_id'));
if($article->save())
return back()->with('save_success', 'La page a été créé avec succès');
}
public function edit(Request $request, $id) {
$article = Article::findOrFail($id);
if($request->input('lang')) {
App::setLocale($request->input('lang'));
}
if($request->isMethod('get')) {
return view('backend.cms.articles.edit')->with('article', $article)->with('categories', ArticleCategory::all());
}
$this->validate($request, [
'title' => 'required|max:255',
'content' => 'required',
]);
$article->title = $request->input('title');
$article->content = $request->input('content');
$article->articleCategory()->associate($request->input('article_categorie_id'));
if($article->save())
return back()->with('save_success', 'La page a été enregistrée avec succès');
}
} Thanks |
Can you describe what's happening, what action has the issue? What do you mean the timestamps are not updated correctly? |
The timestamps are not updated if the update affects ArticleTranslation model attributes (columns are in translations table), but they are updated if changes affects attributes of my Article model (columns exists in the table articles). $article->title = "a title";
$article->content = "some content";
$article->save(); // the timestamps are not modified
--------------------------------------------------------------------------------------------
// if changes affects columns in the same table
$article->title = "a title";
$article->content = "some content";
$article->categorie()->associate($categorie); // this column is part of the table articles
$article->save(); // timestamps are updated but only if categorie changes wich is normal the issue happen in the edit action. |
@karamosky you should just be able to add the following to your <?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class ArticleTranslation extends Model
{
// Your existing code...
/**
* All of the relationships to be touched.
*
* (see https://laravel.com/docs/5.2/eloquent-relationships#touching-parent-timestamps)
*
* @var array
*/
protected $touches = ['article'];
/**
* Setup the relationship to the parent article.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function article()
{
return $this->belongsTo(Article::class);
}
} |
@BenConstable Thank you, this is exactly what i was looking for 👍 it works now. |
No problem, glad I could help! |
Hi, first, thank you for the great work you have done here 👍
I am facing an issue with my translated model, when using magic properties the timestamps are not updated correctly. do you have an idea how to fix it.
Thank you
The text was updated successfully, but these errors were encountered: