Last active
March 28, 2019 08:50
-
-
Save nickdavies791/2b68fb57d7f4a3ea615b45bd492e176b to your computer and use it in GitHub Desktop.
A few snippets from Asset Manager
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App; | |
| use Illuminate\Database\Eloquent\Model; | |
| class Asset extends Model | |
| { | |
| /** | |
| * The attributes that are mass assignable. | |
| * | |
| * @var array | |
| */ | |
| protected $fillable = [ | |
| 'school_id', 'category_id', 'type_id', 'name', 'tag', 'serial_number', 'notes' | |
| ]; | |
| /** | |
| * Disable all timestamp fields. | |
| * @var boolean | |
| */ | |
| public $timestamps = false; | |
| /** | |
| * Returns the school associated with an asset | |
| */ | |
| public function school() | |
| { | |
| return $this->belongsTo(School::class); | |
| } | |
| /* | |
| * Returns the category associated with an asset | |
| */ | |
| public function category() | |
| { | |
| return $this->belongsTo(Category::class); | |
| } | |
| /* | |
| * Returns the type associated with an asset | |
| */ | |
| public function type() | |
| { | |
| return $this->belongsTo(Type::class); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <template> | |
| <div class="form-group"> | |
| <label for="category_id">Category</label> | |
| <select v-model="category" class="form-control" id="category_id" name="category_id"> | |
| <option v-for="category in categories" :value="category.id">{{ category.name }}</option> | |
| </select> | |
| </div> | |
| </template> | |
| <script> | |
| export default { | |
| data() { | |
| return { | |
| type: null, | |
| category: null, | |
| categories: [], | |
| } | |
| }, | |
| created() { | |
| Event.$on('typeChanged', newType => { | |
| this.type = newType; | |
| this.getCategories(); | |
| }); | |
| }, | |
| methods: { | |
| getCategories() { | |
| axios.get('/api/categories', { | |
| params: { | |
| 'type': this.type, | |
| } | |
| }).then(response => (this.categories = response.data)) | |
| } | |
| } | |
| } | |
| </script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App; | |
| use Illuminate\Database\Eloquent\Model; | |
| /** | |
| * A few mutators and accessors | |
| */ | |
| class Finance extends Model { | |
| ... | |
| /** | |
| * Gets the purchase cost in pounds. | |
| * | |
| * @param $value | |
| * @return float|int | |
| */ | |
| public function getPurchaseCostAttribute($value) | |
| { | |
| return $value / 100; | |
| } | |
| /** | |
| * Sets the purchase cost in pence. | |
| * | |
| * @param $value | |
| */ | |
| public function setPurchaseCostAttribute($value) | |
| { | |
| $this->attributes['purchase_cost'] = $value * 100; | |
| } | |
| ... | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace Tests\Unit; | |
| use App\School; | |
| use Tests\TestCase; | |
| use Illuminate\Foundation\Testing\WithFaker; | |
| use Illuminate\Foundation\Testing\RefreshDatabase; | |
| class SchoolTest extends TestCase | |
| { | |
| use RefreshDatabase; | |
| /* | |
| * Test a new school can be stored | |
| */ | |
| public function test_a_school_can_be_stored() | |
| { | |
| $school = factory(School::class)->create([ | |
| 'name' => 'Test School' | |
| ]); | |
| $this->assertDatabaseHas('schools', [ | |
| 'id' => $school->id, | |
| 'name' => 'Test School' | |
| ]); | |
| } | |
| /* | |
| * Test an existing school can be updated | |
| */ | |
| public function test_a_school_can_be_updated() | |
| { | |
| $school = factory(School::class)->create([ | |
| 'name' => 'Test School' | |
| ]); | |
| $school->update([ | |
| 'name' => 'Test School Updated' | |
| ]); | |
| $this->assertDatabaseHas('schools', [ | |
| 'id' => $school->id, | |
| 'name' => 'Test School Updated' | |
| ]); | |
| } | |
| /* | |
| * Test a school can be destroyed | |
| */ | |
| public function test_a_school_can_be_destroyed() | |
| { | |
| $school = factory(School::class)->create([ | |
| 'name' => 'Test School Deleted' | |
| ]); | |
| $school->destroy($school->id); | |
| $this->assertDatabaseMissing('schools', [ | |
| 'id' => $school->id, | |
| 'name' => 'Test School Deleted' | |
| ]); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace Tests\Feature; | |
| use App\Role; | |
| use App\User; | |
| use Tests\TestCase; | |
| use Illuminate\Foundation\Testing\WithFaker; | |
| use Illuminate\Foundation\Testing\RefreshDatabase; | |
| class StoreSchoolValidationTest extends TestCase | |
| { | |
| use RefreshDatabase; | |
| use WithFaker; | |
| /* | |
| * Tests the name field is required | |
| */ | |
| public function test_name_field_is_required() | |
| { | |
| $role = factory(Role::class)->create(['name' => 'Administrator']); | |
| $user = factory(User::class)->create(['role_id' => $role->id]); | |
| $response = $this->actingAs($user)->post(route('schools.store')); | |
| $response->assertSessionHasErrors('name'); | |
| $this->assertEquals(session('errors')->get('name')[0], 'Please enter a name for the school'); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment