【问题标题】:Laravel store data to the pivot table eloquentLaravel 将数据存储到数据透视表 eloquent
【发布时间】:2019-04-07 14:00:56
【问题描述】:

我正在尝试将数据从表单存储到数据库。我有带有subjectsubjectType(Economic, Informatic subject)subjectType_subject(pivot table) 列的表格。我可以将数据存储到 Subject 表,但我需要将 id 存储到 subjectType_subject 表。在我看来,我有一个下拉导航 - 经济科目和信息学科目。如果我想添加经济主题,我需要将 ID 添加到数据透视表以显示它。

请问我该怎么做?

现在我只能将数据存储到主题表,它不会出现在经济主题部分:

SubjectController

public function create() {
}

public function store(Request $request)
{
    $this->validate($request, [
        'name' => 'required',
        'abbreviation' => 'required',
        'description' => ''
    ]);

    $subject = new Subject();
    $subject->name = $request->input('name');
    $subject->abbreviation = $request->input('abbreviation');
    $subject->description = $request->input('description');

    $subject->save();

    return redirect()->back()
         ->with('success', 'Úspěšne jsi vytvořil nový předmět.');
    }
}

主题模型

public $table = 'Subject';

protected $fillable = [
    'name',
    'abbreviation',
    'description'
];

public function subject_type() {
    return $this->belongsToMany('App\Subject_type',  'subjectType_subject');
}

主题类型模型

public $table = 'SubjectType';

public function subject() {
    return $this->belongsToMany('App\Subject', 'subjectType_subject');
}

【问题讨论】:

    标签: php laravel view eloquent laravel-blade


    【解决方案1】:

    首先,尝试修正您的命名约定。如果您遵循 laravel 的命名约定,事情会容易得多。 Here is the guide.

    • 表名应该是snake_case(将@​​987654322@更改为subject_type
    • 数据透视表应该是按字母顺序排列的单数模型名称(将 subjectType_subject 更改为 subject_subject_type
    • belongsToMany 应该有一个复数函数名。 (将public function subject_type() 更改为public function subject_types() 并将public function subject() 更改为public function subjects()

    然后使用 laravel attach 关键字来填充数据透视表。

    $subject = new Subject();
    $subject->name = $request->input('name');
    $subject->abbreviation = $request->input('abbreviation');
    $subject->description = $request->input('description');
    $subject->save();
    
    $subject->subject_types()->attach(id_of_your_subject_type); // for example, 
                 the id of economics in your subject_type table is 1, then put 1 inside the parenthesis
    

    【讨论】:

    • 谢谢您的建议。我收到这样的错误 -> “使用未定义的常量 subject_type_id - 假定为 'subject_type_id
    • 已经通过添加''解决了它,但现在我收到错误“传递给 Illuminate\Database\Eloquent\Relations\BelongsToMany::attach() 的参数 2 必须是数组类型,给定字符串。 ..你能帮我解决这个问题吗?
    • 您的 subject_type 表中有数据吗?
    • 是的,我有。我有查询 -> $subject->subject_types()->attach('subject_type_id', 'subject_id'); -- 因为在数据透视表中两列都不为空
    • 试试sync而不是attach并告诉我是否有任何错误
    【解决方案2】:

    只需在下面添加雄辩的查询

     $subject->save();
    

    喜欢:

    $subjecttype= new SubjectType;
    $subjecttype->subjectid= $request->input(‘subjectid’);
    $subjecttype->save();
    
      return redirect()->back()
             ->with('success', 'Úspěšne jsi vytvořil nový předmět.')
    

    【讨论】:

      猜你喜欢
      • 2021-08-29
      • 2018-11-11
      • 2015-06-24
      • 2020-09-01
      • 2020-10-21
      • 2018-09-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多