Skip to content

Instantly share code, notes, and snippets.

@monbang
Last active May 21, 2020 17:15
Show Gist options
  • Save monbang/4381fa07ccafd47e69467d0dc3305182 to your computer and use it in GitHub Desktop.
Save monbang/4381fa07ccafd47e69467d0dc3305182 to your computer and use it in GitHub Desktop.
lara note
<?php
=> factory(App\User::class)->create();
=> factory(App\User::class, 10)->create();
=> artisan help make:factory
=> artisan make:factory ArticleFactory -m "App\Article"
=> $factory->define(Article::class, function(Faker $faker){
return [
'user_id' => factory(\App\User::class), // belongs to
'title' => $faker->sentence,
'username' => $faker->unique()->safeEmail,
'excerpt' => $faker->sentence,
'body' => $faker->paragraph, // $faker->paragraphs(4)
];
});
=> factory(App\Article::class, 5)->create(['title' => 'Override the title', 'user_id' => 1]); // existing user relation or override the title
// migration
public function up() {
Schema::create('post', function(Blueprint $table) {
$table->increment('id');
$table->unsignedBigInteger('user_id'); // belongs to
$table->string('title');
$table->text('body');
$table->timestamps();
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
});
}
// User (parent relation)
public function posts() {
return $this->hasMany(Post::class);
}
// Post (child to User) inverse of hasMany()
public function user() {
return $this->belongsTo(User::class);
}
// if foreign key name is different
public function author() {
return $this->belongsTo(User::class, 'user_id')
}
// Post belongs to many tags
public function tags() {
return $this->belongsToMany(Tag::class);
// by default timestamp columns are null when attaching relations so
return $this->belongsToMany(Tag::class)->withTimeStamps();
}
// tag migration
public function up() {
Schema::create('tags', function(Blueprint $table) {
$table->id('id');
$table->string('name');
$table->unsignedBigInteger('')
})
}
// post_tag migration
public function up() {
Schema::create('post_tag', function(Blueprint $table) {
$table->unsignedBigInteger('post_id');
$table->unsignedBigInteger('tag_id');
$table->unique(['post_id', 'tag_id']);
$table->foreign('post_id')
->references('id')
->on('posts')
->onDelete('cascade');
$table->foreign('tag_id')
->references('id')
->on('tags')
->onDelete('cascade');
});
}
=> $post->tags->pluck('name')
// Tag belongs to many Post inverse of tag in post
public function posts() {
return $this->belongsToMany(Post::class);
}
// attach one
=> $post->tags()->attach(1)
// dattach one
=> $post->tags()->detach(1)
// attach multiple
=> $post->tags()->attach([1, 2, 3])
// dattach multiple
=> $post->tags()->detach([1, 2])
// or use model
=> $tag = App\Tag::find(1)
=> $post->tags()->attach($tag)
// find many
=> $tags = App\Tag::findMany([1, 2])
=> $post->tags()->attach($tags)
// validation of exists
protected function validatePost() {
return request()->validate([
'title' => 'required',
'excerpt' => 'required',
'body' => 'required',
'tags' => 'exists:tags,id',
]);
}
public function store() {
// separete validation when request do not exactly matches model
// $post = new Post($this->validatePost());
$this->validatePost();
$post = new Post(['title', 'body']);
$post->user_id = 2;
$post->save();
if (request()->has('tags')) {
$post->tags()->attach(request('tags'));
}
return $this->redirect(route('posts.index'));
}
// mail
public function sendMail() {
request()->validate(['email' => 'required|email']);
Mail::raw('it works', function($message) {
$message->to(request('email'))
->subject('hello');
});
return redirect('/contact')
->with('message', 'mail sent');
}
// in view
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment