AfterValidation Hook on BelongsToMany Field in Laravel Nova - laravel

I have a table "articles", a table "images" and a pivot table article_image with unique constraint on 2 columns: 'article_id' and an additional column 'order_nr'. Now I need to check the uniqueness of article_id/order_id in Laravel Nova when attaching an image to an article.
I already found a solution for a unique validation rule with 2 columns with a AfterValidation hook here:
https://grrr.tech/posts/2021/nova-multi-column-validation/
The problem is that this solution is referring to a resource by itself and not to a "pivot resource".
I tried to put the afterValidation method into the resources 'Article' and 'Image', but the method is simply not accessed from here.
How can I set a afterValidation hook on a pivot resource, thus a BelongsToMany field? Or am I completely a on the wrong track and anybody knows a better solution for that? Thank you in advance!

Related

Backpack Laravel select2_from_ajax field returns results, but I can't select any

I'm implementing a 'select2_from_ajax' field using Backpack for Laravel.
I've implemented this in other places and it works correctly. But for some reason when implementing it this time it will not let me select any of the options and doesn't show the highlight when mousing over the options. It lists out the options correctly, I just can't select any of them.
The only thing I can think of is that the relationship it's trying to reference doesn't have a primary 'id' field in the database, but I'm not sure why that would affect this.
I have implemented both the index and show routes.
The issue was that the relationship field's primary key was not 'id' it was setup with a different column name.
I reworked the data structure so the foreign key referenced was pointing to a column labeled 'id'.

Laravel 6 database error : Integrity constraint violation

I am trying to set up the relations between my migrations in Laralvel 6, but I still have this error message and I do not understand why ....
When I want to publish an article, it takes the query but returns empty tables.
From the very limited information given, just by looking at the query it seems to be an issue with the user_id. In the insert stament there is no user_id inserted, which might be the problem? In your Article model, is user_id fillable?
If user_id isn't required, has it been made nullable in the migration for articles?
Please provide some examples of the migration, model and the method in the controller used for the create of the Article.

Relationships "has-one-through" alternative solution Laravel Voyager

I need to make a relation "has-one-through" but Voyager doesnt support it
The "Consum view" have to show the owner name.
https://i.imgur.com/nDrUgU2.jpg (DB Example)
I've already tried belongsToMany with pivot table but didn`t work!
Show a column from the requested table.
Why donĀ“t you change your relation, having an owner_id or a house_id instead of an owner_house_id in the consum table? Probably that solves it all.

how adding a fields to relationship Table in pivot table method using laravel

I want to design an online store. For each category of productions, we would have its own fields.
Connections between tables of fields and categories are done and displayed in the part of registration of productions.
At the end, the value of each field should be stored that it should contains the related table between table of fields and productions (in addition to/plus/+) the value of that field.
My problem is in this part and I want that this related table has an additional field that I could value it.
In the following figure, the picture of table and my connections are shown, if there is (any problem/ something wrong with it), I would appreciate you to help me.
View Relationship Images :
http://ir-up.ir/uploads/1416568805731.jpg
You'd simply need to add a withPivot when declaring the relationship:
return $this->belongsToMany('Role')->withPivot('foo', 'bar');
Extra attributes in pivot tables are covered in the docs, here - http://laravel.com/docs/4.2/eloquent#working-with-pivot-tables

Laravel 4 - how to use a unique validation rule / unique columns with soft deletes?

Assume, you are trying to create a new user, with a User model ( using soft deletes ) having a unique rule for it's email address, but there exists a trashed user within the database.
When trying to validate the new user's data, you will get a validation error, because of the existing email.
I made some kind of extra validation within my Controllers, but wouldn't it be nice to have it all within the Model?
Would you suggest creating a custom validation rule?
As I haven't found a clean solution now, I am interessted in how others solved this problem.
You can validate passing extra conditions:
'unique:users,deleted_at,NULL'
This sounds like an issue with your business logic rather than a technical problem.
The purpose of a soft delete is to allow for the possibility that the soft-deleted record may be restored in the future. However, if your application needs uniqueness of email (which is completely normal), you wouldn't want to both create a new user with that email address and be able to restore the old one as this would contravene the uniqueness requirement.
So if there is a soft deleted record with the email address for which you are adding as a new record, you should consider instead restoring the original record and applying the new information to it as an update, rather than trying to circumvent the uniqueness check to create a new record.
This is the best approach
'email' => 'required|email|unique:users,email,NULL,id,deleted_at,NULL',
It give you this query
select count(*) as aggregate from `users` where `email` = ? and `deleted_at` is null
Laravel offers "Additional Where Clauses".
My url validation rule (from the update model method) looks like this:
$rules['url'] = 'required|unique:pages,url,'.$page->id.',id,deleted_at,NULL';
This means that the url must be unique, must ignore the current page and ignore the pages where deleted_at id not NULL.
Hope this helps.
Your Eloquent model should have the $softDeletes property set. If so, then when you perform a WHERE check, like User::where('username', 'jimbob'), Eloquent will automatically add in the query WHERE deleted_at IS NULL... which excludes soft deleted items.
In laravel 6.2+ you can use
'email' => ['required', Rule::unique('users')->whereNull('deleted_at')]

Resources