Laravel Eloquent 查询运行两次

标签 laravel laravel-5 eloquent

我正在从 Instagram 抓取一些数据,然后在数据库中创建它的记录,但有时,该记录会被插入两次(或更多次)。当我在代码中添加 echo 时,它只打印/回显一次,所以我不确定为什么有时记录会插入多次。代码如下:

public function handle()
{

    #Define variables
    $website_id = null;
    $user_id = null;
    $avg_dataset_start = null;
    $avg_dataset_end = null;

    try {
        $api = new \Instagram\Api();
        $api->setUserName($this->username);
        $obj = $api->getFeed();

        #quick hack to convert to nested obj
        $data = json_decode(json_encode($obj));

        $data->videos_count = 0;
        $data->pictures_count = 0;
        $data->avgPostLikes = 0;
        $data->avgPostComments = 0;
    } catch (Exception $e) {
        return $e;
    }

    $account = InstagramAccount::where('instagram_id', $data->id)->orWhere('username', $data->userName)->first();

        $scraped_data = InstagramAccountScrape::create([
            'instagram_account_id' => $account->id,
            'username' => $data->userName,
            'full_name' => $data->fullName,
            'biography' => $data->biography,
            'profile_picture_url' => $data->profilePicture,
            'external_url' => $data->externalUrl,
            'website_id' => $website_id,
            'media_count' => $data->mediaCount,
            'followers_count' => $data->followers,
            'following_count' => $data->following,
            'avg_likes_count' => $data->avgPostLikes,
            'avg_comments_count' => $data->avgPostComments,
            'avg_dataset_start' => $avg_dataset_start,
            'avg_dataset_end' => $avg_dataset_end,
            'avg_dataset_photos_count' => $data->pictures_count,
            'avg_dataset_videos_count' => $data->videos_count,
            'is_private' => $data->private,
            'is_verified' => $data->verified,
            'user_id' => $user_id,
        ]); #Sometimes adds more than one record

    echo "Print once please"; #prints once, as expected

    return response()->json($scraped_data); #returns only one instance
}

最佳答案

弄清楚了,确实很奇怪。

这是由 style="background: url('')"引起的,其中 url 为空,因此浏览器假设它引用同一页面并导致它再次加载,有时会多次加载,直到浏览器决定停止重新加载/加载疯狂。

它发生在所有页面上,就像在布局 Blade 模板中一样。

特别感谢这里的帖子:MVC controller is being called twice

关于Laravel Eloquent 查询运行两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58029171/

相关文章:

php - Azure Web 应用程序 laravel api - nginx 错误 404

mysql - 检索满足第一个表条件的所有值

php - Laravel 关系数据库连接

php - Laravel 5.4 在数据透视表中插入时调用 null 上的成员函数 Attach()

php - Paypal IPN,更改帐户中的 ipn url 后未​​获得所有交易响应

mysql - 如何使用 Eloquent 获取 N 个最新的数据库条目?

php - 拉维尔 6 : Cannot send message without a sender address

php - 我的表中的记录未显示在 laravel 5.2 中的数据表上

php - 部署在 Heroku 上时查看未找到

laravel-5 - 您的配置文件不可序列化