20210908のlaravelに関する記事は5件です。

laravel ドット記法を使用して配列やオブジェクトから値を取得する

目的 laravelのヘルパーメソッドであるdate_get()を使い、ドット記法で配列やオブジェクトから値を取得する方法をまとめる 方法 下記のように記載することで配列やオブジェクトから任意の値を取得する事ができる。 data_get(配列やオブジェクト, ドット記法での値の指定); 参考文献 https://readouble.com/laravel/6.x/ja/helpers.html#method-data-get
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

php artisan make:authにて作成したビューがリダイレクトしてしまう。

経緯 僕が作ったアプリでは、管理者のみがユーザー登録できるように、ログイン した人間のみが、画面遷移できるよう"カスタマイズ"したかった。 解決方法 新しく自分でコントローラーを作ってください!! php artisan make:authを実行すると 下記のようなコントローラーが作成される。↓(これはほんの一部) ConfirmPasswordController ForgotPasswordController.php LoginController.php さらに下記のようなビューもが作成される。↓(これもほんの一部) register.blade.php login.blade.php  やりたかったこと 通常は、ログイン画面から、register.blade.phpに遷移するが。 ログイン→メイン画面→ボタン遷移でregister.blade.phpに繋げたかった。  やらかしたこと。。。 上記にあるLoginController.phpなど、php artisan make:auth を使って、遷移させようとしたら、、 →何やってもリダイレクトしてしまう。 なので、新しく自分で画面遷移専用のコントローラーを作成すること。 関連ワード auth リダイレクト ビュー
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

Laravelでフラッシュメッセージを表示したい。

View sessionメッセージがあれば、表示する記述を追加する。 blade.php @if (session('flash_message')) <div class="flash_message"> {{ session('flash_message') }} </div> @endif app_blade.php <body> <div id="app"> @yield('header') @yield('dheader') <main class="py-4"> // @yield('content')の上くらいに入れるのがちょうどいい。 @if (session('flash_message')) <div class="flash_message"> {{ session('flash_message') }} </div> @endif @yield('content') @yield('inc_pagination') <section id="dashboard"> @yield('dsidebar') @yield('dashboard') </section> </main> </div> @yield('footer') </body> Controller if文などで、分岐を作り、withでフラッシュメッセージをViewへ送る。 Controller.php return redirect()->back()->with('flash_message', '登録が完了しました。');
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

【初心者向】Laravel 6系でVue.jsを使用する方法

