Add Contact model/view/controller

This commit is contained in:
Daniel Supernault 2019-06-05 22:31:25 -06:00
parent df772689be
commit 9de5f8bef3
No known key found for this signature in database
GPG Key ID: 0DEF1C662C9033F7
4 changed files with 128 additions and 0 deletions

13
app/Contact.php Normal file
View File

@ -0,0 +1,13 @@
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Contact extends Model
{
public function user()
{
return $this->belongsTo(User::class);
}
}

View File

@ -0,0 +1,29 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Auth;
class ContactController extends Controller
{
public function show(Request $request)
{
return view('site.contact');
}
public function store(Request $request)
{
abort_if(!Auth::check(), 403);
$this->validate($request, [
'message' => 'required|string|min:5|max:500',
'request_response' => 'string|max:3'
]);
$message = $request->input('message');
$request_response = $request->input('request_response') == 'on' ? true : false;
$user = Auth::user();
return $request->all();
}
}

View File

@ -0,0 +1,37 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateContactsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('contacts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('user_id')->unsigned()->index();
$table->boolean('response_requested')->default(false);
$table->text('message');
$table->text('response');
$table->timestamp('read_at')->nullable();
$table->timestamp('responded_at')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('contacts');
}
}

View File

@ -0,0 +1,49 @@
@extends('site.partial.template')
@section('section')
<div class="title">
<h3 class="font-weight-bold">Contact</h3>
</div>
<hr>
<section>
@auth
<form method="POST">
@csrf
<div class="form-group">
<label for="input1" class="font-weight-bold">Message</label>
<textarea class="form-control" id="input1" name="message" rows="6" placeholder=""></textarea>
<span class="form-text text-muted text-right msg-counter">0/500</span>
</div>
<div class="form-group form-check">
<input type="checkbox" class="form-check-input" id="input2" name="request_response">
<label class="form-check-label" for="input2">Request response from admins</label>
</div>
<button type="submit" class="btn btn-primary font-weight-bold py-0">Submit</button>
</form>
@else
<p class="lead">
@if(filter_var(config('instance.email'), FILTER_VALIDATE_EMAIL) == true)
You can contact the admins by sending an email to {{config('instance.email')}}.
@else
The admins have not listed any public email. Please log in to send a message.
@endif
</p>
@endauth
</section>
@endsection
@auth
@push('styles')
<meta name="csrf-token" content="{{ csrf_token() }}">
@endpush
@push('scripts')
<script type="text/javascript">
$('#input1').on('keyup change paste', function(el) {
let len = el.target.value.length;
$('.msg-counter').text(len + '/500');
});
</script>
@endpush
@endauth