johnnyfreeman / laravel-custom-relation Goto Github PK
View Code? Open in Web Editor NEWA custom relation for when stock relations aren't enough.
A custom relation for when stock relations aren't enough.
This package is exactly what i need. Unfortunately, I am getting this error:
Call to undefined method Illuminate\Database\Query\Builder::buildDictionary()
in Builder.php line 2448
at Builder->__call('buildDictionary', array(object(Collection)))
at Builder->buildDictionary(object(Collection))
at call_user_func_array(array(object(Builder), 'buildDictionary'), array(object(Collection))) in Builder.php line 1467
at Builder->__call('buildDictionary', array(object(Collection)))
at Builder->buildDictionary(object(Collection))
at call_user_func_array(array(object(Builder), 'buildDictionary'), array(object(Collection))) in Relation.php line 344
at Relation->__call('buildDictionary', array(object(Collection))) in Custom.php line 90
at Custom->buildDictionary(object(Collection)) in Custom.php line 90
at Custom->match(array(object(Category)), object(Collection), 'descendants') in Builder.php line 687
at Builder->loadRelation(array(object(Category)), 'descendants', object(Closure)) in Builder.php line 654
at Builder->eagerLoadRelations(array(object(Category))) in Builder.php line 333
at Builder->get(array('')) in Builder.php line 297
at Builder->first(array('')) in Builder.php line 171
at Builder->find('1') in GroupController.php line 21
As the title says.
if (isset($dictionary[$key = $model->getKey()])) {
Compare works only with the fields that exists in a model. By the way morph match
does like this:
if (isset($dictionary[$key = $model->getAttribute($this->localKey)])) {
Could u add possibility to define keys?
Now that #8 has been merged, I feel like we've learned the best way to tackle the issue of making a custom relation is by accepting callables/closures for the abstract Relation
methods rather than trying to find an implementation that fits every scenario. I don't like the readability of it though...
class User extends Model
{
use HasCustomRelations;
public function someOtherRelation()
{
return $this->custom(
SomeOtherModel::class,
function (...) {
// ...
},
function (...) {
// ...
},
function (...) {
// ...
},
function (...) {
// ...
},
);
}
}
Seems cluttered and I forget which Closure is which and have to reference the package. The introduction of a new RelationBuilder
object might be helpful for readability. Something like this...
class User extends Model
{
use HasCustomRelations;
public function someOtherRelation()
{
return $this->buildRelation()
->related(SomeOtherModel::class)
->addConstraints(function (...) {
// ...
})
->addEagerConstraints(function (...) {
// ...
})
->initRelation(function (...) {
// ...
})
->match(function (...) {
// ...
})
->getRelation();
}
}
While I like this better, my question now becomes, is this any better than just creating a new class that for each custom relation? Then you can just do this.
class User extends Model
{
use HasCustomRelations;
public function someOtherRelation()
{
return new MyCustomRelation(SomeOtherModel::query(), $this);
}
}
I guess on one hand you would have potentially a large amount of custom classes/files that just describe a single custom relationship, and on the other hand, you have a potentially large amount of these verbose RelationBuilder
methods on your models.
Maybe there's room for both? What are your thoughts?
after trying installing this package with compser:
php composer.phar require "johnnyfreeman/laravel-custom-relation"
I get this:
Using version ^0.0.3 for johnnyfreeman/laravel-custom-relation
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.
Problem 1
- Installation request for johnnyfreeman/laravel-custom-relation ^0.0.3 -> satisfiable by johnnyfreeman/laravel-custom-relation[0.0.3].
- Conclusion: remove laravel/framework v7.28.3
- Conclusion: don't install laravel/framework v7.28.3
- johnnyfreeman/laravel-custom-relation 0.0.3 requires laravel/framework ^5.2.0 -> satisfiable by laravel/framework[5.2.x-dev, 5.3.x-dev, 5.4.x-dev, 5.5.x-dev, 5.6.x-dev, 5.7.x-dev, 5.8.x-dev].
- Can only install one of: laravel/framework[5.5.x-dev, v7.28.3].
- Can only install one of: laravel/framework[5.6.x-dev, v7.28.3].
- Can only install one of: laravel/framework[5.7.x-dev, v7.28.3].
- Can only install one of: laravel/framework[5.8.x-dev, v7.28.3].
- Can only install one of: laravel/framework[5.2.x-dev, v7.28.3].
- Can only install one of: laravel/framework[5.3.x-dev, v7.28.3].
- Can only install one of: laravel/framework[5.4.x-dev, v7.28.3].
- Installation request for laravel/framework (locked at v7.28.3, required as ^7.0) -> satisfiable by laravel/framework[v7.28.3].
Installation failed, reverting ./composer.json to its original content.
so is there any fix to this?
btw thanks I just copy-pasted version 3 into my manually app. but with composer, updates are a lot easier.
Relation class don't have buildDictionary method.
it got this error
Call to undefined method LaravelCustomRelation\Relations\Custom::getKeys()
Hi,
Able to update composer.json
for Laravel 8? Thank you.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.