LoginSignup
3
5

More than 5 years have passed since last update.

「Laravel」Eloquent外部キー、table連結について

Posted at

EloquentはLaravelの原始ActiveRecordから実見できた。
ORMの標準に従う:
table -> 記録 -> カラム -> オブジェクト属性
参考:
http://blog.csdn.net/xd43100678/article/details/24376607

一対一 : 

table :  user  と passport は一対一の関係

User.php
######### Userテープル #########
class User extends Eloquent  
{  
    public function passport()  
    {  
        return $this->has_one('Passport');//各ユーザーが自分のパスポート持っている
    }  
}  
UserController.php
//user id = 1  の passport 番号が欲しい時
$user = User::find(1); 
if(is_null($user))  
{  
        echo "No User found!";  
        return;  
}  
if($user->passport)  
{          
        //$userが対応しているpassportメソードへ、関連しているPassportテープルの情報を
     //取り、その後対応するnumber見つかればいい
        echo "The user's passport number is".$user->passport->number;  
}  

Passport.php
######### Passportテープル #########
class  Passport extends Eloquent  
{  
        public function user()  
        {  
                return $this->belongs_to('User');
             //こうすると、ここのuser_idはUser表に対応している。  
        }  
}  
PassportController.php
//ユーザーの名前を取る
$passport = Passport::find(1);  
echo $passport->user->name;

一対多 :

table : 表team と 表player

Team.php
class Team extends Eloquent  
{  
    public function Players()  
    {  
        return $this->has_many(Player);  
    }  
}
Player.php
class Player extends Eloquent  
{  
    public function team()  
    {  
        return $this->belongs_to('Team');  
    }  
}  

そして

TeamContorller.php
//teamに対応しているplayerの名前を全部出力
$team = Team::find(1);  
$players = $team->players()->get();  
foreach($players as $player)  
{  
        echo '$play->name is on team $team->name';  
}  

多対多 :

Student.php
//Studentテープル
class Student extends Eloquent  
{  
        public function courses()  
        {  
                return $this->has_many_and_belongs_to('Course');  
        }  
}  
Course.php
//Course授業テープル
class Course extends Eloquent  
{  
        public function students()  
        {  
                return $this->has_many_and_belongs_to('Student');  
        }  
}  

そして

StudentController.php
// Student id = 1 の授業を出力
$student = Student::find(1);  

if(is_null($student))  
{  
        echo "学生が存在しない";  
        exit;  
}  

if(!$student->courses)  
{  
        echo "学生: $student->name 授業はない。";  
        exit;  
}  
else  
{  
        foreach($student->courses as $course)  
        {  
                echo "学生:$student->name 授業は:$course->name";  
        }  
}  
CourseController.php
//ある授業の全て学生を出力
$course = Course::find(1);  
if(is_null($course))  
{  
        echo "この授業は存在しない";  
        exit;  
}  
if($course->students)  
{  
        foreach($course->students as $student)  
        {  
                echo "この授業:$course->name 参加している学生達の名前は:$student->name";  
        }  
}  
else  
{  
        echo "この授業参加している学生一人もいない";  
        exit;  
}  

データ追加

CourseController.php
//ある授業に学生追加
$course = Course::find(23);  
if(is_null($course))  
{  
        echo "この授業は存在しない"  
        exit;  
}  
$new_student = array(  
        'name' => '沖田杏梨';  
);  

$course->students()->insert($new_student);  
3
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
5