今回の記事は下記の方におすすめです! Laravelの基礎はとりあえず通した Vue.jsの基礎はとりあえず通した でもLaravelでvue.jsを使う方法がわからない 私自身、今まで実務レベルでWEBアプリを作成したことが少なく、困ったので共有します。。 今回の記事が参考になれば幸いです なお、間違いやご指摘ありましたら、コメント下さると幸いです。 実行環境 PHP 7.4 Laravel 6.2 Vue.js 2.5 ゴール Laravelのアプリ内でvue.jsを使えるようにする 手順 laravel/uiパッケージをインストール vue.jsのファイル生成 依存パッケージのインストール js/sassファイルのコンパイル実行 コンポーネントの記述 1)laravel/uiパッケージをインストール composer require laravel/ui:^1.0 --dev 2)vue.jsのファイル生成 php artisan ui vue この時、ルート直下の「package.json」の中身を確認し、 vueが表記されているか確認します。下記のようになっていれば、問題ないかと思います。 package.json { "devDependencies": { "vue": "^2.5.17", "vue-template-compiler": "^2.6.10" } } 3)依存パッケージのインストール npm install 4)js/sassファイルのコンパイル実行 npm run dev コンパイルの際、cross-env周りのエラーが出た方は、 下記の記事を参考にしてみてください。 解決策①:パスの変更 laravelでnpm run devを実行すると「cross-env: not found」というエラーが出る件対応したった 解決策②:mode_modulesディレクトリ以下を削除し、npmインストールからやり直す npmキャッシュ削除とインストール済みを削除して再インストール ちなみに、ルート直下にある「webpack.mix.js」はコンパイル時に要となるファイルです。下記のように、コンパイル後のファイルの行き先が記載されています。 webpack.mix.js mix.js('resources/js/app.js', 'public/js') .sass('resources/sass/app.scss', 'public/css'); resources/js/app.jsはコンパイルされpublic/jsディレクトリに、resources/sass/app.scssはコンパイルされpublic/cssディレクトリに出力されるよう設定されています。詳しくは、下記サイトをご覧ください。 Laravel 6.x JavaScriptとCSSスカフォールド「CSSの出力」 ここまで行えば、すでにコンポーネントが登録されているかと思います。 resources/js/app.js Vue.component('example-component', require('./components/ExampleComponent.vue').default); componentsディレクトリ以下にあるExampleComponentのVueファイルを割り当てしています。ちなみに呼び出すExampleComponent.vueファイルの初期値はこんな感じですね。 resources/js/components/ExampleComponent.vue <template> <div class="container"> <div class="row justify-content-center"> <div class="col-md-8"> <div class="card"> <div class="card-header">Example Component</div> <div class="card-body"> I'm an example component. </div> </div> </div> </div> </div> </template> <script> export default { mounted() { console.log('Component mounted.') } } </script> 5)コンポーネントの記述 あとは、呼び出し先で、コンポーネントの記述をするだけです。 今回は、welcomeページに表示してみます。下記の記述で表示できます。 <example-component></example-component> welcomeページに埋め込むと...こんな感じ↓ resources/views/welcome.blade.php <!DOCTYPE html> <head> (省略) </head> <body> (省略) <div id="app"> <example-component></example-component> </div> <script src="{{mix('js/app.js')}}"></script> </body> </html> welcomeページにコンポーネントが表示されました! 今回の記事は以上になります お役に立てたなら幸いです!!
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

npm run watchを実行したらエラーが出て、localhost:3000が立ち上がらなかった時の話

