mysql - 在 Laravel 中插入或更新?

标签 mysql laravel laravel-5

设置此查询的最佳方法是什么?我似乎找不到任何有关实际插入或更新有效的文档,我希望通过 Eloquent 模型来完成它,但似乎无法让它工作

如有任何帮助,我们将不胜感激。

DB::table('questions')->insert([
    'marital_status' => $request->marital_status,
    'job_title' => $request->job_Title,
    'industry' => $request->industry,
    'occupation' => $request->occupation,
    'paye' => $request->paye,
    'self_employed' => $request->self_employed,
    'child_benefit' => $request->child_benefit,
    'work_home' => $request->work_home,
    'own_transport' => $request->own_transport,
    'company_vehicle' => $request->company_vehicle,
    'annual_income' => $request->annual_income,
    'pay_memberships' => $request->pay_memberships,
    'income_benefits' => $request->income_benefits,
    'printer' => $request->printer,
    'contact' => $request->contact,
    'share' => $request->share,
    'terms' => $request->terms,
    'user_id' => $user
]);

这是我的问题模型

<?php
namespace App;

class Questions extends Model
{
     protected $fillable = [
         'marital_status',
         'job_title',
         'industry',
         'occupation',
         'paye',
         'self_employed',
         'child_benefit',
         'work_home',
         'own_transport',
         'company_vehicle',
         'annual_income',
         'pay_memberships',
         'income_benefits',
         'printer',
         'contact',
         'share',
         'terms',
         'user_id'
    ];

    public function users(){
        return $this->hasOne('App\User');

    }   
}

最佳答案

使用 Eloquent 与 mass assignment :

Question::updateOrCreate($request->all()->put('user_id', $user));

或者:

$question = Question::firstOrNew('some_id', $someId);
$question->fill($request->all()->put('user_id', $user))->save();

不要忘记用您想要保留的所有属性填充 $fillable 数组:

class Question extends Model
{
      protected $fillable = [
            'marital_status', 
            'job_title',
            'industry',
            'occupation',
            'paye',
            'self_employed',
            'child_benefit',
            'work_home',
            'own_transport',
            'company_vehicle',
            'annual_income',
            'pay_memberships',
            'income_benefits',
            'printer',
            'contact',
            'share',
            'terms',
            'user_id'
      ]

更新

如果 put() 方法由于某种原因不起作用,请尝试以下操作:

$request->merge(['user_id' => $user]);

然后只需使用$request->all()

或者:

$requestData = $request->all();
$requestData['user_id'] = $user;

然后使用$requestData

关于mysql - 在 Laravel 中插入或更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37543473/

相关文章:

php - 由验证引起的 Laravel 路由中的重定向循环?

android - 如何使用 Laravel 开发移动应用程序的后端(iOS 和 Android)?任何教程?

php - Laravel 5.2 像静态方法一样调用非静态查询生成器方法

php - Laravel Notification - 在字符串上调用成员函数 routeNotificationFor()

php - phpBB 上的 SQL 错误

php - Laravel 查询生成器原始 AND 语句

mysql - #1093 - 您不能在 FROM 子句中指定要更新的目标表 'comments'

php - Laravel 队列作业失败说 Undefined property 但作业在没有队列的情况下运行正常?

mysql - Sequelize 自动 : Camel case in Programmatic API options

javascript - Cckeditor 从数据库加载模板