Last active
December 30, 2019 07:42
-
-
Save moradi-morteza/2a2219e8f59cdf92a2e99540ddf79577 to your computer and use it in GitHub Desktop.
[relation] #LaravelT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// belongs to - one to one - one to many - many to mayn | |
class PostModel extends Model | |
{ | |
protected $fillable =['title','des','user_id']; | |
public function user(){ | |
return $this->belongsTo(User::class); | |
// return User::findOrFail($this->user_id); | |
} | |
} | |
class User extends Model | |
{ | |
protected $fillable = [ | |
'name', 'email', 'password', | |
]; | |
public function posts(){ | |
return $this->hasMany(PostModel::class);// relation | |
// return PostModel::where('user_id',$this->id)->get(); | |
} | |
} | |
// for user migration | |
public function up() | |
{ | |
Schema::create('users', function (Blueprint $table) { | |
$table->increments('id'); | |
$table->string('name'); | |
$table->string('email')->unique(); | |
$table->timestamp('email_verified_at')->nullable(); | |
$table->string('password'); | |
$table->rememberToken(); | |
$table->timestamps(); | |
}); | |
} | |
// for post migration | |
public function up() | |
{ | |
Schema::create('post_models', function (Blueprint $table) { | |
$table->bigIncrements('id'); | |
$table->text('title'); | |
$table->integer('user_id')->unsigned(); | |
$table->text('des'); | |
$table->timestamps(); | |
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); | |
}); | |
} | |
// IMPORTANT | |
return $posts->user; // not user() | |
return $user->posts; // not posts | |
// One to One ---------------------------- | |
//each user has one profile | |
$user=\App\User::find(3); | |
$user_profile=$user->profile; | |
class User extends Authenticatable | |
{ | |
use Notifiable; | |
protected $fillable = ['name', 'email', 'password',]; | |
protected $hidden = ['password', 'remember_token',]; | |
protected $casts = [ 'email_verified_at' => 'datetime',]; | |
public function profile(){ | |
return $this->hasOne(ProfileModel::class);// relation | |
// return ProfileModel::where('user_id',$this->id)->get(); | |
} | |
} | |
// One to Many---------------------------------------------------------------- | |
// each user has many post | |
$user=\App\User::find(3); | |
$user_posts=$user->posts;// if use posts() insted of posts its return relatio | |
$custom_post=$user->posts()->where('updated_at','>','2018-10-07 15:06:32')->get()-toArray(); | |
$last_posts= $user->LastPosts(90)->get()->toArray(); | |
$user_with_post=\app\User::has(posts)->get(); | |
$user_with_post_more_than_5=\app\User::has(posts,'>',5)->get(); | |
$query_with_function=User::wherehas('posts',function($query){ | |
$query->where('title','like','%hello%) | |
})-get(); | |
$query_with_function=User::doesntHave('posts')-get(); | |
$query_with_function=User::whereDoesntHave('posts',function($query){ | |
$query->where('title','like','%hello%) | |
})-get(); | |
$user_all=User::get()->toArray(); | |
$user_all=User::with('profile')->get(); | |
$user_all=User::with('profile:user_id,age,email,bio')->get(); //user_id is key | |
$user_all=User::withCount('posts as post_count')->toArray();// get use with post_count | |
$user_all=User::withCount(['posts as post_count','tweets as tweet_count'])->toArray();// get use with post_count | |
class User extends Authenticatable | |
{ | |
use Notifiable; | |
protected $fillable = ['name', 'email', 'password',]; | |
protected $hidden = ['password', 'remember_token',]; | |
protected $casts = [ 'email_verified_at' => 'datetime',]; | |
public function posts(){ | |
return $this->hasMany(PostModel::class);// relation | |
// return PostModel::where('user_id',$this->id)->get(); | |
} | |
public function scopeLastPosts($query,$minuts=60){ | |
return $this->posts()->where('created_at','>',Carbon::now()->subMinutes($minuts)); | |
} | |
} | |
// for get user from each post------------------------------------ | |
class PostModel extends Model | |
{ | |
public function user(){ | |
return $this->belongsTo(User::class); | |
} | |
} | |
$user=\App\User::find(3); | |
$user_posts=$user->posts; | |
$first_post=$user_posts[0]; | |
$user_own_post=$first_post->user; | |
$user_own_post_email=$first_post->user->value('email'); | |
//Many to Many---------------------------------- | |
// every post can have many tag , every tag can have many post | |
// this need a new table | |
// posts_table , tags_table , post_tag_table | |
php artisan make:migration create_post_tags_table --create=post_tags | |
// becareful : laravel name like: table1_table2 | |
$table->unsignInteger('post_id'); | |
$table->unsignInteger('tag_id'); | |
$table->primary(['post_id','tag_id']); // very importan : every post can include just a tag (once) | |
$table->forign('post_id')->refrencs('id')->on('posts'); | |
$table->forign('tag_id')->refrencs('id')->on('tags'); | |
// we do not need any model for this migration | |
// add your tags in table tags | |
class PostModel extends Model | |
{ | |
public function tags(){ | |
return $this->belognsToMany(Tag::class); // if you not named like laravel (line67) return $this->belognsToMany(Tag::class,'post_tags'); line(58) | |
//$tag_ids = \DB::table('post_tag')->where('post_id',$this->id)->pluck('tag_id')->toArray(); // give all tag id seted to post | |
//$tags=Tag::whereIn('id',$tag_ids'); | |
//return $tags; | |
} | |
} | |
class TagModel extends Model | |
{ | |
return $this->belognsToMany(Post::class); // if you not named like laravel (line67) return $this->belognsToMany(Tag::class,'post_tags'); line(58) | |
} | |
$post=Post::find(3); | |
$tags=$post->tags; | |
$tag=Tag::find(1); | |
$posts=$tag->posts(); | |
//easy way: without relation | |
$post=Post::find(1); | |
$tag_ids = DB::table('post_tag')->where('post_id',$post->id)->pluck('tag_id')->toArray(); // give all tag id seted to post | |
$tags=Tag::whereIn('id',$tag_ids'); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment