I can make a post fine, but when i add ajax, it doesn't add to the database for posts. its not responding to it, and i don't know why. ultimately i just want to add a post without refresh.
no errors show on the console
i get this
XHR finished loading: POST "http://localhost:8000/createpost".
but once again it does not add to my database for posts.
i referenced this, but i still don't see any changes.
Any suggestions.
Dashboard.blade.php
@extends('layouts.layout')
@section('title')
Dashboard
@endsection
@section('content')
<div class="dashboard eli-main">
<div class="container ">
<div class="row">
<div class="col-md-6 col-md-12">
<h1></h1>
<h4>What do you have to say?</h4>
<form action="" method="post">
<div class="form-group">
<textarea class="form-control" name="body" id="new-post" rows="5" placeholder="Your Post"></textarea>
</div>
<button type="button" id="add" class="mybtn2">Create Post</button>
<input type="hidden" value="" name="_token">
</form>
</div>
</div>
</div>
</div>
@endsection
App.js
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$("#add").click(function(e) {
e.preventDefault();
$.ajax({
type: 'post',
url: '/createpost',
data: {
'_token': $('input[name=_token]').val(),
'name': $('textarea[name=body]').val()
},
success: function(data) {
if ((data.errors)) {
$('.error').removeClass('hidden');
$('.error').text(data.errors.name);
} else {
$('.error').remove();
$('#table').append("<tr class='item" + data.id + "'><td>" + data.id + "</td><td>" + data.name + "</td><td><button class='edit-modal btn btn-info' data-id='" + data.id + "' data-name='" + data.name + "'><span class='glyphicon glyphicon-edit'></span> Edit</button> <button class='delete-modal btn btn-danger' data-id='" + data.id + "' data-name='" + data.name + "'><span class='glyphicon glyphicon-trash'></span> Delete</button></td></tr>");
}
},
});
$('#new-post').val('');
});
PostController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
use App\User;
use Illuminate\Support\Facades\Auth;
use Validator;
use Response;
use Illuminate\Support\Facades\Input;
class PostController extends Controller
{
public function getDashboard()
{
$posts = Post::orderBy('created_at', 'desc')->get();
$cookie = cookie('saw-dashboard', true, 15);
$users = User::all();
$user = new User();
// return view('dashboard', array('user'=> Auth::user()), compact('users'))->withCookie($cookie);
return view('dashboard',array('user'=> Auth::user(), 'posts' => $posts, compact('users')))->withCookie($cookie);
}
// public function postCreatePost(Request $request)
// {
// $this->validate($request,[
// 'body' => 'required|max:1000'
// ]);
// $post = new Post();
// $post->body = $request['body'];
// $message = 'There was an error';
// if($request->user()->posts()->save($post)){
// $message = 'Post Successfully Created';
// }
// return response ()->json( $post );
// }
public function postCreatePost(Request $request) {
$rules = array(
'body' => 'required|max:1000'
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
return Response::json (array(
'errors' => $validator->getMessageBag()->toArray ()
));
} else {
$post = new Post();
$post->body = $request->body;
$post->save();
return response ()->json( $post );
}
}
}
And here is the route.
Route::post('/createpost',[
'uses'=>'PostController@postCreatePost',
'as' => 'createpost',
'middleware' => 'auth'
]);
via Owl Man