mopo922 / laraveltreats Goto Github PK
View Code? Open in Web Editor NEWA collection of goodies for Laravel 5.
License: MIT License
A collection of goodies for Laravel 5.
License: MIT License
I recently found this, and it perfectly handles what I wanted to do. However one problem with it is that it doesn't fire (at least) the eloquent.deleted
event.
If you look at this issue - laravel/framework#2536 I was originally thinking that it was this that was the problem. However if you do it as the issue explains;
Analytic::where('id', 2)->first()->delete();
or
$analytics = Analytic::where('id', '>', 100)->get();
foreach ($analytics as $analytic) {
$analytic->delete();
}
or
Analytic::where('id', '>', 100)->get()->each(function($analytic) {
$analytic->delete();
});
Basically loading the data into memory by calling first()
or using the each
function on the collection.
Unfortunately the only way I have found so far to get the event to fire was to manually check the boolean response of delete()
and then fire the event myself;
$analytic = Analytic::where('id', 100)->first();
$deleted = $analytic->delete();
if ($deleted) {
event("eloquent.deleted: " . Analytic::class, [
$analytic,
]);
}
return [
"success" => $deleted,
];
I don't know if the other events fire at the moment, but at least the deleting
and deleted
events aren't firing.
Hi,
Any reason why php 5.6 is not supported?
I found a bug when using chunk on an Eloquent model instead of get.
Laravel calls a function enforceOrderBy that orders the query based on the primary key.
/**
* Add a generic "order by" clause if the query doesn't already have one.
*
* @return void
*/
protected function enforceOrderBy()
{
if (empty($this->query->orders) && empty($this->query->unionOrders)) {
$this->orderBy($this->model->getQualifiedKeyName(), 'asc');
}
}
/**
* Get the table qualified key name.
*
* @return string
*/
public function getQualifiedKeyName()
{
return $this->qualifyColumn($this->getKeyName());
}
/**
* Qualify the given column name by the model's table.
*
* @param string $column
* @return string
*/
public function qualifyColumn($column)
{
if (Str::contains($column, '.')) {
return $column;
}
return $this->getTable().'.'.$column;
}
You can circumvent this by adding your own orderBy but perhaps you could override the getQualifiedKeyName, qualifyColumn or the getKeyName function to prevent this from being necessary?
Why is php version 7 required? I work with servers contracted with php 5.6, and for that reason alone I can not install this very practical library through composer.json, and instead, I have included the files manually, and it works well even with that older version of php It is not a serious matter, but if it is necessary to maintain the neat structure of the project, put the oldest version that is supported, unless there is a precise reason why it has been put version 7 as required.
Hi there. First off, the HasCompositePrimaryKey
trait has saved me from a major headache, many thanks for creating this!
Second, is there a reason why this has the php requirement set to 7.0? I don't think I see anything that cannot run in 5.6 but I've only looked in that particular trait.
I'm currently migrating a project from 5.6 and evaluating this library but would still need to run this under 5.6 for some time before updating to 7.0.
Declaration of LaravelTreats\Model\Traits\HasCompositePrimaryKey::setKeysForSaveQuery(Illuminate\Database\Eloquent\Builder $query) should be compatible with Illuminate\Database\Eloquent\Model::setKeysForSaveQuery($query)
Hi,
I see the methods setKeysForSaveQuery() and find() in the trait, but I was wondering how the delete() would work ? No risk of deleting everything
Laravel 5.7
Need to handle Model::getQualifiedKeyName or Model::qualifyColumn
because of
\Illuminate\Database\Concerns\BuildsQueries::chunk
and
\Illuminate\Database\Eloquent\Builder::enforceOrderBy
because Str::contains use mb_strpos which expect $hastack as a string
When attempting to execute a hasManyThrough relationship via a table with a composite key, Laravel attempts to assume the JOIN-ON field to be the primary key assigned to the pivot model. If the key is an array using your HasCompositePrimaryKey trait, you end up with an "Array to string conversion" error. This appears to be happening in Model::getQualifiedKeyName() when it calls getKeyName(). That means, just for a start, you'd have to be mindful of all 10 places getQualifiedKeyName gets called, and all 29 places getKeyName gets called, all currently expecting a string response.
Collection method first throws an exception when using HasCompositePrimaryKey:
$sources = $sources->first(function($model) { return $model->getKey() === 1; });
[ErrorException]
array_key_exists(): The first argument should be either a string or an integerException trace:
() at /git/api/src/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php:311
Illuminate\Foundation\Bootstrap\HandleExceptions->handleError() at n/a:n/a
array_key_exists() at /git/api/src/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php:311
Illuminate\Database\Eloquent\Model->getAttribute() at /git/api/src/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1265
Illuminate\Database\Eloquent\Model->getKey() at /git/api/src/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Collection.php:34
Illuminate\Database\Eloquent\Collection->Illuminate\Database\Eloquent{closure}() at n/a:n/a
call_user_func() at /git/api/src/vendor/laravel/framework/src/Illuminate/Support/Arr.php:175
Illuminate\Support\Arr::first() at /git/api/src/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Collection.php:35
I am not sure how to make it work with hasOne
Code is
Program::with('type')->find(['id' => 1, 'year' => 2018]);
in the model
public function type()
{
return $this->hasOne('Type', 'id', 'type_id');
}
I get
A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
Filename: Eloquent/Model.php
Line Number: 1149
An uncaught Exception was encountered
Type: Illuminate\Database\QueryException
Message: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'programs.Array' in 'where clause' (SQL: select * from `programs` where `programs`.`Array` in (1, 2018))
Filename: /shared/httpd/humber/hq/application/vendor/illuminate/database/Connection.php
Line Number: 647
Thanks
PS: I saw there might be a new version coming soon, how soon?
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.