我正在尝试创建新的使用后laravel,ajax和s3,但每次我尝试提交表单我得到状态代码:302 Found,我希望真的有些帮助我
萤火
META
视图
带有csrf标记的表单视图
{!! Form::open(array(
'class' => 'form',
'novalidate' => 'novalidate',
'files' => true
)) !!}
{!! Form::label('title', 'Title: ') !!}
{!! Form::text('title', null, ['class' => 'form-control']) !!}
Select Category list :
Select Category
@foreach($category as $cat)
{{$cat->name}}
@endforeach
Select Subcategory list :
Select Subcategory
{!! Form::label('image', 'Upload Image') !!}
{!! Form::file('image', null, ['class' => 'form-control']) !!}
{!! Form::label('description', 'Description: ') !!}
{!! Form::textarea('description', null, ['class' => 'form-control']) !!}
{!! Form::label('email', 'Your Email: ') !!}
{!! Form::text('email', null, ['class' => 'form-control']) !!}
{!! Form::submit('Post Free Ad', ['class' => 'btn btn-primary form-control']) !!}
{!! Form::close() !!}
CONTROLLER
首先确认请求,然后为用户创建新的电子邮件,如果他没有,而不是保存用户的帖子
public function storePostAds(Request $request)
{
$this->validate($request, [
'title' => 'required',
'description' => 'required',
'image' => 'required',
'category_id' => 'required',
'subcategory_id' => 'required',
]);
$email = $request['email'];
$title = $request['title'];
$description = $request['description'];
$category = $request['category_id'];
$subcategory = $request['subcategory_id'];
$image = $request->file('image');
$user = User::where('email', $email)->first();
if(!$user){
$user = new User();
$user->email = $email;
$user->save();
}
if($image->isValid()){
$name = $image->getClientOriginalName();
$key = 'images/'.$name;
Storage::disk('s3')->put($key, file_get_contents($image));
}
$post = new Post();
$post->title = $title;
$post->description = $description;
$post->category_id = $category;
$post->subcategory_id = $subcategory;
$post->image = $image;
$user->posts()->save($post);
return redirect('/');
}
阿贾克斯
选择后ajax获取子类别foreach类别
(function($){
$('#category').on('change', function(e){
var cat_id = e.target.value;
$.get('/ajax-subcategory?cat_id=' + cat_id, function(data){
var subcategory = $('#subcategory');
subcategory.empty();
$.each(data, function(index, subcatObj){
subcategory.append(''+subcatObj.name+'');
});
});
});
}(jQuery));