Laravel_DB

Laravel: migration file を削除してしまい php artisan migrate:reset でエラー

環境: Laravel@9.52.4 migrate 済みの migration ファイルを削除してしまい、php artisan migrate:reset で Migration not found とエラーが出てしまった場合。 solution) php artisan migrate:fresh >>> DB全てのテーブルが削除された後、migrate する処理 …

Laravel + MongoDB: error: Indirect modification of overloaded property App\....

環境: laravel@8.83.23 jensseger/laravel-mongodb@3.8.0 Model Structure Profile: { roles: [’admin’] } $profile = App\Profile::find(1); $profile->roles = array_push($profile->roles, 'superAdmin') >>> error: Indirect modification of overloade…

Laravel + mongoDB find deepley nested collection with array

環境: laravel@8.79.0 Jenssegers/mongodb@3.8.4 サーチ対象の model 構造は以下とする Order { _id: ********, shippingStatus: [ "status" => "発送完了", "shippingMethod" => "クロネコヤマト", "inquiryNumber" => "11111111" ] } Order model から shi…

Laravel: 親モデルを削除と同時に子モデルを削除する cascading soft-delete / MySQL case and MongoDB case

環境: Laravel@8.79.0 Jenssegers/mongodb@3.8.4 前提: 親モデル >>> User.php MySQL子モデル >>> Post.php MongoDB子モデル >>> ProfileDetail.php case MySQL soft-delete に対応させる migration file に以下を加筆 Schema::create('users', function B…

Laravel MongoDB: mongodbを使用時にmigration file で table を定義する

環境 Laravel@8.79.0 Jenssegers/mongodb@3.8.4 migration をしなくても ORM から query することで DB に table を作成することができるが、 migration file を作って DB table を定義したい場合、 php artisan make:migration create_MONGO_TABLE_MODEL_ta…

Upgrade Laravel from 7.x to 8.x / Upgrade PHP7.2 to PHP8.0 / Upgrade jenssegers/laravel-mongodb on AWS Cloud9

PHPをUpgrade sudo add-apt-repository ppa:ondrej/php sudo apt-get update sudo apt-get install libapache2-mod-php8.0 sudo a2dismod php7.2 sudo a2enmod php8.0 sudo apt-get install php8.0-dom php8.0-mbstring php8.0-zip php8.0-mysql php8.0-comm…

Laravel + MongoDB + jensseger/laravel-mongodb: データベースからランダムにドキュメントを取得する方法

laravel@7.28.4 jensseger/laravel-mongodb@3.7.3 $result = myModel::raw(function($collection){ return $collection->aggregate([ ['$sample' => ['size' => 3]] ]); }) ref: github.com docs.mongodb.com stackoverflow.com

Laravel + MongoDB + jensseger/laravel-mongodb: update deeply nested mongodb documents

updated: 2022/3/29 環境: laravel@7.28.4 mongodb-php-driver@1.7 jensseger/laravel-mongodb@3.7.0 編集したいprofielDetail model のstructureは下記とする。 Userモデルは一つのProfileDetailモデル(hasOne)を持つ。 hasOneModel では protected $filla…

Laravel Database 操作 PHP Notice: Indirect modification of overloaed property ...

Laravel@5.8.33 mongoDB@4.0.10 あるEloquent ORMのネストされたデータdetails['description']をアップデートしたい。 $target = App\ModelName::first() $target->details['description'] >>> "test" と返るので直接 $target->details['description'] = "up…

jenssegers/laravel-mongodb Basic Usage on cloud9 with php7 ⑩ 特定のcollectionのネストされたキーの値を取得する

mysql の select XX に当たるのが mongoDB では projection 'user_id' => 1 は true という意味 参考: github.com jenssegers/laravel-mongodb を使わないで mongoDB php driver を使うなら 参考: MongoDB\Collection::findOne() — PHP Library Manual 1.4…

Laravel 5.x Factory と Faker で relational なモックアップを作る。

updated: 2021/6/1 jenssegers/mongodb の mysql relationships で使用可能なことが確認済み php artisan make:factory FACTORY_NAME factory1 factory2 seeds/DatabaseSeeder.php おまけ ランダムな文字列を生成 return [ 'randomText' => $faker->regexify…

jenssegers/laravel-mongodb Basic Usage on cloud9 with php7 ⑨ Mysql relationships で mongoDB と mysql 上のデータを合わせて返す。

updated: 2018/10/1 Mysql relationships については下記参照 monteecristoo.hatenablog.com mongoDBとMysqlをjoin して返したい場合、mongoDBはjoinに対応していないので、 PixieImage は mongoDB上のデータベース user はmysql のデータベース さらにこれ…

php artisan db:seed でエラー

データベーステーブルを適切に削除しなかったため。 solution: php artisan migrate:refresh //

jenssegers/laravel-mongodb Basic Usage on cloud9 with php7⑧ sample case

// App\User ===> App\MongoMessage Mysql Relationships App\MessageGroup ===> App\MongoMessage hasMany Relationships App\User public function mongoMessages() { return $this->hasMany(MongoMessage::class); } App\MessageGroup public function mo…

Laravel 5.x よく使う query builder

// updated: 2018/4/14 first() get() >>> get() で複数の collection が返ってくるのでさらに処理したい場合は foreach で処理 e.g.) $messages = App\Message::whereNull("body")->get(); foreach($messages as $message) { $mssage->delete(); } whereIn(…

jenssegers/laravel-mongodb Basic Usage on cloud9 with php7⑦ convert mongoDB to Mysql

// setting up Mysql Relationships create the route and controller fetch the data from mongoDB extract the value from the response(3) fetch the data from Mysql using the value(4) return the response 1. @app\user use Jenssegers\Mongodb\Eloqu…

jenssegers/laravel-mongodb Basic Usage on cloud9 with php7⑥ Mysql relationships test

// App\User >>>> Mysql based Database use Jenssegers\Mongodb\Eloquent\HybridRelations; use App\MongoMessage; class User extends Authenticatable { use HybridRelations; use MongoMessage; public function mongoMessages() { return $this->hasMan…

jenssegers/laravel-mongodb Basic Usage on cloud9 with php7⑤ EmbedsMany relationships test

// tinker $mongoUser = new MongoUser(['id' => 1, 'name' => 'Max']); $mongoMessage = new MongoMessage(['id' => 1, 'group' => '1', 'body' => 'Something', 'sender' => 'max', 'receiver' => 'Monica']); $mongoMessage = $mongoUser->mongoMessages(…

jenssegers/laravel-mongodb Basic Usage on cloud9 with php7④

// App/MongoUser namespace App; use Moloquent; use MongoMessage; class MongoUser extends Moloquent{ protected $connection = 'mongodb'; protected $collection = 'mongoUsers'; public function mongoMessages() { return $this->embedsMany('MongoM…

error: SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = c9 and table_name = migrations)

// cloud9 上で php artisan migrate:reset 時に下記のエラー SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = c9 and table_name = migrations) composer dump-autoload してもダメな時 な…

jenssegers/laravel-mongodb Basic Usage on cloud9 with php7③ 導入するメリット

// updated: 2018/3/6 jenssegers/laravel-mongodb を導入するメリット MySQL Relations If you're using a hybrid MongoDB and SQL setup, you're in luck! The model will automatically return a MongoDB- or SQL-relation based on the type of the rela…

Error: In Database.php line 198: No suitable servers found ('serverSelectionTryOnce' set) : [connection refused calling ismaster on '127.0.0.1:27017']

updated: 2018 9/26 Error: In Database.php line 198: No suitable servers found ('serverSelectionTryOnce' set) : [connection refused calling ismaster on '127.0.0.1:27017'] mongoDB と mysql を使った multiple Database 環境で php artisan migrat…

jenssegers/laravel-mongodb Basic Usage on cloud9 with php7② migration file による validation

// updated: 2018/1/17 public function up() { Schema::connection('mongodb')->create('group_messages', function (Blueprint $collection) { $collection->increments('id'); $collection->timestamps(); $collection->string('body');〇 $collection->u…

jenssegers/laravel-mongodb Basic Usage on cloud9 with php7 ①

// updated 2018/1/5 Installation monteecristoo.hatenablog.com monteecristoo.hatenablog.com Laravel Configuration @config\app.php add the service provider Jenssegers\Mongodb\MongodbServiceProvider::class, @config/database.php 'mongodb' => […

Laravel & redis 接続テスト

Installation composer require predis/predis @web.php Route::get('/redis_test', function() { print_r(app()->make('redis')); } // Illuminate\Redis\RedisManager Object ( [driver:protected] => predis [config:protected] => Array ( [default] => …

Laravel Database Seed

create a Seed file php artisan make:seeder UsersSeeder register Seed file @DatabaseSeeder.php use Illuminate\Database\Seeder; class DatabaseSeeder extends Seeder{ public function run() { $this->call(UsersSeeder::class); }} define UsersSeed…

Laravel Migration file 操作

// php artisan make:migration create_messages_table --create="messages" Schema::create付きのmigration fileが生成される。 function up → php artisan migrate した時の動作 funciton down → php artisan migrate:rollback した時の動作 migrateをやり…

Laravel 5.4 Database Structure

Relationships model A: User protected $fillable = [ DBのカラム] public function messages) { return $this->hasMany(Message::class); } model B: Message protected $fillable = [ DBのカラム] foreign_key は登録しなくてOK public function users() …