質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Model

MVCモデルの一部であるModelはアプリケーションで扱うデータとその動作を管理するために扱います。

Twitter

Twitterは、140文字以内の「ツイート」と呼ばれる短文を投稿できるサービスです。Twitter上のほぼ全ての機能に対応するAPIが存在し、その関連サービスが多く公開されています。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

Laravel 5

Laravel 5は、PHPフレームワークLaravelの最新バージョンで、2014年11月に発表予定です。ディレクトリ構造がが現行版より大幅に変更されるほか、メソッドインジェクションやFormRequestの利用が可能になります。

Q&A

解決済

1回答

8712閲覧

PHP/Laravelでモデルをnewしてインスタンスを生成しても「Trying to get property of non-object」とエラーになる

takutyu2

総合スコア7

Model

MVCモデルの一部であるModelはアプリケーションで扱うデータとその動作を管理するために扱います。

Twitter

Twitterは、140文字以内の「ツイート」と呼ばれる短文を投稿できるサービスです。Twitter上のほぼ全ての機能に対応するAPIが存在し、その関連サービスが多く公開されています。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

Laravel 5

Laravel 5は、PHPフレームワークLaravelの最新バージョンで、2014年11月に発表予定です。ディレクトリ構造がが現行版より大幅に変更されるほか、メソッドインジェクションやFormRequestの利用が可能になります。

0グッド

0クリップ

投稿2017/07/15 02:21

###前提・実現したいこと

PHP(Laravel)で、リツイート数ランキングを作成しています。

Streaming APIによりツイートを取得する部分までは上手く行ったのですが、
その後のDBへのツイートの格納がうまくいかず、1日ハマってしまったのでご質問させて頂きます。

具体的には、Phirehoseというライブラリを使用し、Streaming APIよりツイートを取得し、
DBに格納するという処理を実装しているのですが、
最後のDB格納部分でエラーが発生してしまいます。

tinkerで確認すると、モデルをnewしてインスタンスを生成し、all()で取得することはできました。
そのため、下記でも書きましたが指定しているパスが違うのでは?と思っていますが、どこが原因か分かっておりません。

###発生している問題・エラーメッセージ

var_dumpで色々試しましたが、App/Tweet.phpというEloquentモデルを生成し、
別phpファイルにてuseしてモデルをnewしています。

すると、以下のようなエラーメッセージが出ます。
(オブジェクトが見つからない、とのことでモデルのパスが違うのでは?と思っているのですが、どこが問題なのか分かっておりません。。)

Trying to get property of non-object

###該当のソースコード

関連するソースコードを全て記載いたします。

①Eloquentモデルクラス
・Tweet.php

②Migrationファイル
・tweetsテーブルのMigrationファイル

③実行用のCommandsファイル
・TwitterStreaming.php
・Karnel.php

④DB格納用のファイル
・filter-oauth.php

⑤.env

・Tweet.php (App/)

php

