laravel保存,将数据更新为两个表

时间:2014-11-06 00:38:59

标签: php laravel eloquent

将数据更新到数据透视表时遇到问题。

users[id->autoincrement,username,password,firstname,lastname,lga,email]

    class User extends Eloquent
    {
      public function lga()
        {
            return $this->HasMany('LGA');
        }
    }

userslga[id->autoincrement,user_id,lga_id]



 public function user()
        {
            return $this->belongsTo('user');
        }

        public function lga()
        {
            return $this->belongsTo('lga');
        }

lga[id->autoincrement,lg_name]

class LGA extends Eloquent
{
  public function lga()
    {
        return $this->belongsTo('User');
    }
}

 When i tried querying relationship,like User::find($id)->lga  i could not figure that out.but i eventually made it work using this:





$U_LGA      = UserLga::where('user_id',$id)->lists('lga_id');

现在我的问题是我可以将数据存储到我的控制器的相关表中,如下所示:

if ($validation->passes()) 
                {

                    $user = new User;

                    $user->email            = Input::get('email');
                    $user->username         = Input::get('username');
                    $user->firstname        = Input::get('firstname');
                    $user->lastname         = Input::get('lastname');
                    $user->type             = Input::get('type');
                    $user->phone            = Input::get('phone');
                    $user->password         = Hash::make(Input::get('password'));


                    $user->save();

                //save each users lga
                foreach(Input::get('LGA') as $LGS)
                {
                    $userlga = new UserLga;

                    $userlga->user_id       = $user->id;
                    $userlga->lga_id        = $LGS;

                    $userlga->save();
                }
                //end

                return Redirect::to('/')->with('success','New user profile successfully created');
                }

                //return json error
                return Response::json($validation->messages());

但是更新将是一个问题,因为UserLga :: find($ id)无效,因为它与从userlga中选择id = $ id的相同,其中我想要更新user_id所在的所有行如果检查了更多的lga,则当前用户ID正在编辑并添加额外的lga。

我需要帮助,如何做到这一点很困惑。或者我得到的概念错了或是Eloquent关系查询我想做什么,

请某人帮帮忙?

修改

我想跑下面的问题

UserLga::where('user_id',$uid)->lists('lga_id');

我想以英语的方式做到

$statement2 = "SELECT * FROM facility where LGA_id = '$fulga' ";

        //loop through lga id to build query
        foreach($LGA_id as $fax)
        {
            $statement2.= " "."OR"." "."LGA_id=". "'".$fax."'";
        }

        //FACILITY for User assigned LGA
         $facility = DB::Select($statement2);

/////////////////////////////////////////////// ////////////

//生成用户覆盖的所有lgaid         ////////////////////////////////////////////////// /////////////////////

$ulga = UserLga::where('user_id',$uid)->lists('lga_id');

    //remove the first lgaid in array to build query and avoid repetition


$sulga = array_shift($ulga);



// query
        $statement = "SELECT * FROM lga where id = '$sulga' ";

        //loop through lga id to build query
        foreach($ulga as $lga)
        {
            $statement.= " "."OR"." "."id=". "'".$lga."'";
        }

        //user assigned lga
          $LGA = DB::Select($statement);
AM SORI这看​​起来很粗,但是我希望能够更好地使用ELOQUENT RELATION QUERY

0 个答案:

没有答案