控制器仅将数组中的最后一个元素保存 [英] Controller only saving last element in array

查看:61
本文介绍了控制器仅将数组中的最后一个元素保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之前曾问过这个问题,但没有得到回答.所以我又问了一次.我有一个带有列的表格主题:

Have asked this question before but it wasn't answered.So am asking again.I have a table subjects with columns:

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;

我有我的模型来定义这种关系

And i have my model to define there relationship thus

//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中的关系不是很熟悉

Now here comes the problem.When ever i try to insert data into the pivot table subject_user,only the the last data in the array of values that i want to insert gets saved into the database.I dont know if its the foreign constraint keys that's only updating the row instead of inserting a new one when looping through data.I cant figure out what is wrong.And below is my controller to see how my data is been inserted.Am not very conversant with relationships in 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');
        }

我被困在这里.任何帮助都将得到

Am stucked here.Any help would be appriciated

推荐答案

不太确定您的数据库架构是什么样的.但是根据您的描述,我高度怀疑此代码:

Not really sure about what your db schema looks like. But based on what you have described, I highly doubt this code:

``` $ subject =新主题;

``` $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设置为主键?

因为所有主题都具有相同的class_id,所以如果将class_id设置为主,那么您可能总是在更新相同的数据库条目.

Because all subjects have the same class_id, if class_id is set to be primary, probably you are always updating the same database entry.

要进一步研究,请将$ Class_Subjects的输入设置为1的长度,或者仅循环一次以获取第一个主题信息.将其插入数据库,然后查看数据库中的条目是否现在已更改.

To further investigate, set input of $Class_Subjects to length of 1 or simply only loop once to get the first subject info. Insert it into database and see if the entry in database now changes.

示例代码如下:

``` $ subject =新主题;

``` $subject = new Subject;

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

            $subject->save();
            break;

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

``` Please let me know more results.

这篇关于控制器仅将数组中的最后一个元素保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