もし、質問者さんがやりたいことを私が実現するなら、下記の様にします。
以下に長々とサンプル環境構築のソースを貼っていますが、結論だけ知りたいのであればControllerを見て貰えば良いと思います。
ちなみに、上記のエラーはphpの根本的な理解不足によるものかと。
配列定義の中にforeach
は書けませんよ。
サンプル環境定義
(データベース定義)
php
1<?php
3use Illuminate\Support\Facades\Schema;
4use Illuminate\Database\Schema\Blueprint;
5use Illuminate\Database\Migrations\Migration;
7class CreateValidationMastersTable extends Migration
8{
9 1213
14 public function up()
15 {
16 Schema::create('validation_masters', function (Blueprint $table) {
17 $table->increments('id');
18 $table->string('field');
19 $table->string('rule');
20 $table->timestamps();
21 });
22 }
24 2728
29 public function down()
30 {
31 Schema::dropIfExists('validation_masters');
32 }
33}
(テスト用レコード)
php
1<?php
3use Illuminate\Database\Seeder;
4use App\ValidationMaster;
6class ValidationMasterSeeder extends Seeder
7{
8 1112
13 public function run()
14 {
15 ValidationMaster::create(['field'=>'name','rule'=>'required']);
16 ValidationMaster::create(['field'=>'mail','rule'=>'required|string|email|max:255']);
17 }
18}
実装
テスト用View
html
1@extends('layouts.app')
2
3@section('content')
4<div class="container">
5 @if ($errors->any())
6 <div class="alert alert-danger">
7 <ul>
8 @foreach ($errors->all() as $error)
9 <li>{{ $error }}</li>
10 @endforeach
11 </ul>
12 </div>
13 @endif
14 <form method="POST" action="{{route('test.store')}}">
15 {{csrf_field()}}
16 <input type="text" name="name">
17 <input type="text" name="email">
18 <input type="submit" value="更新">
19 </form>
20</div>
21@endsection
コントローラ
php
1<?php
3namespace App\Http\Controllers;
5use Illuminate\Http\Request;
6use App\ValidationMaster;
8class TestController extends Controller
9{
10 1314
15 public function index()
16 {
17 return view('test');
18 }
21 242526
27 public function store(Request $request)
28 {
29 $validations = ValidationMaster::all();
30 $rule = [];
31 foreach ($validations as $validation) {
32 $rule[$validation->field] = $validation->rule;
33 }
34 $this->validate($request,$rule);
35 return redirect('test.index');
36 }
37}
(ルーティング)
php
1<?php
2
3/*
4|--------------------------------------------------------------------------
5| Web Routes
6|--------------------------------------------------------------------------
7|
8| Here is where you can register web routes for your application. These
9| routes are loaded by the RouteServiceProvider within a group which
10| contains the "web" middleware group. Now create something great!
11|
12*/
13
14Route::get('/', function () {
15 return view('welcome');
16});
17Route::resource('test', 'TestController');
18Auth::routes();
実行結果

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/08/03 01:19
2018/08/04 22:36