//檔案:database/migrations/日期_編號_create_addresses_table.php
    public function up()
    {
        Schema::create('addresses', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->usigned()->nullable();
            $table->string('name');
            $table->timestamps();
        });
    }
    //檔案app/user.php
    public function Address(){
        return $this->hasOne('App\Address');
        //每個會員都有一個地址
    }
	//檔案app/address.php
    protected $fillable=[
    	'name'
    ];
//檔案:routes/web.php

use App\User;
use App\Address;

Route::get('/insert', function() {
    $user= User::findOrFail(1);
    $address = new Address(['name'=>'台北市信義區狂狂路9487號']);
    $user->address()->save($address);
});

瀏覽器輸入:public/insert

呈現空白,編號1的會員的地址資料加入剛剛設定的變數(地址資料表)

//檔案:routes/web.php

Route::get('/update', function() {
    $address = Address::where('user_id','=',1)->first();
    				// whereUserId(1);
    $address->name="台中市沙鹿區狂狂路9487號";
    $address->save();

});

瀏覽器輸入:public/update

呈現空白,編號1的會員的地址資料改成剛剛設定的變數(地址資料表)

//檔案:routes/web.php

Route::get('/read', function() {
    $user = User::findOrFail(1);
    echo $user->address->name;
});

瀏覽器輸入:public/read

呈現編號1的會員的地址資料

//檔案:routes/web.php

Route::get('/delete', function() {
    $user =User::findOrFail(1);
    $user->address()->delete();
});

瀏覽器輸入:public/delete

刪除編號1的會員的地址資料(地址資料表)

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 Jerry 的頭像
    Jerry

    Bug倉庫 // 程式日記

    Jerry 發表在 痞客邦 留言(0) 人氣()