Laravel 5.2创建帖子不起作用

时间:2022-10-18 17:38:36

Why is this page returning in JSON?

为什么此页面以JSON格式返回?

JSON : Laravel 5.2创建帖子不起作用

JSON:

Fairly new to laravel and my app is working great except for one thing,

laravel相当新,我的应用程序工作得很好,除了一件事,

When I go to create a new post it is failing to save to the database and returns a request JSON page.

当我去创建一个新帖子时,它无法保存到数据库并返回请求JSON页面。

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
 */

2 个解决方案

#1


1  

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.

如上所述,dd($ request)返回请求的JSON值,您应该只在第一次使用它来查看表单中是否传递了所有值,然后您应该省略它,仅用于测试目的。

#2


1  

The first thing your store() method is doing is: dd($request).

你的store()方法做的第一件事是: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.

dd()代表“dump and die”,它会转储传递给它的任何内容,然后调用die()。您看到的输出是$ request变量的转储,然后脚本死掉。

#1


1  

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.

如上所述,dd($ request)返回请求的JSON值,您应该只在第一次使用它来查看表单中是否传递了所有值,然后您应该省略它,仅用于测试目的。

#2


1  

The first thing your store() method is doing is: dd($request).

你的store()方法做的第一件事是: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.

dd()代表“dump and die”,它会转储传递给它的任何内容,然后调用die()。您看到的输出是$ request变量的转储,然后脚本死掉。