1<?php 2 3namespace App; 4 5use Illuminate\Database\Eloquent\Model; 6 7class Tweet extends Model 8{ 9 // 10}

・Migrationファイル

PHP

1<?php 2 3use Illuminate\Support\Facades\Schema; 4use Illuminate\Database\Schema\Blueprint; 5use Illuminate\Database\Migrations\Migration; 6 7class CreateTweetsTable extends Migration 8{ 9 /** 10 * Run the migrations. 11 * 12 * @return void 13 */ 14 public function up() 15 { 16 Schema::create('tweets', function (Blueprint $table) { 17 $table->increments('id'); 18 $table->string('user_id')->nullable(); 19 $table->string('user_name')->nullable(); 20 $table->string('screen_name')->nullable(); 21 $table->string('post_id')->unique()->nullable(); 22 $table->date('posted_date')->nullable(); 23 $table->string('url')->nullable(); 24 $table->string('image_url')->nullable(); 25 $table->string('image_w')->nullable(); 26 $table->string('image_h')->nullable(); 27 $table->string('comment')->nullable(); 28 $table->integer('retweet_count')->nullable(); 29 $table->integer('favorite_count')->nullable(); 30 $table->timestamps(); 31 }); 32 } 33 34 /** 35 * Reverse the migrations. 36 * 37 * @return void 38 */ 39 public function down() 40 { 41 Schema::dropIfExists('tweets'); 42 } 43}

・TwitterStreaming.php (App/Console/Commands/)

PHP

1<?php 2 3namespace App\Console\Commands; 4 5use Illuminate\Console\Command; 6 7// phirehoseを読み込み 8require_once(base_path() . '/vendor/phirehose/lib/Phirehose.php'); 9require_once(base_path() . '/vendor/phirehose/lib/OauthPhirehose.php'); 10require_once(base_path() . '/vendor/phirehose/filter-oauth.php'); 11 12class TwitterStreaming extends Command 13{ 14 15 protected $signature = 'twstreaming'; 16 17 protected $description = 'Get Twitter Streaming Data'; 18 19 public function __construct() 20 { 21 parent::__construct(); 22 } 23 24 public function handle() 25 { 26 // 環境変数からアクセストークン系呼び出し 27 define("TWITTER_CONSUMER_KEY", env('TWITTER_CONSUMER_KEY')); 28 define("TWITTER_CONSUMER_SECRET", env('TWITTER_CONSUMER_SECRET')); 29 define("OAUTH_TOKEN", env('OAUTH_TOKEN')); 30 define("OAUTH_SECRET", env('OAUTH_SECRET')); 31 32 \Log::debug("Twitterstreaming : ---起動---"); 33 34 // ストリーミング開始 35 $sc = new \FilterTrackConsumer(OAUTH_TOKEN, OAUTH_SECRET, \Phirehose::METHOD_FILTER); 36 $sc->setLang('ja'); // 日本語のみ取得 37 $sc->setTrack(array('#ねこ')); 38 $sc->consume(); 39 } 40}

・Kernel.php (App/Console/)

PHP

1<?php 2 3namespace App\Console; 4 5use Illuminate\Console\Scheduling\Schedule; 6use Illuminate\Foundation\Console\Kernel as ConsoleKernel; 7 8class Kernel extends ConsoleKernel 9{ 10 protected $commands = [ 11 Commands\TwitterStreaming::class 12 ]; 13 14 protected function schedule(Schedule $schedule) 15 { 16 $schedule->command('twstreaming') 17 ->hourly(); 18 } 19 20 protected function commands() 21 { 22 require base_path('routes/console.php'); 23 } 24}

・filter-oauth.php (vendor/phirehose/)

PHP

1<?php 2// require_once(base_path() . '/vendor/phirehose/lib/Phirehose.php'); 3// require_once(base_path() . '/vendor/phirehose/lib/OauthPhirehose.php'); 4// require_once(app_path() . '/Tweet.php'); 5 6use App\Tweet; 7 8class FilterTrackConsumer extends OauthPhirehose 9{ 10 11 public function enqueueStatus($status) 12 { 13 // jsonをデコード 14 $tweets = json_decode($status); 15 16 // ツイートのIDが存在する かつ 文字列に"RT"を含まないツイートのみDBに格納 17 if (isset($tweets->id) && strpos($tweets->text, 'RT') === false) { 18 19 // ツイート毎にDBに格納 20 foreach ($tweets as $tweet) { 21 22 // DBに保存 23 // Tweetモデルのインスタンス$tweet_dataを作成 24 $tweet_data = new Tweet; 25 26 // DBのカラムに$tweetの中身を全部格納 27 $tweet_data->user_id = $tweet->user->id; 28 $tweet_data->user_name = $tweet->user->name; 29 $tweet_data->screen_name = $tweet->user->screen_name; 30 $tweet_data->post_id = $tweet->id; 31 $tweet_data->posted_date = date('Y-m-d H:i:s', strtotime($tweet->created_at)); 32 $tweet_data->url = 'https://twitter.com/' . $tweet->user->screen_name . '/status/' . $tweet->id; 33 $tweet_data->image_url = $tweet->entities->media->media_url_https; 34 $tweet_data->image_w = $tweet->entities->media->sizes->large->w; 35 $tweet_data->image_h = $tweet->entities->media->sizes->large->h; 36 $tweet_data->comment = trim(str_replace($tweet->entities->media->url, '', $tweet->text)); 37 $tweet_data->retweet_count = $tweet->retweet_count; 38 $tweet_data->favorite_count = $tweet->favorite_count; 39 $tweet_data->save(); 40 } 41 } 42 } 43}

・.env(関係しそうなところのみ抜粋)

PHP

1DB_CONNECTION=mysql 2DB_HOST=127.0.0.1 3DB_PORT=3306 4DB_DATABASE=socialnavi 5DB_USERNAME=root 6DB_PASSWORD=

###試したこと
mysqlにてtweetsテーブルは存在し、テスト用にクエリも入れています。

php artisan tinkerにて、以下のようにモデルをnewして取得ができています。
tinkerを実施したディレクトリはこのアプリケーションのrootディレクトリ直下です。

>>> $tweet = new App\Tweet; => App\Tweet {#706} >>> $tweet = App\Tweet::all(); => Illuminate\Database\Eloquent\Collection {#717 all: [ App\Tweet {#719 id: 1, user_id: "1", user_name: "taku", screen_name: "Taku", post_id: "1", posted_date: null, url: null, image_url: null, image_w: null, image_h: null, comment: null, retweet_count: null, favorite_count: null, created_at: null, updated_at: null, }, ], }

###補足情報(言語/FW/ツール等のバージョンなど)
PHP 7.0.20
Laravel 5.3

長文で恐れ入りますが、何卒宜しくお願い致します。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

色々と間違えすぎてるようなのでこれを使ってみてください。
https://github.com/spatie/laravel-twitter-streaming-api

・TwitterStreaming.php (App/Console/Commands/)
Laravel内からrequire_once使うことはほぼありません。

・filter-oauth.php (vendor/phirehose/)
Laravel下ではないのでuse App\Tweetしても使えません。

投稿2017/07/15 05:09

kawax

総合スコア10377

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

takutyu2

2017/07/17 12:01

vendor配下ではmodelをuseしても使えないのですね。 頂いたstreaming apiのサンプルを導入して、実装し直してみます。 有難う御座います!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問