Skip to content

Instantly share code, notes, and snippets.

@AlexandreGerault
Created October 1, 2019 14:49
Show Gist options
  • Save AlexandreGerault/14468c57ba2e30ff77a26a5c608399ee to your computer and use it in GitHub Desktop.
Save AlexandreGerault/14468c57ba2e30ff77a26a5c608399ee to your computer and use it in GitHub Desktop.
<?php
namespace App\Models\App;
use Eloquent;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;
/**
* App\Models\App\CompanyData
*
* @property int $id
* @property int $accept_offers
* @property int $partnership
* @property int $bank_funding
* @property int $wcr
* @property int $shareholding
* @property int $looking_for_funding
* @property int $looking_for_accompaniment
* @property int $share_capital
* @property int $employees_number
* @property int $clients_number
* @property int $turnover
* @property int $turnover_progression
* @property int $average_monthly_turnover
* @property int $logistic_cost
* @property int $marketing_cost
* @property int $banking_investment
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
* @method static Builder|CompanyData newModelQuery()
* @method static Builder|CompanyData newQuery()
* @method static Builder|CompanyData query()
* @method static Builder|CompanyData whereAcceptOffers($value)
* @method static Builder|CompanyData whereAverageMonthlyTurnover($value)
* @method static Builder|CompanyData whereBankFunding($value)
* @method static Builder|CompanyData whereBankingInvestment($value)
* @method static Builder|CompanyData whereClientsNumber($value)
* @method static Builder|CompanyData whereCreatedAt($value)
* @method static Builder|CompanyData whereEmployeesNumber($value)
* @method static Builder|CompanyData whereId($value)
* @method static Builder|CompanyData whereLogisticCost($value)
* @method static Builder|CompanyData whereLookingForAccompaniment($value)
* @method static Builder|CompanyData whereLookingForFunding($value)
* @method static Builder|CompanyData whereMarketingCost($value)
* @method static Builder|CompanyData wherePartnership($value)
* @method static Builder|CompanyData whereShareCapital($value)
* @method static Builder|CompanyData whereShareholding($value)
* @method static Builder|CompanyData whereTurnover($value)
* @method static Builder|CompanyData whereTurnoverProgression($value)
* @method static Builder|CompanyData whereUpdatedAt($value)
* @method static Builder|CompanyData whereWcr($value)
* @mixin Eloquent
*/
class CompanyData extends Model
{
/**
* @return mixed
*/
public function structure () {
return $this->morhOne(Structure::class, 'data');
}
}
<?php /** @noinspection PhpUndefinedClassInspection */
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateStructuresTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('structures', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('comment');
$table->string('phone_number');
$table->string('address');
$table->integer('siren');
$table->bigInteger('siret');
$table->boolean('validated')->default(false);
/**
* Polymorphic relation keys
*/
$table->integer('data_id')->nullable()->default(null);
$table->string('data_type');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('structures');
}
}
<?php
namespace App\Models\App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Carbon;
use App\User;
use Eloquent;
/**
* App\Models\App\Structure
*
* @property-read Collection|Structure[] $followers
* @property-read Collection|Structure[] $following
* @property-read Collection|User[] $members
* @property-read Collection|News[] $news
* @property-read Collection|UserStructure[] $structureUsers
* @method static Builder|Structure newModelQuery()
* @method static Builder|Structure newQuery()
* @method static Builder|Structure query()
* @method static Builder|Structure searchByName($name)
* @mixin Eloquent
* @property int $id
* @property string $name
* @property string $comment
* @property int $siren
* @property int $siret
* @property int $validated
* @property int $data_id
* @property string $data_type
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
* @method static Builder|Structure whereComment($value)
* @method static Builder|Structure whereCreatedAt($value)
* @method static Builder|Structure whereId($value)
* @method static Builder|Structure whereName($value)
* @method static Builder|Structure whereSiren($value)
* @method static Builder|Structure whereSiret($value)
* @method static Builder|Structure whereType($value)
* @method static Builder|Structure whereUpdatedAt($value)
* @method static Builder|Structure whereValidated($value)
* @property string $phone_number
* @property string $address
* @property-read Collection|Structure[] $data
* @method static Builder|Structure whereAddress($value)
* @method static Builder|Structure whereDataId($value)
* @method static Builder|Structure whereDataType($value)
* @method static Builder|Structure wherePhoneNumber($value)
*/
class Structure extends Model
{
protected $guarded = [];
/**
* Scope a query to search structures by name.
*
* @param Builder $query
* @param String $name
* @return mixed
*/
public function scopeSearchByName($query, $name)
{
return $query->where('name', 'LIKE', '%' . $name . '%');
}
/**
* @return HasMany
*/
public function structureUsers()
{
return $this->hasMany(UserStructure::class, 'structure_id');
}
/**
* @return Builder|BelongsToMany
*/
public function members()
{
$structure = $this;
return $this->belongsToMany(User::class, 'users_structures', 'structure_id', 'user_id')
->whereHas('UserStructure', function (Builder $q) use ($structure) {
$q->where('status', '=', config('enums.structure_membership_request_status.ACCEPTED'))
->where('structure_id', '=', $structure->id);
});
}
/**
* Returns an array of data depending on the structure type
*
* @return MorphTo
*/
public function data()
{
return $this->morphTo();
}
/**
* @return HasMany
*/
public function news()
{
return $this->hasMany(News::class, 'structure_id');
}
/**
* @return News|Builder
*/
public function timeline()
{
return News::byFollowersOf(auth()->user()->userStructure->structure);
}
/**
* @return BelongsToMany
*/
public function following()
{
return $this->belongsToMany(Structure::class, 'followers', 'follower_id', 'following_id');
}
/**
* @return BelongsToMany
*/
public function followers()
{
return $this->belongsToMany(Structure::class, 'followers', 'following_id', 'follower_id');
}
/**
* Determine whether the structure is subscribed to the target timeline
*
* @param Structure : The target structure
*
* @return boolean
*/
public function follows(Structure $structure)
{
return $this->following->contains($structure);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment