Comments (16)
Can you share your use case with some sample data?
Is it an OR
constraint where the email address needs to be in email[]->to
or email[]->from
?
from eloquent-json-relations.
Can you share your use case with some sample data?
Is it an
OR
constraint where the email address needs to be inemail[]->to
oremail[]->from
?
I have a table contacts with an email column, I then have an inbound email table with 2 columns to & from both of which are JSON for example {"name": "intrflex", "email": "[email protected]"
i need to be able to do a HasManyJson to the 2 email fields to & from where the email is either the recipient or the receiver
from eloquent-json-relations.
but essentially yes its an OR
constraint new to this, unfortunately, its either going to be from the person or to the person
from eloquent-json-relations.
The package itself doesn't support that, but you can use one of my other packages:
https://github.com/staudenmeir/laravel-merged-relations
Create two HasManyJson
relationships (one for to
and one for from
) and combine them with mergedRelationWithModel()
.
from eloquent-json-relations.
I know this will work with the from column as that's only a JSON object, however, the to column is a JSON array? will this package still work
from eloquent-json-relations.
it returns the following error PDOException::("SQLSTATE[42S22]: Column not found: 1054 Unknown column 'inbound_emails.to[]' in 'field list'"
from eloquent-json-relations.
I'll look into it.
from eloquent-json-relations.
Thankyou, really trying to get this to work
from eloquent-json-relations.
What version of MySQL/MariaDB are you using?
from eloquent-json-relations.
10.4.13-MariaDB
from eloquent-json-relations.
public function mailTo(): HasManyJson
{
return $this->hasManyJson(InboundEmail::class, 'to[]->email' , 'email')->where('public', 1);
}
public function mailFrom(): HasManyJson
{
return $this->hasManyJson(InboundEmail::class, 'from->email', 'email')->where('public', 1);
}
thats both my relationships
and this is my schema
Schema::createMergeView('emailables', [(new Contact)->mailTo(), (new Contact)->mailFrom()]);
from eloquent-json-relations.
You're right, JSON array don't work out of the box. I see two options:
-
You extend the
HasManyJson
relationship in your project and add support for multiple foreign keys. Depending on your use case, this will take a while. -
You use the
laravel-merged-relations
package, but create the view manually (you need to add the rest of your columns):
$view = <<<EOT
CREATE VIEW `emailables` AS
WITH RECURSIVE `to_emails` AS
(SELECT *,
-1 AS `i`
FROM `inbound_emails`
UNION SELECT `inbound_emails`.*,
`to_emails`.`i` + 1
FROM `inbound_emails`
JOIN `to_emails` ON `inbound_emails`.`id`=`to_emails`.`id`
WHERE `to_emails`.`i` < JSON_LENGTH(`inbound_emails`.`to`) - 1 )
(SELECT *, JSON_UNQUOTE(JSON_EXTRACT(`inbound_emails`.`from`, '$."email"')) AS `laravel_foreign_key`,
'App\\\\Models\\\\InboundEmail' AS `laravel_model`,
'' AS `laravel_placeholders`,
'' AS `laravel_with`
FROM `inbound_emails`
WHERE `public` = 1 )
UNION
(SELECT `id`,
`to`,
`from`,
`public`,
JSON_UNQUOTE(JSON_EXTRACT(`to`, CONCAT('$[', `i`, '].email'))) `laravel_foreign_id`,
'App\\\\Models\\\\InboundEmail' AS `laravel_model`,
'' AS `laravel_placeholders`,
'' AS `laravel_with`
FROM `to_emails`
WHERE `public` = 1
AND `i` >= 0 );
EOT;
DB::statement($view);
Then you can create the merged relationship:
class Contact extends Model
{
use HasMergedRelationships;
public function emailables(): MergedRelation
{
return $this->mergedRelationWithModel(InboundEmail::class, 'emailables', 'email');
}
}
from eloquent-json-relations.
- id
- user_id
- public
- email_id
- message_id
- thread_id
- from
- to
- cc
- subject
- email_date
- message
- labels
- snippet
- deleted_at
- created_at
- updated_at
MySQL queries arent really my speciality, i dont even really understand where id add my other columns in ive included my database columns
from eloquent-json-relations.
- id
- user_id
- public
- email_id
- message_id
- thread_id
- from
- to
- cc
- subject
- email_date
- message
- labels
- snippet
- deleted_at
- created_at
- updated_at
MySQL queries arent really my speciality, i dont even really understand where id add my other columns in I've included my database columns
Okay scrap that message i worked it out but, Holy that query takes forever to run it doesn't even complete on a table of 20000 emails laravel returns a 504 time out
from eloquent-json-relations.
Just an update, I worked out a temporary fix, I've extended your MergedRelation swapped out the query so it searches for a foreign key like the email i'm trying to find, hopefully one day this package can support multiple foreign keys i found another packaged that merged yours with compoships but unfortunately the functionality i needed was released in the packages it used
from eloquent-json-relations.
Can you share the code of your solution for others?
from eloquent-json-relations.
Related Issues (20)
- pivot always return the same data on belongsToJson HOT 3
- Support for filtering by pivot value? HOT 5
- Support for custom key HOT 1
- BelongsToJson returns wrong results HOT 2
- hasMany - belongsToJson - hasManyJson HOT 4
- Artisan model:show failed HOT 1
- Need a basic Understanding for a json_encoded column HOT 6
- getAttribute() conflict HOT 1
- SqliteGrammar / Sqlite support HOT 11
- laravel compoships HOT 4
- Defining Json Relation in factory definition HOT 3
- Not an issue, really need a help HOT 2
- Nested Array of Objects throws array_key_exists(): Argument #2 ($array) must be of type array, null given HOT 2
- has and WhereHas doesn't work HOT 1
- One dimensional array HOT 5
- MySQL MEMBER OF () performance improvements HOT 5
- Is it possible to define a relationship using a key that can be nested at variable depths? HOT 14
- Great Package HOT 1
- String values fail for belongsToJson and HasManyJson HOT 4
- Many To Many Eager Loading Breaks with array_key_exists(): Argument #2 ($array) must be of type array, null given HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from eloquent-json-relations.