Skip to content

Instantly share code, notes, and snippets.

@monbang
Last active March 27, 2020 15:05
Show Gist options
  • Save monbang/c2cd6e8220393c3ea49f294957ea4aef to your computer and use it in GitHub Desktop.
Save monbang/c2cd6e8220393c3ea49f294957ea4aef to your computer and use it in GitHub Desktop.
eloquent 1
<?php
Schema::create('users', function(Blueprint $table) {
$table->bigIncrements('id');
$table->username('string')->unique();
$table->password();
});
Schema::create('articles', function(Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->string('title');
$table->text('excerpt');
$table->text('body');
$table->timestamps();
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
});
Sactory->define(User::class, function(Faker $faker) {
return [
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'email_verified_at' => now(),
'password' => '',
'remember_token' => Str::random(10),
];
});
-> factory(App\User::class)->create();
-> factory(App\User::class, 10)->create();
-> php artisan make:factory ArticleFactory -m 'App\Article'
Sactory->define(Article::class, function(Faker $faker) {
return [
'user_id' => factory(\App\User::class),
'title' => $faker->sentence,
'excerpt' => $faker->sentence,
'body' => $faker->paragraphs(3, true),
];
});
-> factory(App\Article::class, 5)->create(['user_id' => 1]);
App\User.php
public function articles() {
return $this->hasMany(Article::class);
}
App\Article.php
public function user() {
return $this->belongsTo(User::class);
}
public function author() {
return $this->belongsTo(User::class, 'user_id');
}
Schema::create('tags', function(Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->timestamps();
});
Schema::create('article_tag', function(Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('article_id');
$table->unsignedBigInteger('tag_id');
$table->timestamps();
$table->unique(['article_id', 'tag_id']);
$table->foreign('article_id')->references('id')->on('articles')->onDelete('cascade');
$table->foreign('tag_id')->references('id')->on('tags')->onDelete('cascade');
});
App\Article.php
public function tags() {
return $this->belongsToMany(Tag::class);
return $this->belongsToMany(Tag::class)->withTimestamps();
}
App\Tag.php
public function articles() {
return $this->belongsToMany(Article::class);
}
ArticlesController
public function index() {
if (request('tag')) {
$articles = Tag::where('name', request('tag'))->firstOrFail()->articles;
} else {
$articles = Article::latest()->get();
}
}
@forelse($articles as $article)
$article->name
@empty
// nothing
@endforelse
ArticlesController {
public function create() {
return view('articles.create', [
'tags' => Tag::all(),
]);
}
public function store() {
$this->validateArticles();
$article = new Article(request(['title', 'excerpt', 'body']));
$article->user_id = auth()->id();
$article->save();
$article->tags()->attach(request('tags'));
return redirect(route('articles.index'));
}
public function update() {
$article->tags()->sync(request('tags'));
}
protected function validateArticles() {
return request()->validate([
'title' => 'required',
'excerpt' => 'required',
'body' => 'required',
'tags' => 'exists:tags,id',
]);
}
}
$article->tags()->attach(1);
$article->tags()->attach([1, 2]);
$article->tags()->deattach([1, 2]);
?>
<label>Tags</label>
<select name="tags[]">
@foreach($tags as $tag)
<option value="{{ $tag->id }}">{{ $tag->name }}</option>
@endforeach
</select>
@error('tags')
<p>{{ $message }}</p>
@enderror
<?php
$articles = App\Article::with('tags')->get();
$articles->pluck('tags')->collapse()->pluck('name')->unique();
$articles->pluck('tags.*.name')->collapse()->unique()->map(function($item) {
return ucwords($item);
});
class AppServiceProvider extends ServiceProvider {
public function register() {
$this->app->bind('App\Example', function() {
$collaborator = new \App\Collaborator();
$foo = 'foobar';
return new \App\Example($collaborator, $foo);
});
$this->app->singleton('App\Example', function() {
$collaborator = new \App\Collaborator();
$foo = 'foobar';
return new \App\Example($collaborator, $foo);
});
}
}
class PageController extends Controller {
public function index() {
dd(resolve('App\Example'));
}
}
?>
<form method="POST" action="{{ route('articles) }}">
<?php
class Article extends Model {
protected $fillable = ['title', 'excerpt', 'body'];
public function getRouteKeyName() {
return 'slug';
}
}
public function show(Article $article) {
return view('articles.view', compact('article'));
}
public function store() {
Article::create($this->validateArticle());
return redirect('/');
}
public function update(Article $article) {
$article->update($this->validateArticle());
return redirect('/');
}
protected function validateArticle() {
return request()->validate([
'title' => 'required',
'excerpt' => 'required',
'body' => 'required'
]);
}
public function sendEmail() {
request()->validate(['email' => 'required|email']);
Mail::raw('It works', function($message) {
$message->to(request('email'))
->subject('Hello there');
});
return redirect('/contact us')->with('message', 'mail sent');
}
@if(session('message'))
<div>{{ session('message') }}</div>
@endif
-> php artisan make:notification PaymentReceived
class PaymentController {
public function store() {
Notification::send(request()->user(), new PaymentReceived);
request()->user()->notify(new PaymentReceived());
$user->notify(new PaymentReceived());
ProductPurchased::dispatch('event fired on controller');
// or
event(new ProductPurchased('event fired'));
}
}
-> php artisan notification:table
->php artisan make:event ProductPurchased
namespace App\Events;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class ProductPurchased
{
use Dispatchable, SerializesModels;
public $name; // or class
public function __construct($name) {
$this->name = $name;
}
}
-> php artisan make:listener AwardPrize -e ProductPurchased
@can('update-item', $item)
<button>Edit</button>
@endcan
AuthServiceProvider {
public function boot() {
$this->registerPolicies();
Gate::define('update-item', function(User $user, Item $item) {
return $item->user->is($user);
});
}
}
class ItemBestReplyController extends Controller {
public function store(Reply $reply) {
$this->authorize('update-item', $reply->item);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment