Skip to content

Instantly share code, notes, and snippets.

View FrankFlow's full-sized avatar

FrankFlow FrankFlow

View GitHub Profile
<?php
public function store()
{
$this->validate(request(), [
'denaro' => [function ($attribute, $value, $fail) {
if ($value <= 10) {
$fail(':attribute è necessario un maggior numero');
}
}]
<?php
public function store()
{
$this->validate(request(), [
'denaro' => [new MiaValidationRule]
]);
}
<?php
use Illuminate\Contracts\Validation\Rule;
class MiaValidationRule implements Rule
{
public function passes($attribute, $value)
{
return $value > 10;
}
<?php
protected $availableIncludes = [
'actedIn',
'directed',
];
public function includeActedIn(Person $person)
{
$books = $person->actedIn;
<?php
protected $availableIncludes = [
'authors',
'publishers',
];
public function includeAuthors(Book $book)
{
$authors = $book->authors;
<?php
Route::post('authors', 'AuthorsController@store');
// dove [$person_id => 1, $book_id => 1, 'role' => 'author']
Route::delete('authors', 'ActorsController@destroy');
Route::post('publishers', 'PublishersController@store');
// dove [$person_id => 1, $book_id => 1, 'role' => 'publisher']
Route::delete('publishers', 'PublishersController@destroy');
<?php
Schema::create('book_person', function(Blueprint $table) {
$table->unsignedInteger('person_id')->index();
$table->foreign('person_id')->references('id')->on('people');
$table->unsignedInteger('book_id')->index();
$table->foreign('book_id')->references('id')->on('books');
$table->string('role');
<?php
Route::get('people', 'PeopleController@index');
Route::post('people', 'PeopleController@store');
Route::get('people/{person_id}', 'PeopleController@show');
Route::patch('people/{person_id}', 'PeopleController@update');
Route::delete('people/{person_id}', 'PeopleController@destroy');
<?php
public function transform(Book $book)
{
return [
'id' => $book->id,
'title' => $book->title,
'year' => $book->year,
'ranking' => $book->complicatedAlgorithmToCalculateTomatoesThumbsAndStarsForAggregateRanking(),
'links' => [
<?php
public function index(Request $request)
{
$movies = Book::when($request->has('genre'), function ($query) {
return $query->where('genre', $request->input('genre'));
})
->when($request->has('year'), function ($query) {
return $query->where('year', $request->input('year'));
})