Skip to content

Instantly share code, notes, and snippets.

@ph4un00b
Last active September 18, 2015 21:21
Show Gist options
  • Save ph4un00b/49ae0f33e0a55b1b2026 to your computer and use it in GitHub Desktop.
Save ph4un00b/49ae0f33e0a55b1b2026 to your computer and use it in GitHub Desktop.
illuminate-angular-test
<?php
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
/**
* Class User
*/
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = array('password', 'remember_token');
protected $fillable = ['first_name', 'last_name', 'email', 'password', 'hash'];
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function notes()
{
return $this->hasMany('Note');
}
// then you can use something like this
// return Response::json(User::with(['notes.lesson'])->where('hash', $hash)->firstOrFail());
/**
* @return Illuminate\Support\Collection
*/
public function getLessonsWithNotes()
{
return static::with([
'notes' => function ($query)
{
$query->with('lesson');
$query->groupBy('lesson_id');
}
])
->findOrFail($this->id)->notes
->map(function ($note) { return $note->lesson->name; });
}
// then you can retrieve like
// Auth::user()->getLessonsWithNotes() and send a json response;
/** Write a function that will update the 'hash' column to a random string before creating a user */
// before sounds weird, did you mean after creation?
// or we could have some validation stuff or some crazy stuff on "creating static method"
public static function boot()
{
parent::boot();
static::created(function (User $model)
{
$model->update(['hash' => 'super_fancy_hash_service';
});
}
}
<?php
/**
* Class Note
*/
class Note extends \Eloquent {
protected $fillable = ['title', 'body'];
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function lesson()
{
return $this->belongsTo('Lesson');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user()
{
return $this->belongsTo('User');
}
}

notes

  • you should have some transformer layer to send a well formatted data and a protect you againts a database column change; I recommend http://fractal.thephpleague.com/
.config(function ($routeProvider) {
$routeProvider
.when('/web/products', {
templateUrl: 'views/web/products.html',
controllerAs: 'productCtrl',
controller: [function () {
var self = this;
self.products = [
{id: 1, name: 'Spanish course'},
{id: 2, name: 'French course'},
{id: 3, name: 'Italian course'},
{id: 4, name: 'Arabic course'},
];
}]
})
///////////////////
.directive('fadeOutAlert', function ($timeout) {
return {
restric: "A",
scope: {
showTime: "@showTime",
switcher: "=ngShow"
},
link: function ($scope, element, $attrs) {
$scope.$watch('switcher', function () {
if ($scope.switcher) {
$timeout(function () {
$scope.switcher = false;
}, parseInt($scope.showTime, 10));
}
});
}
};
})
///////////////
.directive('productsList', [function () {
return {
restrict: 'E',
templateUrl: 'views/web/products_lists.html',
scope: {
productsCollection: '='
},
link: function ($scope, element, $attrs) {
$scope.showAlert = false;
$scope.itemSelected = '';
$scope.setItemSelected = function (itemName) {
$scope.itemSelected = itemName;
$scope.showAlert = true;
}
}
};
}]);
<div class="products">
<!-- Your Code here -->
<h1>products</h1>
<products-list products-collection="productCtrl.products"></products-list>
</div>
<div class="alert alert-success" fade-out-alert show-time="2000"
role="alert" ng-show="showAlert">
<p>Product Selected: {{ itemSelected }}</p>
</div>
<hr>
<ul>
<li ng-click="setItemSelected(product.name)"
ng-repeat="product in productsCollection track by product.id">
{{ product.name }}
</li>
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment