[Laravel][備忘録]バリデーション

コントローラー

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Post;

class PostController extends Controller
{
    public function create(){
        return view('post.create');
    }
    public function store(Request $request){
        $validated = $request->validate([
            'title' => 'required|max:20',
            'body'  => 'required|max:400',
        ]);
        $post = Post::create($validated);
        /*
        saveを使う場合
        $post = new Post();
        $post->title = $validated['title'];
        $post->body = $validated['body'];
        $post->save();
        */
        /*
        withを使わない場合
        $request->session()->flash('message','保存しました');
        return back();
        */
        return back()->with('message','保存しました');
    }
}

 

表示側

<div class="max-w-7xl">
    @if(session('message'))
        <div class="text-red-600">
            {{ session('message') }}
        </div>
    @endif
    <form method="post" action="{{ route('post.write') }}">
        @csrf
        <div>
            <x-input-error :messages="$errors->get('title')" class="mt-2" />
            <input type="text" name="title" class="w-auto py-2 border border-gray-300 rounded-md" id="title" value="{{old('title')}}">
        </div>

        <div>
            <x-input-error :messages="$errors->get('body')" class="mt-2" />
            <textarea name="body" class="w-auto py-22 border border-gray-300 rounded-md" id="body" cols="30" rows="5">{{old('body')}}</textarea>
        </div>

        <x-primary-button>{{ __('送信する') }}</x-primary-button>
    </form>
</div>

[Laravel][備忘録]POSTリクエストの内容をDBへ保存

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Post;

class PostController extends Controller
{
    public function create(){
        return view('post.create');
    }
    public function store(Request $request){
        $post = Post::create([
            'title' => $request->title,
            'body'  => $request->body
        ]);
        return back();
    }
}

[Laravel Sail]メール送信備忘録

ファイル作成

sail artisan make:mail UserRegistered

 
Mail\UserRegistered.php
追記

public $user;

 
変更

public function content(): Content
{
    return new Content(
        view: 'mail.user_registered',
    );
}

 
resources\views\mail\user_registered.blade.php

<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>ユーザー登録通知</title>
</head>
<body>
    {{ $user->name }}さんが登録しました。<br>
    <a href="{{route('dashboard')}}">dashboard</a>
 
</body>
</html>

 
app\Http\Controllers\Auth\RegisteredUserController.php
追記

use App\Mail\UserRegistered;
use Illuminate\Support\Facades\Mail;

変更storeメソッドのeventメソッド後に追記

Mail::to('送信先メアド')->send(new UserRegistered($user));

[Laravel]AES_ENCRYPT

$result = DB::table('users')->insert([
                ['email' => \DB::raw("AES_ENCRYPT('{$xxx}',SHA2('".env('DB_ENCRYPT_KEY')."',512))"), 'token' => \DB::raw("AES_ENCRYPT('{$yyy}',SHA2('".env('DB_ENCRYPT_KEY')."',512))")]
            ]);

[Shopify]テーマに画像設定項目を設置

config/settings_schema.jsonへ下記を追記

[
  {
    "name": "xxx",
    "theme_name": "xxx",
    "theme_version": "xxx",
    "theme_author": "xxx",
    "theme_documentation_url": "https:\/\xxx",
    "theme_support_url": "https:\/\/yyy"
  }
//---------ここから---------
  ,{
    "name": "トップページ",
    "settings": [
      {
        "type": "image_picker",
        "id": "main_image",
        "label": "メイン画像",
        "info": "メイン画像です"
      }
    ]
  }
//--------ここまでを追記-----------
]

 
画像を表示したい場所へ下記コードを追記

{% if settings.main_image != blank %}<img src="{{ settings.main_image.src | image_url: width: 1040, format: 'pjpg' }}">{% endif %}

[Shopify]コレクション内商品をタグでフィルタ

パターン1
URLが「/collections/new/」のコレクションがある時、下記のURLでは「タグ」が付与された商品のみが一覧に表示される

/collections/new/タグ

 
パターン2
下記のURLでは「タグ1」「タグ2」両方付与された商品のみが一覧に表示される

/collections/new/タグ1+タグ2

[Liquid]会員登録、ログイン

{%- if shop.customer_accounts_enabled -%}
    {%- if customer -%}
    <a href="{{ routes.account_url }}">会員情報</a>
    <a href="{{ routes.account_logout_url }}">ログアウト</a>
    {%- else -%}
    <a href="{{ routes.account_register_url }}">会員登録</a>
    <a href="{{ routes.account_login_url }}">ログイン</a>
    {%- endif -%}
{%- endif -%}

[Laravel]AES_DECRYPT

$record = DB::table('xxx')
            ->selectRaw("id,AES_DECRYPT(`yyy`,SHA2(:key1,512)) AS shop,AES_DECRYPT(`zzz`,SHA2(:key2,512)) AS token",['key1'=>env('DB_ENCRYPT_KEY'),'key2'=>env('DB_ENCRYPT_KEY')])
            ->whereRaw("AES_DECRYPT(`yyy`,SHA2(:key,512)) = :yyy", ['key'=>env('DB_ENCRYPT_KEY'),'yyy'=>$yyy])
            ->first();

[Shopify]セクションに可変長カスタム入力欄を設置

対象セクションのschemaを設定

{% schema %}
  {
    "name": "商品詳細",
    "tag": "div",
    "class": "product",
    "settings": [],
    //---------ここから---------
    "blocks": [
      {
        "type": "userlinks",
        "name": "リンク",
        "settings": [
          {
            "type": "text",
            "id": "linktext",
            "label": "リンクテキスト"
          }, {
            "type": "url",
            "id": "linkurl",
            "label": "リンクURL"
          }
        ]
      }
    ]
    //--------ここまでを追記-----------
  }
{% endschema %}

 
対象セクションの表示したい場所へ下記コードを追記

{%- for block in section.blocks -%}
  {%- case block.type -%}
    {%- when 'userlinks' -%}
    <h3>{{ block.name }}</h3>
    {%- if block.settings.linktext != blank -%}
      {%- if block.settings.linkurl != blank -%}
        <a href="{{ block.settings.linkurl }}">{{ block.settings.linktext | escape }}</a><br>
      {%- endif -%}
    {%- endif -%}
  {%- endcase -%}
{%- endfor -%}