John Davidson

php - Laravel PUT relationship

0 comments
Message:


Fiddling with Laravel and coming from Symfony, I'm trying to replicate some code.
I'm trying to PUT a Suggestion model (overwritting anything, even relationships) and wanted to know the proper way to overwrite the model.
Since tags attribute in fillable doesn't exist, I certainly get an error (Undefined column: 7 ERROR: column "tags" of relation "suggestions" does not exist).
Suggestions and tags both have their own tables and a pivot table that contains two foreign keys to both tables id.


Request & Response :


{
"id":2,
"content":"Magni.",
"tags":[{"id":13,"name":"MediumAquaMarine"}]
}

{
"id":2,
"content":"Magni.",
"tags":[{"id":10,"name":"Navy"},{"id":13,"name":"MediumAquaMarine"}]
}

public function update(Request $request, Suggestion $suggestion)
{
$validator = Validator::make($request->all(), [
'content' => 'required',
'tags.id' => 'numeric',
]);

if ($validator->fails()) {
return response()->json($validator->messages(), Response::HTTP_BAD_REQUEST);
}

$suggestion->fill($request->only($suggestion->getFillable()))->save();

return new SuggestionResource($suggestion);
}

class Suggestion extends Model
{
use HasFactory;

protected $fillable = ['content', 'tags'];

protected $with = ['tags'];

public function tags()
{
return $this->belongsToMany(Tag::class, 'suggestions_tags')->withTimestamps();
}
}

class Tag extends Model
{
use HasFactory;

protected $hidden = ['pivot'];

public function suggestions()
{
return $this->belongsToMany(Suggestion::class, 'suggestions_tags')->withTimestamps();
}
}



My Answer:
To update the relationships of a model in Laravel, you can use the `sync` method on the relationship. In your case, you can update the tags relationship of the Suggestion model by calling the `sync` method on the tags relationship.

Here's how you can update the Suggestion model with the tags relationship in your update method:

php
public function update(Request $request, Suggestion $suggestion)
{
$validator = Validator::make($request->all(), [
'content' => 'required',
'tags.*.id' => 'numeric',
]);

if ($validator->fails()) {
return response()->json($validator->messages(), Response::HTTP_BAD_REQUEST);
}

$suggestion->fill($request->only($suggestion->getFillable()))->save();

if ($request->has('tags')) {
$tagIds = collect($request->input('tags'))->pluck('id');
$suggestion->tags()->sync($tagIds);
}

return new SuggestionResource($suggestion);
}


In this code snippet, we first validate the request data to ensure that the `content` field is required and the `tags` array contains numeric `id` values. Then, we fill and save the Suggestion model with the provided data.

If the request contains `tags` data, we extract the `id` values from the request and use the `sync` method on the `tags` relationship to update the tags relationship of the Suggestion model.

This way, you can update the Suggestion model along with its relationships, specifically the tags relationship.

Rate this post

4 of 5 based on 2103 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us