Skip to main content

Relation Managers: Attach or Create a New Tag

Premium
3:44

The Full Lesson is Only for Premium Members

Want to access all of our courses? (31 h 16 min)

You also get:

55 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

E
Emruardo ✓ Link copied!

Error throwed while deleting tags which are attached to any product and vice versa.

To delete Product or Tag without Error

// ProductResource
// ---
Tables\Actions\DeleteBulkAction::make()->before(
		function (Collection $records) {
				$records->each(function (Product $record){
						$record->tags()->detach();
				});
		}
),
// ---
//--------------------------------------------------------------------------


// EditProduct
// ---
Actions\DeleteAction::make()->before(
		function (Product $record){
				$record->tags()->detach();
		}
),
// ---
//--------------------------------------------------------------------------


// TagResource
// ---
->bulkActions([
		Tables\Actions\DeleteBulkAction::make()->before(
				function (Collection $records) {
						$records->each(function (Tag $record){
								$record->products()->detach();
						});
				}
		),
]);
// ---


//--------------------------------------------------------------------------
// EditTag
// ---
Actions\DeleteAction::make()->before(
		function (Tag $record){
				$record->products()->detach();
		}
),
// ---


//--------------------------------------------------------------------------
// TagsRelationManager
// ---
->actions([
		Tables\Actions\DetachAction::make(),
		Tables\Actions\EditAction::make(),
		Tables\Actions\DeleteAction::make()->before(
				function (Tag $record){
						$record->products()->detach();
				}
		),
])
->bulkActions([
		Tables\Actions\DeleteBulkAction::make()->before(
				function (Collection $records) {
						$records->each(function (Tag $record){
								$record->products()->detach();
						});
				}
		),
]);
// ---
D
dascorp ✓ Link copied!

Can we elaborate on this lesson with slightly more advanced example likie one with Project, Member and Responsibility.

Member BelongsToMany Projects with Pivot Responsibility. Is this at all possible to attach new Member to Project with multiselected responsibilities in fillamentPHP?

PK
Povilas Korop ✓ Link copied!

I don't have the answer at the moment, but it's a good candidate for a future tutorial, adding on the to-do list of topics for the upcoming month or so.

N
Nerijus ✓ Link copied!

dascorp need more info on your example. Are you creating records from the relation manager? What do you mean by multiselected responsibilities? How they should be in the DB? It would be best if you would make some starter repository. Thanks. Edit: after rethinking how else would do add records without relation manager? Everything is in docs

HH
Halil Hodžić ✓ Link copied!

how attach user id who creating post for example we have posts table and we want to each post belongs to userId who creat it? we have user_id in posts table.

PK
Povilas Korop ✓ Link copied!

I would do it in the Eloquent Observer: here's the article about it.

IM
Ismail Mahmoud ✓ Link copied!

why the relationManager only appers in edit form, not also in create from?

IM
Ismail Mahmoud ✓ Link copied!

also how to use the relationManager in create form?