# Thêm một bảng mới và thêm giá trị bằng lệnh artisan (ok)

![](https://1957079826-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MCBeDUD-PK_RF-YgJRe%2F-MEGRj2OneOcuAOYRALS%2F-MEGSTHGpGpt2xpGJjlh%2FScreenshot_1.png?alt=media\&token=5e46c8c5-f4d6-4163-98c9-4114d90cade3)

![](https://1957079826-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MCBeDUD-PK_RF-YgJRe%2F-MEGRj2OneOcuAOYRALS%2F-MEGSWOUeqBKSWaYCltr%2FScreenshot_2.png?alt=media\&token=1e11079f-d216-4dfb-b189-94f1d4d738ba)

![](https://1957079826-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MCBeDUD-PK_RF-YgJRe%2F-MEGRj2OneOcuAOYRALS%2F-MEGSYzeDOmqiuttbUuh%2FScreenshot_3.png?alt=media\&token=bd566624-0473-48a0-82c1-fdf19460e383)

![](https://1957079826-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MCBeDUD-PK_RF-YgJRe%2F-MEGRj2OneOcuAOYRALS%2F-MEGSgQlkTMq0St7XoK2%2FScreenshot_5.png?alt=media\&token=5ebda531-6aca-4f64-a45a-a747fea4fb38)

C:\xampp\htdocs\hanam.com\routes\web.php

```
<?php

use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
 */

Route::get('/', function () {
  return view('welcome');
});
Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');
// Route::get('/user', function(){
//   factory(\App\User::class,5)->create();
// });
Route::get('/user', function () {
	// $user  = factory(\App\User::class)->create();
	// \App\Address::create([
	// 	"user_id" => $user->id,
	// 	"country" => "Ha Nam 10"
	// ]);
	// $user->address()->create([
	// 	"country" => "Ha Nam 9"
	// ]);
  // $users = \App\User::all();
  // $addresses = \App\Address::all();
  // $users = \App\User::with('addresses')->get();
  // $users = \App\User::get();
  // Hoặc dùng cách khác 
  // $users = \App\User::with('posts')->get();
  // $users = \App\User::has('posts','>=',2)->with('posts')->get();
  // $users = \App\User::whereHas('posts',function($query) {
  // 	$query->where('title','like','%Post 2%');
  // })->with('posts')->get();
  $users = \App\User::doesntHave('posts')->with('posts')->get();
  // $users[0]->posts()->create([
  // 	"title" => "Post 1 Test"
  // ]);
  // $users[2]->posts()->create([
  // 	"title" => "Post 2 Test"
  // ]);
  return view('users.index', compact('users'));
});
Route::get('/posts', function () {
	// \App\Post::create([
	// 	'user_id' => 1,
	// 	'title' => 'Post title 1'
	// ]);
	// $posts = \App\Post::get();
  // \App\Tag::create([
  //   'name' => 'Lavarel'
  // ]);
  // \App\Tag::create([
  //   'name' => 'PHP'
  // ]);
  // \App\Tag::create([
  //   'name' => 'Javascript'
  // ]);
  // \App\Tag::create([
  //   'name' => 'VueJs'
  // ]);
  // $tag = \App\Tag::first();
  // $post = \App\Post::first();
  // $post = \App\Post::with('tags')->first();
  // $post->tags()->attach($tag);
  // $post->tags()->attach([1,2,3,4]); 
  // $post->tags()->sync([1,2,3,4]);
  // $post->tags()->detach([1]);
  $post  = \App\Post::first();
  $posts = \App\Post::with(['user','tags'])->get();
  $post->tags()->attach([
    1 => [
      'status' => 'approved'
    ]
  ]);
	return view('posts.index',compact('posts'));
});
Route::get('/tags', function () {
  $tags = \App\Tag::with("posts")->get();
  return view('tags.index',compact('tags'));
});
```

