Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sngrl/f36c6856b868043fd5296181d6f1472e to your computer and use it in GitHub Desktop.
Save sngrl/f36c6856b868043fd5296181d6f1472e to your computer and use it in GitHub Desktop.
<?php namespace App\Relations;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\Relation;
/**
* 1. Define relation method in your model:
* > public function foo()
* > {
* > return new EmptyRelation();
* > }
*
* 2. Create file app/Relations/EmptyRelation.php and paste code from this gist.
*
* 3. Do not forget to run artisan dump-autoload
*/
class EmptyRelation extends Relation
{
public function __construct()
{
}
/**
* Set the base constraints on the relation query.
*
* @return void
*/
public function addConstraints()
{
}
/**
* Add the constraints for a relationship count query.
*
* @param Builder $query
* @param Builder $parent
*
* @return Builder
*/
public function getRelationCountQuery(Builder $query, Builder $parent)
{
return $query;
}
/**
* Set the constraints for an eager load of the relation.
*
* @param array $models
*
* @return void
*/
public function addEagerConstraints(array $models)
{
}
/**
* Initialize the relation on a set of models.
*
* @param array $models
* @param string $relation
*
* @return array
*/
public function initRelation(array $models, $relation)
{
foreach ($models as $model) {
$model->setRelation($relation, null);
}
return $models;
}
/**
* Match the eagerly loaded results to their parents.
*
* @param array $models
* @param Collection $results
* @param string $relation
*
* @return array
*/
public function match(array $models, Collection $results, $relation)
{
return $models;
}
/**
* Get the results of the relationship.
*
* @return mixed
*/
public function getResults()
{
return $this->get();
}
/**
* Execute the query as a "select" statement.
*
* @param array $columns
*
* @return Collection
*/
public function get($columns = ['*'])
{
return new Collection();
#return $this->related->newCollection([]);
}
}
@dani821
Copy link

dani821 commented Sep 7, 2020

when I implemented this I got this error

public function meter_type()
	{
		if( $this->belongsTo('\App\Models\CRUD\MeterType', 'meter_type_id')->count() )
		{

			return $this->belongsTo('\App\Models\CRUD\MeterType', 'meter_type_id');
		
		}else{

			return new EmptyRelation();

		}
		
	}

Undefined property: Illuminate\Database\Eloquent\Collection::$meter_type_id (attribute)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment