working with the collection of objects in laravel - laravel

Trying to work with the collection of objects.
the collection is NOT an Eloquent one but hand made Illuminate\Support\Collection
if i understood it right in the case of collection of objects i'm not able to use most of methods but only those which can use callback.
so, i have collection of objects:
and here is the code ($country = 'Russia'):
dump($this->countries);
$filtered = $this->countries->filter(function ($countryObj) use($country) {
dump($countryObj->name == $country);
return $countryObj->name == $country;
});
dd($filtered);
i expect the $filtered contains only one element, the one which return true (in our case Russia)
but instead of it i have the same collection of 3 elements.
here is the rest of classes to be sure they are collection related
use App\Services\Taxes\DataSourceInterface;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
abstract class JsonModel extends Collection implements DataSourceInterface
{
public function __construct()
{
parent::__construct($this->readDataFile(env('JSON_DATA_PATH')));
}
protected function readDataFile(string $path): array
{
$disk = Storage::disk('local');
try {
$dataObj = json_decode($disk->get($path), false, 10, JSON_THROW_ON_ERROR);
return $this->loadData($dataObj);
} catch (FileNotFoundException $e) {
Log::error('Storage ' . $e->getMessage() . ' was not found');
} catch (\JsonException $e) {
Log::error('Json DataFile: ' . $e->getMessage());
}
return [];
}
abstract protected function loadData(object $dataObject): array;
}
class JsonCountries extends JsonModel
{
public function loadData(object $dataObject): array
{
$data = array_filter($dataObject->countries, function ($item){
unset($item->states);
return true;
});
return $data;
}
}

