DB::raw を使えばいけるんだろうけど
こんな感じでもいけるようなのでメモメモ
1 2 3 4 |
$unique = DB::table("table") ->distinct('column') ->count('column'); |
DB::raw を使えばいけるんだろうけど
こんな感じでもいけるようなのでメモメモ
1 2 3 4 |
$unique = DB::table("table") ->distinct('column') ->count('column'); |
Laravelのblade内で、DataTablesを使ってたんだけど
td の中にプルダウンを設置したくて、
どうにかできないかと
もっとよい方法がありそうだけど
とりあえずこれでしのいだ
1 2 3 4 5 6 7 8 |
{ 'data' : 'test', render: function(data, type, row) { let tag = '{{ Form::select('test[]', $testSelectList, null, ['class'=>'form-control']) }}'; tag = tag.replace('value="'+data+'"', 'value="'+data+'" selected="selected"'); return tag; } }, |
Laravel で ajax の POST通信をしようとしたところ
エラーとなったのでその対処方法をメモメモ
HTMLのどっかにmetaタグでcsrf-tokenの文字列を記載。
1 |
<meta name="csrf-token" content="{{ csrf_token() }}"> |
あとは、ajaxのところで、headerにこの値を
セットすればOK!!
1 2 3 4 5 6 |
$.ajax({ 'type': 'POST', 'dataType': 'json', headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') }, |
ついでに時間がたったら更新しておく
1 2 3 4 5 6 |
setInterval(refreshToken, 3600000); //1 hour function refreshToken(){ $.get('refresh-csrf').done(function(data){ $('meta[name="csrf-token"]').attr('content',data); }); } |
Laravel使ってて
.envファイルとか更新したのに
反映されなかったりするのは
すべてキャッシュのせいだー!
コマンドラインで
これをたたけば解決なり!
1 2 3 4 |
php artisan cache:clear php artisan config:clear php artisan route:clear php artisan view:clear |
Console\Comands でバッチプログラムを作成したときに
パラメータを渡す方法をメモメモ
2つあって
1 2 3 4 5 6 7 8 9 10 |
// こんな感じにすると protected $signature = 'test:hoge {--hogehoge=}'; public function handle() { // こんな感じで受け取れる $hogehoge = $this->option("hogehoge"); // 実行するときはこんな感じ php artisan test:hoge --hogehoge=1 |
もうひとつは
1 2 3 4 5 6 7 8 9 10 |
// こんな感じにすると protected $signature = 'test:hoge {hogehoge?}'; public function handle() { // こんな感じで受け取れる $hogehoge = $this->argument("hogehoge"); // 実行するときはこんな感じ php artisan test:hoge 1 |
両方ともパラメータなしでも実行できる
なんとなくやる機会があったので
いちおう忘れないようにメモメモ
まずは composer で phpmailer をインストール
1 |
composer require phpmailer/phpmailer |
https://github.com/PHPMailer/PHPMailer
あとはこんな感じ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
use PHPMailer\PHPMailer\PHPMailer; $mail = new PHPMailer(true); $host = 'smtp.gmail.com'; $userName = 'xxx@gmail.com'; $password = 'xxxxx'; $from = 'xxxxxx'; $fromname = 'xxxxx'; $to = 'xxxxxxxxx'; $subject = '件名'; $body = '本文'; $mail->SMTPDebug = 2; $mail->isSMTP(); $mail->SMTPAuth = true; $mail->Host = $host; $mail->Username = $userName; $mail->Password = $password; $mail->SMTPSecure = 'tls'; $mail->Port = 587; $mail->CharSet = "utf-8"; $mail->Encoding = "base64"; $mail->setFrom($from, $fromname); $mail->addAddress($to); $mail->Subject = $subject; $mail->Body = $body; $mail->send(); |
Laravelで、mysqlにCSVファイルをインポートしようとしたら
エラーになったので、そのときの対応をメモメモ
CSVの読み込みはこんな感じ
1 |
DB::statement('LOAD DATA LOCAL INFILE "/tmp/sample.csv" INTO TABLE sampleTable FIELDS TERMINATED BY \',\' OPTIONALLY ENCLOSED BY \'"\''); |
そうすると
こんなエラーや
string(420) “SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active. Consider using PDOStatement::fetchAll(). Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute.
こんなエラーで怒られる、、、
PDO::exec(): LOAD DATA LOCAL INFILE forbidden
コンソール画面から直接SQLを実行するといけるのに
config/database.php に
下記2行を追加してあげたらうまくいった
PDO::ATTR_EMULATE_PREPARES => true,
PDO::MYSQL_ATTR_LOCAL_INFILE => true,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
'mysql' => [ 'driver' => 'mysql', 'url' => env('DATABASE_URL'), 'host' => env('DB_HOST', 'xxx.xxx.xxx.xxx'), 'port' => env('DB_PORT', '3306'), 'database' => env('DB_DATABASE', 'xxxxx'), 'username' => env('DB_USERNAME', 'xxxxx'), 'password' => env('DB_PASSWORD', 'xxxxx'), 'unix_socket' => env('DB_SOCKET', ''), 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'prefix_indexes' => true, 'strict' => true, 'engine' => null, 'options' => extension_loaded('pdo_mysql') ? array_filter([ PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'), PDO::ATTR_EMULATE_PREPARES => true, PDO::MYSQL_ATTR_LOCAL_INFILE => true, ]) : [], ], |
Laravelで新規にサイト作ろうとすると
パーミッションで怒られるはず、、、
やり方は、いくつかあるけど
この方法が自分好みなので、メモメモ
おおざっぱな手順
・[laravel] グループを新規に作成して
・[apache] など必要なユーザを [laravel] グループに追加する
・[storage] 配下 の所有グループを [laravel] に変更
・新規で作成されるファイルのパーミッションを指定
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# groupadd laravel # gpasswd -a apache laravel # chown -R :laravel ./storage # find ./storage -type d -exec chmod 775 {} \; # find ./storage -type f -exec chmod 664 {} \; # find ./bootstrap/cache -type d -exec chmod 775 {} \; # find ./bootstrap/cache -type f -exec chmod 664 {} \; # find ./storage -type d -exec chmod g+s {} \; # find ./bootstrap/cache -type d -exec chmod g+s {} \; # setfacl -R -d -m g::rwx ./storage # setfacl -R -d -m g::rwx ./bootstrap/cache |
以上!
Laravel でバッチ処理を作ってたんだけど、
ログを吐こうとすると、
storage/logs/ 配下の larabel-xxxx-xx-xx.log ファイルに
全部まとまられてしまって、管理しづらいので
別に書き出す方法をメモメモ
いろいろ方法はあるみたいだけど
これが一番簡単にできたので採用!
手順は2つ
1.config/logging.php のchannels に、’daily’ をコピッてひとつ追加
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
'channels' => [ ・ ・ ・ 'errorlog' => [ 'driver' => 'errorlog', 'level' => 'debug', ], 'batch' => [ 'driver' => 'daily', 'path' => storage_path('logs/batch.log'), 'level' => 'debug', 'days' => 14, ], ], |
2.該当の処理でログを書き出す
1 2 3 4 5 6 7 8 |
use Illuminate\Support\Facades\Log; ・ ・ ・ public function handle() { Log::setDefaultDriver('batch'); Log::info('start'); |
あと、キャッシュ削除しないとうまく動かないかも
1 |
php artisan config:cache |
どうしようもないときの手段として
1 2 3 4 5 |
$query = 'INSERT INTO test (a) VALUES (1)'; DB::insert($query); $query = 'truncate table test'; DB::statement($query); |