Reputation: 93
Why is this page returning in JSON?
Fairly new to laravel and my app is working great except for one thing,
When I go to create a new post it is failing to save to the database and returns a request JSON page.
Any help fixing this issue would be greatly appreciated!
I think the issue may be related to eloquent, the code I have I can't seem to find an error in.
@extends('main')
@section('title', '| Create New Post')
@section('stylesheets')
{!! Html::style('css/parsley.css') !!}
{!! Html::style('css/select2.min.css') !!}
<script src="//cdn.tinymce.com/4/tinymce.min.js"></script>
<script>
tinymce.init({
selector: 'textarea',
plugins: 'link code',
menubar: false
});
</script>
@endsection
@section('content')
<div class="row">
<div class="col-md-8 col-md-offset-2">
<h1>Create New Post</h1>
<hr>
{!! Form::open(array('route' => 'posts.store', 'data-parsley-validate' => '')) !!}
{{ Form::label('title', 'Title:') }}
{{ Form::text('title', null, array('class' => 'form-control', 'required' => '', 'maxlength' => '255')) }}
{{ Form::label('slug', 'Slug:') }}
{{ Form::text('slug', null, array('class' => 'form-control', 'required' => '', 'minlength' => '5', 'maxlength' => '255') ) }}
{{ Form::label('category_id', 'Category:') }}
<select class="form-control" name="category_id">
@foreach($categories as $category)
<option value='{{ $category->id }}'>{{ $category->name }}</option>
@endforeach
</select>
{{ Form::label('tags', 'Tags:') }}
<select class="form-control select2-multi" name="tags[]" multiple="multiple">
@foreach($tags as $tag)
<option value='{{ $tag->id }}'>{{ $tag->name }}</option>
@endforeach
</select>
{{ Form::label('body', "Post Body:") }}
{{ Form::textarea('body', null, array('class' => 'form-control')) }}
{{ Form::submit('Create Post', array('class' => 'btn btn-success btn-lg btn-block', 'style' => 'margin-top: 20px;')) }}
{!! Form::close() !!}
</div>
</div>
@endsection
@section('scripts')
{!! Html::script('js/parsley.min.js') !!}
{!! Html::script('js/select2.min.js') !!}
<!-- <script type="text/javascript">
$('.select2-multi').select2();
</script> -->
@endsection
the controller function for create posts is this,
public function create()
{
$categories = Category::all();
$tags = Tag::all();
return view('posts.create')->withCategories($categories)->withTags($tags);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
dd($request);
// validate the data
$this->validate($request, array(
'title' => 'required|max:255',
'slug' => 'required|alpha_dash|min:5|max:255|unique:posts,slug',
'category_id' => 'required|integer',
'body' => 'required'
));
// store in the database
$post = new Post;
$post->title = $request->title;
$post->slug = $request->slug;
$post->category_id = $request->category_id;
$post->body = Purifier::clean($request->body);
$post->save();
$post->tags()->sync($request->tags, false);
Session::flash('success', 'The blog post was successfully save!');
return redirect()->route('posts.show', $post->id);
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
Upvotes: 0
Views: 457
Reputation: 111
As mentioned Above dd($request) is returning you JSON value of request, You should use it only first time to see if all the values from the form are passed or not then you should omit it, use it for testing purpose only.
Upvotes: 1
Reputation: 62288
The first thing your store()
method is doing is: dd($request)
.
dd()
stands for "dump and die", where it dumps out whatever is passed to it, and then calls die()
. The output you're seeing is the dump of the $request
variable, and then the script dies.
Upvotes: 1