problem was in new static which used inside laravel methods which return instance of collection and in fact that i dont expect array entry in my constructor.
choice between empty array and file reading fixes the problem
abstract class JsonModel extends Collection implements DataSourceInterface
{
public function __construct($dataArr = [])
{
if(!is_array($dataArr))
$dataArr = $this->readDataFile(env('JSON_DATA_PATH'));
parent::__construct($dataArr);
}

Related

How to add custom methods to eloquent model in a way we can chain them?

What I want is add methods to eloquent models so I can chain them, for example:
class MovieResolver
{
public function getMoviesFeaturingToday(array $args)
{
// Movie is an Eloquent model
$movie = (new Movie())
->getMoviesFeaturingTodayOnTheater($args['movieTheaterId'])
->getBySessionCategory($args['sessioncategory']);
// And keep doing some operations if necessary, like the code below.
// I cannot call the get() method unless I finish my operations.
return $movie->whereDate('debut', '<=', Carbon::today())
->orderBy('debut', 'desc')
->get();
}
}
But adding these methods to the model:
class Movie extends Model
{
public function getMoviesFeaturingTodayOnTheater($theaterId)
{
return $this->whereHas(
'sessions.entries.movieTheaterRoom',
function ($query) use ($theaterId) {
$query->where('movie_theater_id', $theaterId);
}
);
}
public function getBySessionCategory($sessionCategory)
{
return $this->whereHas(
);
}
}
Results in the following error:
Call to undefined method Illuminate\Database\Eloquent\Builder::getMoviesFeaturingTodayOnTheater()
But why? What I'm doing wrong?
This is done using Query Scopes. So try this in your model instead:
public function scopeMoviesFeaturingTodayOnTheater($query, $theaterId)
{
return $query->whereHas(
'sessions.entries.movieTheaterRoom',
function ($query) use ($theaterId) {
$query->where('movie_theater_id', $theaterId);
}
);
}
public function scopeBySessionCategory($query, $sessionCategory)
{
return $query->whereHas(
// ...
);
}
Then to use it you do:
Movie::moviesFeaturingTodayOnTheater($args['movieTheaterId'])
->bySessionCategory($args['sessioncategory']);;

Laravel - Get value instances via facades

In Illuminate\Support\Facades\Facade abstract In method
protected static function resolveFacadeInstance($name)
{
if (is_object($name)) {
return $name;
}
if (isset(static::$resolvedInstance[$name])) {
return static::$resolvedInstance[$name];
}
return static::$resolvedInstance[$name] = static::$app[$name];
}
static::$app is an instance of Application. And static::$app[$name] that like access value of array, and i don't understand that, What technique here?
ex: static::$app['router'] it return instance of Router. Seem that get values of protected $instances in Illuminate\Container\Container
I think it like example ? but got FATAL ERROR Uncaught Error: Cannot use object of type Foo as array
class Foo
{
public $bar = 'barValue';
}
$foo = new Foo();
echo $foo['bar'];
If you check the API of Illuminate\Container\Container, you will notice that it implements ArrayAccess and consequently the following methods.
offsetExists()
offsetGet()
offsetSet()
offsetUnset()
ArrayAccess lets you access objects as arrays. Here's a very simplistic example of a Container.
<?php
class Container implements ArrayAccess {
private $items = array();
public function __construct() {
$this->items = [
'one' => 1,
'two' => 2,
'three' => 3,
];
}
public function offsetSet($offset, $value) {
if (is_null($offset)) {
$this->items[] = $value;
} else {
$this->items[$offset] = $value;
}
}
public function offsetExists($offset) {
return isset($this->items[$offset]);
}
public function offsetUnset($offset) {
unset($this->items[$offset]);
}
public function offsetGet($offset) {
return isset($this->items[$offset]) ? $this->items[$offset] : null;
}
}
$container = new Container();
echo $container['one']; // outputs 1
$container['four'] = 4; // adds 4 to $items.
echo $container['four']; // outputs 4
As you can see, you can access the Container object as an array since it implements ArrayAccess.
It also doesn't matter if the items property is not publicly accessible. In any case, the implementation of ArrayAccess means that it will allow us to retrieve those values as if they were in an array.

Laravel - Delete if no relationship exists

Below is the one of the model. I would like to delete a Telco entry only if no other model is referencing it? What is the best method?
namespace App;
use Illuminate\Database\Eloquent\Model;
class Telco extends Model
{
public function operators()
{
return $this->hasMany('App\Operator');
}
public function packages()
{
return $this->hasMany('App\Package');
}
public function topups()
{
return $this->hasMany('App\Topup');
}
public function users()
{
return $this->morphMany('App\User', 'owner');
}
public function subscribers()
{
return $this->hasManyThrough('App\Subscriber', 'App\Operator');
}
}
You can use deleting model event and check if there any related records before deletion and prevent deletion if any exists.
In your Telco model
protected static function boot()
{
parent::boot();
static::deleting(function($telco) {
$relationMethods = ['operators', 'packages', 'topups', 'users'];
foreach ($relationMethods as $relationMethod) {
if ($telco->$relationMethod()->count() > 0) {
return false;
}
}
});
}
$relationships = array('operators', 'packages', 'topups', 'users', 'subscribers');
$telco = Telco::find($id);
$should_delete = true;
foreach($relationships as $r) {
if ($telco->$r->isNotEmpty()) {
$should_delete = false;
break;
}
}
if ($should_delete == true) {
$telco->delete();
}
Well, I know this is ugly, but I think it should work. If you prefer to un-ugly this, just call every relationship attributes and check whether it returns an empty collection (meaning there is no relationship)
If all relationships are empty, then delete!
After seeing the answers here, I don't feel copy pasting the static function boot to every models that need it. So I make a trait called SecureDelete. I put #chanafdo's foreach, inside a public function in SecureDelete.
This way, I can reuse it to models that need it.
SecureDelete.php
trait SecureDelete
{
/**
* Delete only when there is no reference to other models.
*
* #param array $relations
* #return response
*/
public function secureDelete(String ...$relations)
{
$hasRelation = false;
foreach ($relations as $relation) {
if ($this->$relation()->withTrashed()->count()) {
$hasRelation = true;
break;
}
}
if ($hasRelation) {
$this->delete();
} else {
$this->forceDelete();
}
}
}
Add use SecureDelete to the model that needs it.
use Illuminate\Database\Eloquent\Model;
use App\Traits\SecureDelete;
class Telco extends Model
{
use SecureDelete;
public function operators()
{
return $this->hasMany('App\Operator');
}
// other eloquent relationships (packages, topups, etc)
}
TelcoController.php
public function destroy(Telco $telco)
{
return $telco->secureDelete('operators', 'packages', 'topups');
}
In addition, instead of Trait, you can also make a custom model e.g BaseModel.php that extends Illuminate\Database\Eloquent\Model, put the function secureDelete there, and change your models to extends BaseModel.

Custom Eloquent relation which sometimes returns $this Model

I have a Model Text which has a 1-to-many-relation called pretext(), returning a 1-to-many-Relationshop to Text, like so:
class Text extends Model
{
public function pretext(){
return $this->belongsTo('App\Models\Text', 'pretext_id');
}
public function derivates(){
return $this->hasMany('App\Models\Text', 'pretext_id');
}
}
If a $text does not have any pretext (which, in my scenario, means $text['pretext_id'] == 0) $text->pretext() shall return the $text itself. When I try
public function pretext(){
if ( $this->belongsTo('App\Models\Text', 'pretext_id') ) {
return $this->belongsTo('App\Models\Text', 'pretext_id');
}
else {
return $this;
}
}
I get the error
local.ERROR: LogicException: Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation
when the else-part is executed. So my questions are:
How do I turn $this into an object of type Relation? or alternatively:
How can I achieve my goal on a different way?
Try dynamic props
class Text extends Model
{
protected $appends = ['content'];
public function pretext(){
return $this->belongsTo('App\Models\Text', 'pretext_id');
}
public function getContentAttribute(){
$pretext = $this->pretext()->get();
if ($pretext->count()) {
return $pretext;
}
return $this;
}
}
Then in controller or view if you have the instance
(consider optimizing it if you have N+1 issues)
$obj = Text::find(1);
dd($obj->content);
I think you can create another method that calling pretext() and check the returned value.
public function getPretext() {
$value = pretext();
return ($value)? $value : $this;
}

Eloquent relationships for not-existing model class

I would like to have in my applications many models/modules but some of them would be removed for some clients.
Now I have such relation:
public function people()
{
return $this->hasMany('People', 'model_id');
}
and when I run $model = Model::with('people')->get(); it is working fine
But what if the People model doesn't exist?
At the moment I'm getting:
1/1 ErrorException in ClassLoader.php line 386: include(...): failed
to open stream: No such file or directory
I tried with
public function people()
{
try {
return $this->hasMany('People', 'model_id');
}
catch (FatalErrorException $e) {
return null;
}
}
or with:
public function people()
{
return null; // here I could add checking if there is a Model class and if not return null
}
but when using such method $model = Model::with('people')->get(); doesn't work.
I will have a dozens of relations and I cannot have list of them to use in with. The best method for that would be using some empty relation (returning null) just to make Eloquent not to do anything but in this case Eloquent still tries to make it work and I will get:
Whoops, looks like something went wrong.
1/1 FatalErrorException in Builder.php line 430: Call to a member function
addEagerConstraints() on null
Is there any simple solution for that?
The only solution I could come up with is creating your own Eloquent\Builder class.
I've called it MyBuilder. Let's first make sure it gets actually used. In your model (preferably a Base Model) add this newEloquentBuilder method:
public function newEloquentBuilder($query)
{
return new MyBuilder($query);
}
In the custom Builder class we will override the loadRelation method and add an if null check right before addEagerConstraints is called on the relation (or in your case on null)
class MyBuilder extends \Illuminate\Database\Eloquent\Builder {
protected function loadRelation(array $models, $name, Closure $constraints)
{
$relation = $this->getRelation($name);
if($relation == null){
return $models;
}
$relation->addEagerConstraints($models);
call_user_func($constraints, $relation);
$models = $relation->initRelation($models, $name);
$results = $relation->getEager();
return $relation->match($models, $results, $name);
}
}
The rest of the function is basically the identical code from the original builder (Illuminate\Database\Eloquent\Builder)
Now simply add something like this in your relation function and it should all work:
public function people()
{
if(!class_exist('People')){
return null;
}
return $this->hasMany('People', 'model_id');
}
Update: Use it like a relationship
If you want to use it like you can with a relationship it gets a bit more tricky.
You have to override the getRelationshipFromMethod function in Eloquent\Model. So let's create a Base Model (Your model obviously needs to extend it then...)
class BaseModel extends \Illuminate\Database\Eloquent\Model {
protected function getRelationshipFromMethod($key, $camelKey)
{
$relations = $this->$camelKey();
if ( $relations instanceof \Illuminate\Database\Eloquent\Collection){
// "fake" relationship
return $this->relations[$key] = $relations;
}
if ( ! $relations instanceof Relation)
{
throw new LogicException('Relationship method must return an object of type '
. 'Illuminate\Database\Eloquent\Relations\Relation');
}
return $this->relations[$key] = $relations->getResults();
}
}
Now we need to modify the relation to return an empty collection
public function people()
{
if(!class_exist('People')){
return new \Illuminate\Database\Eloquent\Collection();
}
return $this->hasMany('People', 'model_id');
}
And change the loadRelation function in MyBuilder to check for the type collection instead of null
protected function loadRelation(array $models, $name, Closure $constraints)
{
$relation = $this->getRelation($name);
if($relation instanceof \Illuminate\Database\Eloquent\Collection){
return $models;
}
// ...
}

Resources