Trying to make a multi-input form, including json, text and image .. each in a specific column in the database, and update it in case of edit.
Categories is json column. Image is image column. rest is text.
Retrieving part is working well as it is pure Laravel, but i cannot send anything to database, whether update or create.
I'm stuck in the Json, so I didn't start the image uploading yet.
Json is generated in chrome console and is working fine, however I'm getting error 500 and the console log is giving me undefined for my variable Categories.
not sure why it is not reflecting in Laravel database, below are my Ajax, routes and Controller
jQuery
$(document).ready(function(){
var myUrl;
var Categories;
var type;
//create new task / update existing task
$("#reset").click(function (e) {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
})
e.preventDefault();
var myUrl = window.location.href;
var Categories = JSON.stringify($('#form-fields-tags').val());
var type = "POST";
console.log(Categories);
$.ajax({
type: type,
url: myUrl,
data: Categories,
dataType: 'json',
success: function (data) {
console.log(Categories);
},
error: function (data) {
var errors = $.parseJSON(data.responseText);
$.each(errors, function (key, value) {
$('#' + key).parent().addClass('error');
});
}
});
});
});
Controller
public function postCreate(Request $request)
{
$post = new blog([
'title'=>$request->input('title'),
'Smaller'=>$request->input('Smaller'),
'Categories'=>$request->input('Categories'),
'BodyS'=>$request->input('BodyS'),
'Status'=>$request->input('Status')
]);
$post->save();
return redirect()->route('admin.blog')->with('info', 'Post created, Title is: ' . $request->input('title'));
}
public function postUpdate(Request $request)
{
$post = blog::find($request->input('Bid'));
$post->title = $request->input('title');
$post->Smaller = $request->input('Smaller');
$Cat = json_decode($post->Categories);
$Cat = $request->input('Categories');
$post->BodyS = $request->input('BodyS');
$post->Status = $request->input('Status');
$post->save();
return redirect()->route('admin.blog')->with('info', 'Post edited, new Title is: ' . $request->input('title'));
}
Blade Blog.edit/update view
<div class="form-group">
<label class="col-sm-3 control-label no-padding-right" for="form-field-tags">Categories</label>
<div class="container-fluid">
<div class="inline">
<input type="text" name="Categories" id="form-field-tags" value="{{ $post->Categories }}" placeholder="Enter tags ..." name="Categories" />
</div>
</div>
</div>
Blade Create view
<div class="container-fluid">
<div class="inline">
<label>
<input type="text" name="Categories" id="form-field-tags"value="" placeholder="Enter tags ..." name="Categories" />
</label>
</div>
</div>
Routes
Route::group(['prefix'=> 'blog'], function(){
Route::get('/', [
'uses'=>'BlogController@getBlogIndex',
'as'=>'admin.blog'
]);
Route::get('/create',[
'uses'=>'BlogController@getPostCreate',
'as'=>'admin.createB'
]);
Route::get('/edit/{Bid}',[
'uses'=>'BlogController@getPostEdit',
'as'=>'admin.editB'
]);
Route::post('/create', [
'uses'=>'BlogController@postCreate',
'as'=>'admin.createB'
]);
Route::post('/edit', [
'uses'=>'BlogController@postUpdate',
'as'=>'admin.updateB'
]);
});
Thanks