<?phpnamespaceApp\Http\Controllers;useApp\Models\Admin;useApp\Imports\UsersImport;useApp\Exports\UsersExport;useMaatwebsite\Excel\Facades\Excel;useApp\Http\Controllers\Controller;classUserControllerextendsController {publicfunction__construct() {$this->middleware('auth'); }/** * Display a listing of the resource. * * @return\Illuminate\Http\Response */publicfunctionindex() { $admin =Admin::with('roles')->orderBy('admin_id','DESC')->paginate(5);returnview('admin.users.all_users')->with(compact('admin')); }publicfunctionimport() {Excel::import(newUsersImport,storage_path('users.xlsx'));returnredirect('/')->with('success','All good!'); }publicfunctionexport() {returnExcel::download(newUsersExport,'tests.xlsx'); }}
Ví dụ 1: Create Tag
<?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'
]);
// return view('posts.index',compact('posts'));
});
Ví dụ 2: Tạo post_tag
<?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->tags()->attach($tag);
$post->tags()->attach([1,2,3,4]); //$post->tags()->sync([1,2,3,4]);
// return view('posts.index',compact('posts'));
});
Ngoài ra chúng ta cũng có thể sử dụng $post->tags()->sync([1,2,3,4]); để update tag
Đặc biệt chúng ta có thể viết ngắn gọn
Thay vì viết:
$tag = \App\Tag::first(); $post = \App\Post::first();
Chúng ta viết:
$post = \App\Post::with('tags')->first();
Ví dụ 3: Get tags of post
<?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->tags()->attach($tag);
// $post->tags()->attach([1,2,3,4]);
$posts = \App\Post::with(['user','tags'])->get();
return view('posts.index',compact('posts'));
});
Ví dụ 4: Delete tags of post
<?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->tags()->attach($tag);
// $post->tags()->attach([1,2,3,4]);
$post->tags()->detach([1]);
$posts = \App\Post::with(['user','tags'])->get();
return view('posts.index',compact('posts'));
});
Ví dụ 5: belongsToMany
C:\xampp\htdocs\hanam.com\app\Tag.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Tag extends Model
{
protected $fillable = [
'name'
];
public function posts(){
return $this->belongsToMany(Post::class);
// return $this->belongsToMany(Post::class,'post_tag','tag_id','post_id'); chưa hiểu rõ
}
}