控制器只保存数组中的最后一个元素

时间:2014-11-20 09:09:41

标签: php laravel

之前已经问过这个问题,但没有回答。所以我再问一遍。我有一个表格主题栏目:

id->primary key,auto-increment;
class_id->foreign key->refrences id on classes table;
subject;
and a users table:
id->primary key,auto-increment;
username;
password;
and a classes table:
id->primary,autoincrement;
class;
term;
and a pivot table student_user: 
id->primary;
user_id->refrences id on users table;
subject_id->refrences id on subject table;
class_id->foreign,refrences id on classes table;

我有我的模型来定义那里的关系

//User Model 
//User.php



public function subjects()
    {
    return $this->belongsToMany('Subject');
    }

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

//Class Model
//SchoolClass.php



 public function subjects()
    {
    return $this->hasMany('Subject','class_id');
    }

//Subject Model
//Subject.php
public function term()
    {
    return $this->belongsTo('SchoolClass');
    }

    public function student()
    {
    return $this->belongsToMany('User');
    }

现在问题来了。当我尝试将数据插入数据透视表subject_user时,只有我要插入的值数组中的最后一个数据被保存到数据库中。我不知道它是否是外来的约束键只更新行而不是在循环数据时插入新行。我无法弄清楚出了什么问题。下面是我的控制器,看看我的数据是如何被插入的。我不太熟悉laravel中的关系

//Save Registered Courses


public function post_proceed_registration()
        {
            $PageTitle = 'Register Subjects';
            $id        = Auth::user()->id;
            $User      = User::find($id);
            $class_id  = $User->profile->class_id;
            $subjects  = Input::get('subjects');
            $registration = RegistrationCheck::where('class_id',$class_id)->where('user_id',$id)->pluck('status');

            if(count(Input::get('students')) != 0)
            {
                if($registration == 'true')
                {
                PivotSubject::where('user_id',$id)->where('class_id',$class_id)->delete();

                foreach($subjects as $sub_id)
                    {
                        $subject = new PivotSubject;

                        $subject->user_id = $id;
                        $subject->subject_id = $sub_id;
                        $subject->class_id   = $class_id;

                        $subject->save();
                    }

                RegistrationCheck::where('class_id',$class_id)->where('user_id',$id)->update(['status'=>'false']);

                return Response::Json('Success: Subject/Courses Registered');
                }
            return Response::Json('Error: You already registered for this Term/Semester.Please Contact ICT for help');
            }
            return Response::Json('Error:You did not choose any course to register');
        }

我被困在这里。任何帮助都会受到欢迎

1 个答案:

答案 0 :(得分:0)

不确定您的数据库架构是什么样的。但根据你所描述的内容,我非常怀疑这段代码:

```                 $ subject = new Subject;

            $subject->class_id      = $Class_id;
            $subject->subject       = ucfirst($data);
            $subject->subject_slug  = Str::Slug(ucfirst($data));

            $subject->save();

```

  1. 您的主题是否只包含三列,即class_id,subject和subject_slug?
  2. 您是否已将class_id设为主键?
  3. 由于所有主题都具有相同的class_id,如果class_id设置为primary,则可能始终更新相同的数据库条目。

    要进一步研究,请将$ Class_Subjects的输入设置为长度1,或者只是循环一次以获取第一个主题信息。将其插入数据库,看看数据库中的条目现在是否发生了变化。

    示例代码可能如下所示:

    ```                 $ subject = new Subject;

                $subject->class_id      = $Class_id;
                $subject->subject       = ucfirst($data);
                $subject->subject_slug  = Str::Slug(ucfirst($data));
    
                $subject->save();
                break;
    

    ``` 请让我知道更多结果。