首页 > 解决方案 > Laravel 尝试保存到数据库时出错

问题描述

我对 laravel 有点陌生,我大约 3 个月前就开始了。我正在尝试将您从 twitteroath + 关注者返回的数据保存到我的数据库中,但是当我尝试保存时出现错误。Atm 我正在使用 laravel 5.6 和 php 7.6


这是我的控制器中商店功能中的代码。我的控制器中的所有内容都可以保存到每个日期,即使 $twitter = new twitter 也可以正常工作。如果我最后转储()/dd()$twitter,它必须更正信息。但是一旦我添加了 $twitter->save(); 它返回一个错误

    define('CONSUMER_KEY', getenv('TWITTER_CONSUMER_KEY'));
    define('CONSUMER_SECRET', getenv('TWITTER_CONSUMER_SECRET'));
    define('OAUTH_CALLBACK', getenv('TWITTER_OAUTH_CALLBACK'));

    $request_token = [];
    $request_token['oauth_token'] = $_SESSION['oauth_token'];
    $request_token['oauth_token_secret'] = $_SESSION['oauth_token_secret'];

    if (isset($_REQUEST['oauth_token']) && $request_token['oauth_token'] !== $_REQUEST['oauth_token']) {
        return redirect::to('dashboard.settings.index')->with('message', 'Login Failed!');
    }

    //This is for getting the twitteroath access_tokens, id and screenname.
    $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $request_token['oauth_token'], $request_token['oauth_token_secret']);
    $access_token = $connection->oauth("oauth/access_token", ["oauth_verifier" => $_REQUEST['oauth_verifier']]);
    $_SESSION['access_token'] = $access_token;

    //This is used to get the the follower count
    $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
    $user = $connection->get('account/verify_credentials', ['tweet_mode' => 'extended', 'include_entities' => 'true']);

    $twitter = new twitter;
    $twitter->twitter_id = $access_token['user_id'];
    $twitter->auth_token = $access_token['oauth_token'];
    $twitter->auth_token_secret = $access_token['oauth_token_secret'];
    $twitter->username = $access_token['screen_name'];
    $twitter->followers = $user->followers_count;
    $twitter->save();  //<--- this is where is gets the error
    return redirect('/dashboard/settings');

这是我连接到我的数据库的模型

<?php 
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class twitter extends Model
{
   public function twitter_post()
   {
      return $this->hasMany('App\Models\twitter_post');
   }

   public function ContactInformation()
   {
      return $this->hasOne('App\Models\ContactInformation');
   }
}

万一有人想要它,这也是我数据库中的表。

我的桌子

我想知道我做错了什么是我正在寻找的一个非常小的错误。


编辑

我忘记在这里发布错误了。

SQLSTATE [42S22]:找不到列:1054 '字段列表'中的未知列'updated_at'(SQL:插入twitterstwitter_id,,,,,,,,) 值(123,测试,测试auth_tokenauth_token_secret测试usernamefollowers123,2018-11-01 2018-11-01 10:50:18 10:50:18))updated_atcreated_at

这是我回来的错误。

标签: phplaravel

解决方案


在您的模型中编写以下代码

public $timestamps = false;

然后再试一次


推荐阅读