フリーランス 技術調査ブログ

フリーランス/エンジニア Ruby Python Nodejs Vuejs React Dockerなどの調査技術調査の備忘録

Laravel フロントエンド

はじめに

  • 前回、laravel/uiをインストールすることができたので、実際にlaravel/uiを利用してみる
$ composer require laravel/ui

ログイン/ユーザー登録スカフォールドを生成

$ php artisan ui vue --auth
$ npm install
$ npm run watch
  • laravel/uiのエラーメッセージを日本語にしたい場合、下記のgithubからダウンロードし、ダウンロードしたファイルを「resources/lang/ja」フォルダに格納する github.com

コントローラ

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Category;
use Illuminate\Support\Facades\DB;

class TestController extends Controller
{
    public function index(){
        $categories = Category::all();
        return view('tests.test', compact('categories'));
    }
}

View(blade)

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="csrf-token" content="{{ csrf_token() }}">

        <title>Laravel</title>

        <link href="{{ asset('css/app.css') }}" rel="stylesheet" type="text/css">
    </head>
    <body>
        <div class="alert alert-primary" role="alert">
            BootStrapのアラート
        </div>
        <h3>カテゴリ一覧</h3>

        @foreach($categories as $category)

            {{$category->id}}<br>
            {{$category->name}}<br>

        @endforeach
        <div id="app">
            <example-component></example-component>
            <p>@{{ message }}</p>
        </div>
    <script src="{{ asset('js/app.js') }}"></script>
    </body>
</html>

Vuejs

  • 基本的にデフォルトに設定された状態のままです
/**
 * First we will load all of this project's JavaScript dependencies which
 * includes Vue and other libraries. It is a great starting point when
 * building robust, powerful web applications using Vue and Laravel.
 */

require('./bootstrap');

window.Vue = require('vue');

/**
 * The following block of code may be used to automatically register your
 * Vue components. It will recursively scan this directory for the Vue
 * components and automatically register them with their "basename".
 *
 * Eg. ./components/ExampleComponent.vue -> <example-component></example-component>
 */

// const files = require.context('./', true, /\.vue$/i)
// files.keys().map(key => Vue.component(key.split('/').pop().split('.')[0], files(key).default))

Vue.component('example-component', require('./components/ExampleComponent.vue').default);

/**
 * Next, we will create a fresh Vue application instance and attach it to
 * the page. Then, you may begin adding components to this application
 * or customize the JavaScript scaffolding to fit your unique needs.
 */

const app = new Vue({
    el: '#app',
    data: {
        message: 'こんにちは!!!'
    },
});

実行結果

f:id:PX-WING:20201014005826p:plain