-
app/Models/Team.php
Open in GitHubuse App\Models\Traits\HasIconFile; use Illuminate\Database\Eloquent\Model; class Team extends Model { use HasIconFile; // }
-
app/Models/Traits/HasIconFile.php
Open in GitHubuse App\Models\File; use Exception; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasOne; use Illuminate\Http\UploadedFile; trait HasIconFile { function iconfile() { return $this->hasOne('App\Models\File', 'model_id')->where('model_class', '=', get_class($this)); } function saveUploadedFile(UploadedFile $file){ if ($this->iconfile !== null) { $this->iconfile->delete(); } $icon = File::saveFileToDB($file, $this, 'uploads'); $this->icon_file_id = $icon->id; $this->save(); } }
-
resources/views/common/team/details.blade.php
Open in GitHub@isset($model) {{ Form::model($model, ['route' => ['team.update', $model->public_key], 'method' => 'patch', 'files' => true]) }} @else {{ Form::open(['route' => 'team.savenew', 'files' => true]) }} @endisset // <div class="form-group{{ $errors->has('logo') ? ' has-error' : '' }}"> {!! Form::label('logo', __('Logo')) !!} {!! Form::file('logo', ['class' => 'form-control']) !!} </div> //
-
app/Http/Controllers/TeamController.php
Open in GitHubuse App\Http\Requests\Tag\TagFormRequest; use App\Models\Team; class TeamController extends Controller { public function store(TeamFormRequest $request, Team $team = null) { $new = $team === null; if ($new) { $team = new Team(); $team->name = $request->get('name'); $team->public_key = Team::generateRandomPublicKey(); } $team->description = $request->get('description'); $team->invite_code = Team::generateRandomPublicKey(12, 'invite_code'); $team->icon_file_id = -1; if ($team->save()) { $logo = $request->file('logo'); if ($logo !== null) { try { $team->saveUploadedFile($logo); } catch (Exception $ex) { if ($new) { $team->delete(); } throw $ex; } } if ($new) { // If saving team + logo was successful, save our own user as its first member $team->addMember(Auth::user(), 'admin'); } } return $team; } // }