1

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

2 Answers 2

1

if you want to make a new entry in database use create method

  • make sure you've imported the model at the top of the controller page
  • rename the model class name to Blog instead of blog
  • change the model file name accordingly to Blog.php

then use:

use App\Blog;

public function postCreate(Request $request)
{
    Blog::create(['title'=>$request->input('title'), ..]);

    return redirect()->route('admin.blog')->with('info', 'Post created, Title is: ' . $request->input('title'));
}
Sign up to request clarification or add additional context in comments.

Comments

0

my bad, adding ajax was over kill and not required, as laravel Request was doing the that i require, I adjust the code and everything is working now.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.