こんにちは!むちょこです。今日もPHPが楽しいです? 今日はLaravelで多対多リレーションシップを扱う方法について解説したいと思います。 環境はPHP7、Laravel5.6です。 1. テーブル設計 Laravelの$php artisan make:authで生成されたusersテーブルが予めあるものとします。今回は、そのユーザに任意のスキルを登録できる機能を作りたいと思います。 ここでの”スキル”は、現時点で用意されているもの以外にも今後追加される可能性が高い類のデータとします。 そのため、usersテーブルに直接追加するのではなくskillsテーブルを別途用意し、多対多の関係となった各テーブルを中間テーブルで繋ぎます。 Laravelの場合、中間テーブルの命名はアルファベット順でテーブル名を並べるという規則がありますので、今回の場合はskill_userテーブルという名前になり
Laravel 標準のユーザー認証機能でログインされている時に、どうやってユーザー情報をとるかや、ログイン時の条件分岐の仕方などをまとめました。動作を確認している Laravel のバージョンは 5.7 です。 <目次> ログインしていない場合はログインページへ移動させる ログインしているユーザーの情報を取得する。 ログインしているかどうかを判定する。 ログイン中のユーザーをログアウトさせる。 ログインしていない場合はログインページへ移動させる。 モデル名Controller.php ファイルで以下のように __construct クラスのコードを追加します。 class モデル名Controller extends Controller { public function __construct(){ $this->middleware('auth'); } : : } 全てのページで、
routes/web.php Route::get('/profile', 'ProfileController@index'); Route::post('/profile', 'ProfileController@store'); Route::get()がフォームを表示するルート定義、 Route::post()が送信された値を受け取るためのルート定義です。 今回はフォームの表示と送信された値の受け取りを同じURLで行います。 もしこれらを別のURLにしたい場合は、それぞれの第一引数を適宜変更してください。
file system 悩み事 ファイルの内容を取得したい 解決方法 File::get() メソッドを利用します $contents = \File::get($filename); アドバイス ファイルが見つからない場合は例外が返却されます 具体的には Illuminate\Filesystem\FileNotFoundException がスローされます try/catchを利用して、エラーを捕捉する様にしましょう try { $contents = \File::get($filename); } catch (\Illuminate\Filesystem\FileNotFoundException $exception) { die("ファイルがありません"); } Author:Chuck Heintzelman Editor and Translator:Yuuki Take
こんにちは!むちょこです。 今日はリクエストでいただいたsocialiteを使ったOAuth認証の実装方法について書いてみようと思います☆ 既にたくさんの類似記事があるのですが、私の周りには「読んでもよくわからなかった…」という方がたくさんいたので その方々の疑問にできる限り丁寧に答える形で書きました。たぶんこれ以上丁寧な記事はないと思います……! その代わりかなり長いので、不要なところはどんどん飛ばして読んでくださいね。 1. 前提条件 環境 Laravel Framework 5.7.13 要件 通常のパスワード認証と共存する。認証方法に関わらず、メールアドレスが一致すれば同一人物とみなす。メールアドレス情報は必須とする。SNSの情報を認証以外には使用しない。今回の対象プロバイダはTwitterのみだが、今後Facebookなど他のプロバイダを追加する可能性がある。 実装済みの機能 以
FacebookやTwitterなどのアカウントを使ったソーシャルログイン(SNSログイン)。個別に実装しようとすると結構手間ですが、Socialiteを使えば実装の手間を減らせますね。 Laravel Socialiteは複雑なSNS認証を高機能で使いやすいインターフェイスとして手軽に組み込めるパッケージです。 SocialiteがOAuthプロバイダーとしてサポートしているのはGoogle、Facebook、Twitter、LinkedIn、GitHub、Bitbucketです。サポート対象が拡大される予定はないものの、コミュニティーが開発を進めているコレクション、Socialite Providersを使えば非公式ですが多くのSNSをSocialiteのOAuthプロバイダーとして使えます。詳しくは、後で説明します。 この記事では、Laravelアプリケーションのインスタンスが稼働し
public function test(Request $request) { dd( // リクエストURIの取得 $request->path(), // リクエストのURIが指定されたパターンに合致するか確認 $request->is('*/sumo'), // 完全なURLを取得(クエリ文字列なし) $request->url(), // 完全なURLを取得(クエリ文字列付き) $request->fullUrl(), // リクエストメソッドの取得 $request->method(), // リクエストメソッドの取得 $request->isMethod('post') ); return view('test.index'); }
こんにちは、エリカです。 Laravel をさらっと使って、カンタンなお問い合わせフォームを作ってみます。 環境は、Laradock + Laravel です。前回の記事を参考にセットアップしてください Laravel ではもはや Model という言葉があまり出てこないのですが、便宜的におなじみ MVC な感じで進めていきます。 お問い合わせ用モデルクラスの作成 今回はデータベース抜きで話を進めますが、まずは「お問い合わせ」というモデルクラスを作ります。したがって、ビューやコントローラでは、この「お問い合わせ」というモデルを主に取り扱います。 また、今回は言語設定も省いていますので、全て英語で処理していきます。後ほど、日本語の対応方法なども紹介していければと思います。 というわけで「 Contact 」という名前でモデルを作ります。 まずは、各種 docker コンテナを起動してください
こんなとき使う @foreach($titles as $title) @if($index == 4) @endif @endforeach こんなふうに、4番目のデータのときにだけなにかをしたい場合。 書き方いくつかあります。 $loop変数を使う(Laravel5.3以上) @foreach ($titles as $title) @if ($loop->index == 3) @endif @endforeach 0から読み込んだ回数が入ります。 $loop->iterationなら1から始まります。 配列のキーを用いる(Laravel5.2以下) @foreach ($titles as $index => $title) @if ($index == 3) @endif @endforeach 配列などは、キーを明示しなければ、0,1,2,3...というキーを用いるのを応用する
LaravelのViewComposerでViewで使用する共通のデータを設定する Laravelでサイドバーなどすべてのページで共通するデータを表示したいときはViewComposerを使用すると便利です。 投稿日2017年09月08日 更新日2017年09月08日 Laravel 5.4を使用しています。 ComposerServiceProviderの作成 最初にapp/Providers/にComposerServiceProvider.phpを作成します。 例えばViewに$hogeという変数で「hogehoge」と表示させたい場合は下記のようにします。 app/Providers/ComposerServiceProvider.php <?php namespace App\Providers; use Illuminate\Support\Facades\View; use
環境 PHP:7.1.27 Laravel:5.7 はじめに 前回、Cloud9にLaravel5.7を導入する記事を書きました。 参考↓ Cloud9にLaravel5.7を実装する 環境できたーと喜ぶのもつかの間に、500エラー地獄にハマりました。 500エラーが発生 環境もできた、PHPも動いた、DBも問題無い。 よーし、やるぞ!と始めるも、 500エラー!! PHPのバージョンアップから続くトライアンドエラーの日々を表すかのような砂漠のイラスト・・・ まさに管理人の心を表すかのようです・・・ no application encryption key has been specified. がログに・・・ ーーー砂漠をヒタヒタ彷徨うこと数時間 ログを見ると下記エラーが・・・ production.ERROR: No application encryption key has be
こんにちは、okutani(@okutani_t)です。超かんたんなTipsなのですが、Laravelのプロジェクトをgit cloneしてきたときの手順を紹介します。今回はLaravel5.1で動作を確認しています。 よくあるケースとしては、「Laravelのプロジェクトに途中参加したけど、vendorとかないし動かないしなにこれ」といった場面ですね。あ、僕のことです。 そもそもLaravelって?という方は、過去記事の「Laravel導入方法&初期設定まとめ | vdeep」をご覧ください。 Laravelをgit cloneしてくるとき、いくつかコマンドを覚えておく必要があります。覚えるまではこちらの記事をブックマークでもしておくと便利かと思います。 では、さくっと手順を見ていきましょう。 スポンサーリンク Laravelのプロジェクトをgit cloneした後にやること とりあえず
この記事では、Mac の環境をメインに説明しますが、Windows 環境で行う方法も補足します。 開発環境の構築は、ターミナルを使ってコマンドで指示をします。 この記事内では、コマンド自体の詳しい説明はしませんので、不明な箇所は調べてください。 Windows の方で、コマンドプロンプトを利用している方は、Git for Windows をインストールして、Git Bash というソフトを使って作業してください。 最初に Laravel、VirtualBox、Vagrant、Homestead、Composer について簡単に学んだ後に、実際に開発環境の構築を行います。 私自身未熟ですので説明に誤りがあるかもしれません。 誤りがあれば、ご指摘ください ((_ _ (´ω` )ペコ 第1章 Laravel とは 第2章 VirtualBox とは 第3章 Vagrant とは 第4章 Ho
はじめに 前回の記事:Qiita - 【Laravel超入門】開発環境の構築(VirtualBox + Vagrant + Homestead + Composer) 前回の記事は Laravel 5.4 のときに構築した手順です。 少し古い箇所もあるため書き直しました。 今回は下記のバージョンで構築した手順を記載しています。 OS git VirtualBox Vagrant Laravel の最新バージョンのダウンロード方法と、LTS の Laravel 5.5 の最新バージョンのダウンロード方法を説明しています。 今回は mac のみの手順となります。Windowsの方は前回の記事を参考にしてください。 前回の記事の第1章から第5章で Laravel、VirtualBox、Vagrant、Homestead、Composerの基本的な説明をしております。 この記事では第6章にあたる開
はじめに 対象者 JavaScript / Vue.js / PHP / Laravel の入門レベルの方、入門書を読んだあとに「何か作ってみたい」という方が、それぞれの知識を組み合わせて本格的なアプリケーションを開発できるようになることを目指しています。 上記のいずれかの知識がまったくない状態だとこのチュートリアルの内容は少し難しいかもしれません。チャレンジするのもいいですが、参考文献に挙げた市販の入門書か、関連記事のチュートリアルを先に読んでおくと理解しやすいと思います。 学べること Vue.js と Laravel を組み合わせて SPA を構築する方法 SPA におけるクッキー認証と CSRF 対策 Vue Router を使用した画面遷移 Vuex を使用した状態管理 Vue でのタブやローディング UI の表現 SPA におけるエラー処理 扱わないこと デザインと CSS コー
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く