Cách lấy giá trị bảng pivot thật khó nhằn :( (ok)
C:\xampp\htdocs\hanam.com\app\Post.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model {
protected $fillable = ['user_id','title'];
public function user() {
return $this->belongsTo(User::class)->withDefault([
'name' => 'Guest User'
]);
}
public function tags() {
// return $this->belongsToMany(Tag::class,'post_tag','post_id','tag_id');
// return $this->belongsToMany(Tag::class)->withTimestamps();
return $this->belongsToMany(Tag::class)->withTimestamps()->withPivot('status');
}
}
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'
// ]
// ]);
dd($post->tags->first()->pivot->status);
return view('posts.index',compact('posts'));
});
Route::get('/tags', function () {
$tags = \App\Tag::with("posts")->get();
return view('tags.index',compact('tags'));
});
PreviousThêm một bảng mới và thêm giá trị bằng lệnh artisan (ok)NextHandle events on attach, detach or sync in Many To Many Laravel Eloquent Relationships (ok)
Last updated