いつも、laravel+Vue.jsでSPAを作る時、プロジェクトのあるディレクトリに移動してから$ php artisan serveでlocalhost:8000を立ち上げ、ターミナルの別ウィンドウでプロジェクトのあるディレクトリに移動してから$ npm run watchをしてlocalhost:3000を開いて、書いたコードがどの様に反映されるか確認しながらやってた。 でも突然、なぜかエラーがいっぱい出て、localhost:3000がブラウザで開かなかった。 今回は、そんな時の解決法を備忘録として書いておこうと思います。 エラーの詳細 入力後の出力結果はこちら。 $ npm run watch Error: Missing binding /Users/saya/Documents/laravel_Vue_SPA/node_modules/node-sass/vendor/darwin-x64-72/binding.node Node Sass could not find a binding for your current environment: OS X 64-bit with Node.js 12.x Found bindings for the following environments: - OS X 64-bit with Node.js 10.x This usually happens because your environment has changed since running `npm install`. Run `npm rebuild node-sass` to download the binding for your current environment. at module.exports (/Users/saya/Documents/laravel_Vue_SPA/node_modules/node-sass/lib/binding.js:15:13) at Object.<anonymous> (/Users/saya/Documents/laravel_Vue_SPA/node_modules/node-sass/lib/index.js:14:35) at Module._compile (/Users/saya/Documents/laravel_Vue_SPA/node_modules/v8-compile-cache/v8-compile-cache.js:194:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10) at Module.load (internal/modules/cjs/loader.js:986:32) at Function.Module._load (internal/modules/cjs/loader.js:879:14) at Module.require (internal/modules/cjs/loader.js:1026:19) at require (/Users/saya/Documents/laravel_Vue_SPA/node_modules/v8-compile-cache/v8-compile-cache.js:161:20) at Object.<anonymous> (/Users/saya/Documents/laravel_Vue_SPA/webpack.mix.js:11:21) at Module._compile (/Users/saya/Documents/laravel_Vue_SPA/node_modules/v8-compile-cache/v8-compile-cache.js:194:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10) at Module.load (internal/modules/cjs/loader.js:986:32) at Function.Module._load (internal/modules/cjs/loader.js:879:14) at Module.require (internal/modules/cjs/loader.js:1026:19) at require (/Users/saya/Documents/laravel_Vue_SPA/node_modules/v8-compile-cache/v8-compile-cache.js:161:20) at Object.<anonymous> (/Users/saya/Documents/laravel_Vue_SPA/node_modules/laravel-mix/setup/webpack.config.js:12:1) at Module._compile (/Users/saya/Documents/laravel_Vue_SPA/node_modules/v8-compile-cache/v8-compile-cache.js:194:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10) at Module.load (internal/modules/cjs/loader.js:986:32) at Function.Module._load (internal/modules/cjs/loader.js:879:14) at Module.require (internal/modules/cjs/loader.js:1026:19) at require (/Users/saya/Documents/laravel_Vue_SPA/node_modules/v8-compile-cache/v8-compile-cache.js:161:20) at WEBPACK_OPTIONS (/Users/saya/Documents/laravel_Vue_SPA/node_modules/webpack-cli/bin/utils/convert-argv.js:114:13) at requireConfig (/Users/saya/Documents/laravel_Vue_SPA/node_modules/webpack-cli/bin/utils/convert-argv.js:116:6) at /Users/saya/Documents/laravel_Vue_SPA/node_modules/webpack-cli/bin/utils/convert-argv.js:123:17 at Array.forEach (<anonymous>) at module.exports (/Users/saya/Documents/laravel_Vue_SPA/node_modules/webpack-cli/bin/utils/convert-argv.js:121:15) at /Users/saya/Documents/laravel_Vue_SPA/node_modules/webpack-cli/bin/cli.js:71:45 at Object.parse (/Users/saya/Documents/laravel_Vue_SPA/node_modules/webpack-cli/node_modules/yargs/yargs.js:576:18) at /Users/saya/Documents/laravel_Vue_SPA/node_modules/webpack-cli/bin/cli.js:49:8 at Object.<anonymous> (/Users/saya/Documents/laravel_Vue_SPA/node_modules/webpack-cli/bin/cli.js:366:3) at Module._compile (internal/modules/cjs/loader.js:1138:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10) at Module.load (internal/modules/cjs/loader.js:986:32) at Function.Module._load (internal/modules/cjs/loader.js:879:14) at Module.require (internal/modules/cjs/loader.js:1026:19) な…長い。 冷静に、エラーメッセージを読み解いてみる こんなに長いメッセージが出ると萎えちゃってやめたくなりますが、此処はひとつ冷静に読み解いてみます。 まず注目したのは序盤のメッセージです。 Error: Missing binding /Users/saya/Documents/laravel_Vue_SPA/node_modules/node-sass/vendor/darwin-x64-72/binding.node Node Sass could not find a binding for your current environment: OS X 64-bit with Node.js 12.x Found bindings for the following environments: - OS X 64-bit with Node.js 10.x これによると、どうやらNode.jsのバージョンが今は12系になっているけれど、前は10系使ってたじゃん! と言っている模様です。 あまり記憶はないのですが、そういえば最近、nvmで他のアプリケーションを作ろうとNode.jsの環境を変更しました。(浮気者ですね) 今はNode.jsをグローバルのバージョンを見に行く様になっているので、こういうことが起きるんですね。(多分) ローカルでバージョンをそれぞれ分けるとかやれる様になるといいんですがね。今後の課題です。。 さらにその下の行を読み進めます。 This usually happens because your environment has changed since running `npm install`. Run `npm rebuild node-sass` to download the binding for your current environment. nom installをしてから、環境が変わるとこれが起きますよ。 $ npm rebuild node-sassを実行して、現在の環境用のバインディングをダウンロードしてね、とのこと。 なるほど。 それだけでいいのですか。 $ npm rebuild node-sass Binary found at /Users/UserName/Documents/laravel_Vue_SPA/node_modules/node-sass/vendor/darwin-x64-72/binding.node Testing binary Binary is fine node-sass@4.14.1 /Users/UserName/Documents/laravel_Vue_SPA/node_modules/node-sass これで、また$ npm run watchすれば、ちゃんとlocalhost:3000の画面がブラウザで立ち上がりました! よかったよかった。
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む