記錄一個Laravel orm訪問器不起作用的問題

前言

今天碼磚的時候發現laravel orm的訪問器在使用者前臺死活都用不了,但是管理員後臺卻顯示正常,一時間感到很迷亂……

問題原因

經過仔細觀察,原來後臺是先查詢然後再透過手工呼叫物件屬性的方式顯示,前臺則是查詢後直接將結果返回給使用者。

翻了下官方手冊,orm的訪問器並不是在資料庫查詢的時候起的作用,而是在呼叫相應的orm物件屬性的時候才會生效。

所以,想用訪問器的話就不要把查詢到的orm物件直接返回給客戶了,中間加一層處理就好了。

附加

根據訪問器的工作原理可知,假如資料庫查詢時限制了輸出的欄位,那麼在訪問器中被限制輸出的欄位是沒辦法用$this->field或$this->attributes[‘field’]來索引的。

laravel查詢記錄時連關聯表的記錄一同查詢

前言
剛接觸laravel開發框架,一直糾結一個問題:為什麼有時候查詢記錄時會順帶輸出關聯表的內容,有時候卻不輸出?
後來翻官方文件的時候才知道關聯表預設是懶載入,只有在關聯表資料被呼叫的時候才會載入出來。為了實現在不呼叫關聯表資料的情況下預設查詢輸出關聯表資料可以將載入方式改為預載入

預載入的方法
在呼叫查詢的時候加上with(‘關聯表的函式名’)即可,如:

return User::with('comment')->where('id', $id)->first();

這樣,在輸出user記錄的時候就會預設附帶上和這個使用者有關的comment記錄。

homestead環境中使用Laravel Mix報錯的解決方法

報錯資訊彙總(解決方案在文章下面)

報錯資訊1:
此報錯在執行yarn install命令時彈出

error An unexpected error occurred: "EPROTO: protocol error, symlink '../../../parser/bin/babel-parser.js' -> '/home/vagrant/Code/cloudsystem/node_modules/@babel/core/node_modules/.bin/parser'".
info If you think this is a bug, please open a bug report with the information provided in "/home/vagrant/Code/cloudsystem/yarn-error.log".
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.

報錯資訊2:
此報錯在執行npm run watch-poll命令時彈出

sh: 1: cross-env: not found
npm ERR! file shnpm ERR! code ELIFECYCLE
npm ERR! errno ENOENTnpm ERR! syscall spawn
npm ERR! @ development: `cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js "--watch" "--watch-poll"`
npm ERR! spawn ENOENT
npm ERR!npm ERR! Failed at the @ development script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:npm ERR!     /home/vagrant/.npm/_logs/2019-06-10T01_49_28_082Z-debug.log
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! @ watch: `npm run development -- --watch "--watch-poll"`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the @ watch script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR!     /home/vagrant/.npm/_logs/2019-06-10T01_49_28_139Z-debug.log
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! @ watch-poll: `npm run watch -- --watch-poll`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the @ watch-poll script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR!     /home/vagrant/.npm/_logs/2019-06-10T01_49_28_187Z-debug.log

解決方案

問題1的解決方案:
為命令新增–no-bin-links引數

yarn install --no-bin-links

問題2的解決方案:
步驟一:
修改專案根目錄下的package.json,將`scripts中的內容修改為以下(刪掉原內容貼上新內容即可):

"dev": "npm run development",
"development": "NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
"watch": "NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
"watch-poll": "npm run watch -- --watch-poll",
"hot": "NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
"prod": "npm run production",
"production": "NODE_ENV=production node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"

如圖:
修改後的package.json檔案

步驟二:
執行命令

npm run watch --watch-poll

此時Laravel Mix即可成功執行。