Fehler bei der Aktualisierung von Artikel -Tags in Laravel 8 Blog -Anwendung?
Posted: 15 Feb 2025, 14:21
Ich arbeite an einer Blogging -Anwendung in Laravel 8. < /p>
Es besteht die Möglichkeit, Artikeln Tags hinzuzufügen. Es gibt eine viele zu viele Beziehung zwischen Artikeln und Tags. Ich habe eine article_tag pivot -Tabelle.
zum Artikel Modell Ich habe die Tags () Methode
hinzugefügt
Wo ist mein Fehler?
Es besteht die Möglichkeit, Artikeln Tags hinzuzufügen. Es gibt eine viele zu viele Beziehung zwischen Artikeln und Tags. Ich habe eine article_tag pivot -Tabelle.
Code: Select all
class Tag extends Model
{
use HasFactory;
protected $fillable = ['name'];
public function articles()
{
return $this->belongsToMany(Article::class);
}
}
hinzugefügt
Code: Select all
public function tags()
{
return $this->belongsToMany(Tag::class)->as('tags');
}
< /code>
I Der ArticleController -Controller Ich habe die beiden Methoden zum Bearbeiten und Aktualisieren eines Artikels: < /p>
public function edit($id)
{
$article = Article::find($id);
return view(
'dashboard/edit-article',
[
'categories' => $this->categories(),
'tags' => $this->tags(),
'selected_tags' => $this->tags()->pluck('id')->toArray(),
'article' => $article
]
);
}
public function update(Request $request, $id)
{
$validator = Validator::make($request->all(), $this->rules, $this->messages);
if ($validator->fails()) {
return redirect()->back()->withErrors($validator->errors())->withInput();
}
$fields = $validator->validated();
$article = Article::find($id);
// If a new image is uploaded, set it as the article image
// Otherwise, set the old image...
if (isset($request->image)) {
$imageName = md5(time()) . Auth::user()->id . '.' . $request->image->extension();
$request->image->move(public_path('images/articles'), $imageName);
} else {
$imageName = $article->image;
}
$article->title = $request->get('title');
$article->short_description = $request->get('short_description');
$article->category_id = $request->get('category_id');
$article->tags[] = $request->get('tags[]');
$article->featured = $request->has('featured');
$article->image = $request->get('image') == 'default.jpg' ? 'default.jpg' : $imageName;
$article->content = $request->get('content');
// Save changes to the article
$article->save();
//Attach tags to article
if ($request->has('tags')) {
$article->tags()->attach($request->tags);
}
return redirect()->route('dashboard.articles')->with('success', 'The article titled "' . $article->title . '" was updated');
}
< /code>
Das Formular zur Bearbeitung eines Artikels: < /p>
@csrf
{{ __('Title') }}
@error('title')
[b]{{ $message }}[/b]
@enderror
{{ __('Short description') }}
@error('short_description')
[b]{{ $message }}[/b]
@enderror
{{ __('Category') }}
Pick a category
@foreach ($categories as $category)
id == $article->category->id ? 'selected' : '' }}>{{ $category->name }}
@endforeach
@error('category_id')
[b]{{ $message }}[/b]
@enderror
{{ __('Tags') }}
@foreach ($tags as $tag)
id, $selected_tags) ? 'selected' : '' }}>
{{ $tag->name }}
@endforeach
Featured article?
featured) ? 'checked' : '' }}>
{{ __('Toggle') }}
{{ __('Article image') }}
@error('image')
[b]{{ $message }}[/b]
@enderror
{{ __('Content') }}
{{ old('content', $article->content) }}
@error('content')
[b]{{ $message }}[/b]
@enderror
{{ __('Update') }}
< /code>
Aus einem Grund, warum ich nicht in der Lage war, zu erkennen, fällt die Aktualisierungsmethode mit dem folgenden Fehler fehl: < /p>
Integrity constraint violation: 1062 Duplicate entry '306-1' for key 'PRIMARY' (SQL: insert into `article_tag` (`article_id`, `tag_id`)