Courses

Laravel Filament Admin v2: Practical Course

Relation Managers: Attach or Create a New Tag

Previous: Product Tags: Simple MultiSelect
avatar

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();
						});
				}
		),
]);
// ---
avatar
You can use Markdown
avatar

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?

avatar

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.

avatar

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

avatar
You can use Markdown
avatar

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.

avatar

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

avatar
You can use Markdown
avatar

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

avatar

also how to use the relationManager in create form?

avatar
You can use Markdown
avatar
You can use Markdown