0

I’m studying Laravel 6 and there is always a problem with the blade template.

The templates do not work: @csrf, @error ("name") $message @enderror, @method ("patch"), although constructs like @foreach, @forelse and @if work without problems.

Instead, I use {{csrf_field ()}}, @foreach ($ errors-> all () as error) {$ error} @endforeach respectively.

If @error("name") $message @enderror throws an error: Undefined variable: message, then @csrf and @method ("patch") just output in HTML as plain text. Now there is a problem with @method ("patch"). I work on windows 7, open server I write everything according to the documentation, but I have to look for another way to write code. What could be the problem?

Here's the situation with @error ("name") {$ message}. Here is the controller.

<?php

namespace App \ Http \ Controllers;

use App \ Http \ Controllers \ Controller;
use Illuminate \ Http \ Request;
use Illuminate \ Support \ Facades \ DB;

class ServiceController extends Controller
{
    public function index () {
        $ var = 'create';

        return view ('services.index', ['data' => $ var,]);
    }

    public function store (Request $ request) {
        $ data = $ this-> validate ($ request, [
            'name' => 'required'
        ]);
        $ var = request ('name');
        DB :: table ('services') -> insert (['name' => $ var]);
        return redirect () -> back ();
    }
}

Here is the template in services / index.blade.php. All templates are named correctly.

@extends ('html')

@section ('title', 'create')
@section ('content')

    <h1> Create service </h1>
    <form action = '/ service' method = 'post'>
        <input type = 'text' name = 'name'>
        {{csrf_field ()}}
        <button> Add service </button>
    </form>
    @error ('name') {{$ message}} @enderror

@endsection
Thân LƯƠNG Đình
  • 3,082
  • 2
  • 11
  • 21
Aleks
  • 1
  • 2
  • 1
    First of all check the version constant `vendor/laravel/framework/src/Illuminate/Foundation/Application.php`. If that one is correct then check the `vendor/laravel/framework/src/Illuminate/View/Compilers/BladeCompiler.php` to make use it includes `use Concerns/CompilesErrors` (or similar). If all these seem to be as you expect them try deleting `vendor` and running `composer install` again – apokryfos Feb 07 '20 at 18:05

1 Answers1

0

    namespace App\Http\Controllers;
    use App\Http\Controllers\Controller;
    use Illuminate\Http\Request;
    use Illuminate\Support\Facades\DB;
    class ServiceController extends Controller
    {
        public function index(){
            $var = 'create';
            return view ('services.index', ['data' => $var]);
        }
        public function store(Request $request) {       
            $datavalidation = $request->validate([
                'name' => 'required'
            ]);
            $var =$request->name;
            DB::table('services')->insert(['name' => $var]);
            return redirect()->back();
        }
    }

    view blade page 

    @extends ('html')

    @section ('title', 'create')
    @section ('content')

        

Create service

@csrf {{ $errors->first('name') }} Add service /*@error ('name') {{$ message}} @enderror*/ @endsection
  • 1
    Hello, welcome to stack overflow. Code-only answers are often not helpful. Try to explain what OP is doing wrong and how your solution solves their question. – R10t-- Feb 07 '20 at 17:57