jeudi 28 décembre 2017

Laravel Form Input not required

I'm trying to make a simple contact form for my users and IF they have an order id to put it in a field. Or else i put it by default as "0" (lets say that they have a question)

So this is my code

Controller
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\ContactUS;

class ContactUSController extends Controller
{
    public function contactUS()
    {
        return view('contactUS');
    }

    public function contactUSPost(Request $request)
    {
        $this->validate($request, [
            'name' => 'required',
            'email' => 'required|email',
            'message' => 'required'
        ]);

        ContactUS::create($request->all());

        return back()->with('success', 'Thanks for contacting us! We will get back shortly.');
    }
}



View

    <div class="container">
        <h1>Contact US Form</h1>

        @if(Session::has('success'))
            <div class="alert alert-success">
                
            </div>
        @endif

        {!! Form::open(['route'=>'contactus.store']) !!}

        <div class="form-group ">
            {!! Form::label('Name:') !!}
            {!! Form::text('name', old('name'), ['class'=>'form-control', 'placeholder'=>'Enter Name']) !!}
            <span class="text-danger"></span>
        </div>

        <div class="form-group ">
            {!! Form::label('Email:') !!}
            {!! Form::text('email', old('email'), ['class'=>'form-control', 'placeholder'=>'Enter Email']) !!}
            <span class="text-danger"></span>
        </div>

        <div class="form-group">
            {!! Form::label('Order ID:') !!}
            {!! Form::number('orderId', old('orderId'), ['class'=>'form-control no-spinners', 'placeholder'=>'Do you have and order id?']) !!}

        </div>

        <div class="form-group ">
            {!! Form::label('Message:') !!}
            {!! Form::textarea('message', old('message'), ['class'=>'form-control', 'placeholder'=>'Enter Message']) !!}
            <span class="text-danger"></span>
        </div>

        <div class="form-group">
            <button class="btn btn-success">Contact US!</button>
        </div>

        {!! Form::close() !!}

Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class ContactUS extends Model
{
    public $table = 'contact_us';

    public $fillable = ['name','email','message'];
}

Migration

class CreateContactUsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('contact_us', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email');
            $table->text('message');
            $table->integer('orderId')->default('0');
            $table->boolean('solved')->default(false);
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('contact_us');
    }
}

So the issue is that when I don't put any value on field OrderId it takes the default value 0 But if i put any number value 1 or 2 etc it still saves 0 at my database.

Any idea why is this reaction made?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire