Creating default object from empty value error on Laravel 5.2 - ajax

I am building an application using Laravel 5.2. I'm trying to create an Edit modal using jquery. However I am getting a 500 internal server error, every time I try to update a record in the database. On further investigation using Firebug I get the error:
"Creating default object from empty value"...
These are the relevant code blocks.
Route.php - The edit route is what I'm trying to access from my modal
<?php
Route::group(['middleware' => ['web']], function () {
Route::get('/', function () {
return view('welcome');
})->name('home');
Route::post('/signup', [
'uses' => 'UserController#postSignup',
'as' => 'signup']);
Route::post('/signin', [
'uses' => 'UserController#postSignin',
'as' => 'signin']);
Route::get('/logout', [
'uses' => 'UserController#getLogout',
'as' => 'logout']);
Route::get('/dashboard', [
'uses' => 'PostController#getDashboard',
'as' => 'dashboard',
'middleware' => 'auth'
]);
Route::post('/createpost', [
'uses' => 'PostController#CreatePost',
'as' => 'createpost',
'middleware' => 'auth']);
Route::get('/delete-post/{post_id}', [
'uses' => 'PostController#getDeletePost',
'as' => 'post.delete',
'middleware' => 'auth']);
Route::post('/edit', [
'uses' => 'PostController#getEditPost',
'as' => 'edit'
]);
});
PostController.php
public function getEditPost(Request $request)
{
$this->validate($request, [
'body' => 'required'
]);
$post = Post::find($request['postid']);
$post->body = $request['body'];
$post->update();
return response()->json(['new_body' => $post->body], 200);
}
The Javascript file with the click event, app.js. I am printing a message to the console upon successful update of the database
var postId = 0;
$('.post').find('.interaction').find('.edit').on('click', function (event) {
event.preventDefault();
var postBody = event.target.parentNode.parentNode.childNodes[1].textContent;
postId = event.target.parentNode.dataset['postid'];
$('#post-body').val(postBody);
$('#edit-modal').modal();
});
$('#modal-save').on('click', function () {
$.ajax({
method: 'POST',
url: url,
data: {body: $('#post-body').val(), postId: postId, _token: token}
}).done(function (msg) {
console.log(JSON.stringify(msg));
});
});
This is my view page
dashboard.blade.php
#extends('layouts.master')
#section('content')
#include('includes.message-block')
<section class="row new-post">
<div class="col-md-6 col-md-offset-3">
<header><h3>What do you have to say?</h3></header>
<form action="{{ route('createpost') }}" method="post">
<div class="form-group">
<textarea class="form-control" name="body" id="new-post" rows="5" placeholder="Your Post"></textarea>
</div>
<button type="submit" class="btn btn-primary">Create Post</button>
<input type="hidden" value="{{ Session::token() }}" name="_token">
</form>
</div>
</section>
<section class="row posts">
<div class="col-md-6 col-md-offset-3">
<header><h3>What other people say...</h3></header>
#foreach($posts as $post)
<article class="post" data-postid="{{ $post->id }}">
<p>{{ $post->body }}</p>
<div class="info">
Posted by {{ $post->user->first_name }} on {{ $post->created_at }}
</div>
<div class="interaction">
Like |
Dislike
#if(Auth::user() == $post->user)
|
Edit |
Delete
#endif
</div>
</article>
#endforeach
</div>
</section>
<div class="modal fade" tabindex="-1" role="dialog" id="edit-modal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Edit Post</h4>
</div>
<div class="modal-body">
<form>
<div class="form-group">
<label for="post-body">Edit the Post</label>
<textarea class="form-control" name="post-body" id="post-body" rows="5"></textarea>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" id="modal-save">Save changes</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<script>
var token = '{{ Session::token() }}';
var url = '{{ route('edit') }}';
</script>
#endsection
Please can anyone help me to see what I'm missing here? Thanks

It looks like you get null on $post = Post::find($request['postid']);.
Do some checks before trying to update the model.
You can use ::findOrFail() or check if !is_null($post).
Also you should use $request->input('postid').

Related

Keep modal open if any validation error exists laravel

I am trying to upload a csv and store the data accordingly in database. Form is in the modal. I want the modal to open if there exists any validation error message.
Here is the modal and ajax query:
$('#formSubmit').click(function(e) {
e.preventDefault();
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('input[name="_token"]').val()
}
});
$.ajax({
url: "{{ url('/resellers') }}",
type: 'POST',
contentType: false,
processData: false,
data: {
csv_file: $('#csv_file').val(),
},
success: function(result) {
if (result.errors) {
$('.alert-danger').html('');
$.each(result.errors, function(key, value) {
$('.alert-danger').show();
$('.alert-danger').append('<li>' + value + '</li>');
});
} else {
$('.alert-danger').hide();
$('#reseller_modal').modal('hide');
}
}
});
});
<div class="col-sm-6">
<button type="button" id="csv-import" class="btn btn-secondary m-2 csv-import" data- toggle="modal" data-target="#reseller_modal">Import Csv</button>
</div>
<!-- Bootstrap modal -->
<div class="modal fade" id="reseller_modal" tabindex="-1" role="dialog" aria- labelledby="reseller_modal" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="demoModalLabel">CSV import</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="alert alert-danger" style="display:none"></div>
<div class="modal-body">
<div class="row">
<form class="form-horizontal" method="POST" action="{{ route('processImport') }}" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="form-group">
<input id="csv_file" type="file" class="form-control" name="csv_file" required>
#if ($errors->has('csv_file'))
<span class="help-block">
<strong>{{ $errors->first('csv_file') }}</strong>
</span>
#endif
</div>
<div class="form-check">
</div>
<button type="submit" class="btn btn-primary" id="formSubmit">Submit</button>
</div>
</form>
</div>
</div>
</div>
</div>
And here is the controller:
public function processImport(Request $request)
{
$validator = Validator::make($request->all(), [
'csv_file' => 'required|file|mimes:csv,txt'
]);
if ($validator->fails()) {
return response()->json(['errors' => $validator->errors()->all()]);
}
$rows = importCsv($request->file('csv_file'));
foreach ($rows as $data) {
$validator = Validator::make($data, [
'name' => ['required', 'string'],
'email' => ['required', 'string', 'email'],
'password' => ['required', 'string'],
]);
$status = User::where('email', '=', $data['email'])->exists();
if (!$validator->fails()) {
if (!$status) {
User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password'])
]);
} else {
$user = User::where('email', '=', $data['email'])->first();
$user->update([
'password' => $data['password']
]);
}
} else {
Log::channel('import_fail')->info($data['email'] . ' Couldnot be stored . Has the validation message" : ' . $validator->errors()->first());
}
}
return redirect()->route('resellers')->with('success', 'Resellers imported successfully');
Even though I have csv_field in form, I get this error The csv_file field is required. It keeps the modal open.
I guess problem is here.
data: {
csv_file: $('#csv_file').val(),
},
You are sending text value i.e. name of file instead of actual file. and validating file.
Refer this to upload file using ajax. https://makitweb.com/how-to-upload-a-file-using-jquery-ajax-in-laravel-8/

I make Comment by Ajax but its not working

I make Comment by Ajax but its not working. It can insert and I already recived the data form controller. I check alert is display but html not display.
Here is my Route: Route::post('/addComment', 'LessonController#addComment');
Here is my controller
public function addComment(Request $request)
{
$lesson_id = $request->lesson_id;
$name = $request->name;
$body= $request->body;
$comment = new Comment();
$comment->name = $name ;
$comment->body = $body;
$comment->lesson_id = $lesson_id;
$comment->save();
return response()->json($comment);
}
Here is view comment create
<div class="leave_review">
<h3 class="blog_heading_border"> Leave a Comment </h3>
{!! Form::open(['action' => 'LessonController#addComment', 'method' => 'POST', 'id' => 'comment' ]) !!}
<input type="hidden" name="_token" value="{{ csrf_token() }}" />
<input type="hidden" id="lesson_id" name="lesson_id" value="{{$lesson->id}}" />
</div>
<div class="row">
<div class="col-sm-6">
{{Form::label('name','Name')}}
{{Form::text('name', '', ['class' => 'form-group', 'id'=>'name']) }}
</div>
<div class="col-sm-12">
{{Form::label('body','Message')}}
{{Form::textarea('body', '', ['class' => 'form-group', 'id'=>'body']) }}
</div>
</div>
<div class="row">
<div class="col-md-12">
</div>
</div>
{{Form::submit('Submit', ['name'=>'submit', 'class' => 'mt_btn_yellow pull-right'])}}
{!! Form::close() !!}
<br>
Here is comment display view blade
<div id="commen_show">
#foreach ($comment as $item)
<ol class="review-lists">
<li class="comment">
<div class="activity_rounded">
<img src="/storage/Iconimage/about.png" alt="image"> </div>
<div class="comment-body">
<h4 class="text-left">{{$item->name}}
<small class="date-posted pull-right">{{$item->created_at}}</small>
</h4>
<p>{{$item->body}}</p>
Reply
<div class="clearfix"></div>
</div>
</li>
</ol>
#endforeach
</div>
</div>
Here is my javascript, I think something wrong with append
<script>
$(document).ready(function($){
$('#comment').on('submit', function(){
var lesson_id = $('#lesson_id').val();
var name = $('#name').val();
var body = $('#body').val();
var _token = $('input[name="_token"]').val();
$.ajax({
type : 'POST', // define the type of HTTP verb we want to use (POST for our form)
url : "{{url('/addComment')}}", // the url where we want to POST
data: {_token:_token, lesson_id:lesson_id, body:body, name:name},
success: function(response) {
if(response){
$('#commnet_show').prepend(
` <ol class="review-lists">
<li class="comment">
<div class="activity_rounded">
<img src="/storage/Iconimage/about.png" alt="image"> </div>
<div class="comment-body">
<h4 class="text-left">${response.name}
<small class="date-posted pull-right">${response.created_at}</small>
</h4>
<p>${response.body}</p>
Reply
<div class="clearfix"></div>
</div>
</li>
</ol> `
;);
}
}
});
});
});
</script>

Ajax not displaying error message itself, box only

For some reason AJAX is not passing the error message. Whatever I try to submit in my form, I get red line (error message style) but without error message init, which makes hard for me to debug. Any ideas what is causing error message not to be displayed?
My AJAX function, pointing to store method in TicketCategory controller:
$(document).ready(function() {
$("#btn-add").click(function() {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
type: 'POST',
url: '/ticket-category/store',
data: {
name: $("#frmAddTicketCategory input[name=name]").val(),
},
dataType: 'json',
$('#frmAddTicketCategory').trigger("reset");
$("#frmAddTicketCategory .close").click();
window.location.reload();
},
error: function(data) {
var errors = $.parseJSON(data.responseText);
$('#add-ticket-category-errors').html('');
$.each(errors.messages, function(key, value) {
$('#add-ticket-category-errors').append('<li>' + value + '</li>');
});
$("#add-error-bag").show();
}
});
});
My view:
<div class="modal fade" id="addTicketCategoryModal">
<div class="modal-dialog">
<div class="modal-content">
<form id="frmAddTicketCategory">
<div class="modal-header">
<h4 class="modal-title">
Add New Task
</h4>
<button aria-hidden="true" class="close" data-dismiss="modal" type="button">
×
</button>
</div>
<div class="modal-body">
<div class="alert alert-danger" id="add-error-bag">
<ul id="add-ticket-category-errors">
</ul>
</div>
<div class="form-group">
<label>
Name
</label>
<input class="form-control" id="name" name="name" type="text">
</input>
</div>
</div>
<div class="modal-footer">
<input class="btn btn-default" data-dismiss="modal" type="button" value="Cancel">
<button class="btn btn-info" id="btn-add" type="button" value="add">
Add New Task
</button>
</input>
</div>
</form>
</div>
</div>
</div>
Controller:
public function create()
{
return view('ticket_category/ticket_cat',[
'tickets' => TicketCategory::all()
]);
}
public function store(Request $request)
{
$validator = Validator::make($request->input(), array(
'name' => 'required'
));
if ($validator->fails()) {
return response()->json([
'error' => true,
'messages' => $validator->errors(),
], 422);
}
$ticket_category = TicketCategory::create([$request->name]);
return response()->json([
'error' => false,
'ticket_category' => $ticket_category,
], 200);
}
see the error issue
I think value is an array of errors for a field that is why you are not able to display the errors. Try this
$('#add-ticket-category-errors').append('<li>' + value[0] + '</li>')

Ajax post request- Unresolvable dependency resolving and error 500

I'm trying to create a multi-step form using jQuery anad AJAX. But Im getting this error when "go to step 2" is clicked:
jquery.min.js:4 POST http://store.test/product/1/product-test/payment/storeUserInfo 500 (Internal Server Error)
Also when clicking in network tab and then click in "storeUserInfo" it appears:
exception
:
"Illuminate\Contracts\Container\BindingResolutionException"
file
:
"/Users/jakeB/projects/store/vendor/laravel/framework/src/Illuminate/Container/Container.php"
line
:
933
message
:
"Unresolvable dependency resolving [Parameter #1 [ <required> array $data ]] in class Illuminate\Validation\Validator"
Do you know where is the error?
In the PaymentController there is the storeUserInfo() Method that is the method for the step 1:
public function storeUserInfo(Request $request, $id, $slug = null, Validator $validator){
$validator = Validator::make($request->all(),[
'buyer_name' => 'required|max:255|string',
'buyer_surname' => 'required|max:255|string',
'buyer_email' => 'required|max:255|string',
'name_invoice' => 'required|max:255|string',
'country_invoice' => 'required|max:255|string',
]);
if ($validator->fails()) {
if($request->ajax())
{
return response('test');
}
$this->throwValidationException(
$request, $validator
);
}
}
Route:
Route::post('/product/{id}/{slug?}/payment/storeUserInfo', [
'uses' => 'PaymentController#storeUserInfo',
'as' =>'products.storeUserInfo'
]);
// step 1 and step 2 html
<div class="tab-pane fade show active clearfix" id="step1" role="tabpanel" aria-labelledby="home-tab">
<h6>User Info</h6>
<form method="post" id="step1form" action="">
{{csrf_field()}}
<div class="form-group font-size-sm">
<label for="name" class="text-gray">Name</label>
<input name="name" type="text" required class="form-control" value="{{ (\Auth::check()) ? Auth::user()->name : old('name')}}">
</div>
<!-- other form fields -->
<input type="submit" id="goToStep2" href="#step2"
class="btn btn-primary btn float-right next-step" value="Go to step 2"/>
</form>
</div>
<div class="tab-pane fade clearfix tabs hide" id="step2" role="tabpanel" aria-labelledby="profile-tab">
<form method="post">
<h6>Payment method</h6>
<div class="form-group">
<div class="form-check">
<input class="form-check-input" type="radio" name="paymentmethod1" value="option1" checked>
<label class="form-check-label d-flex align-items-center" for="exampleRadios1">
<span class="mr-auto">payment method 1</span>
</label>
</div>
<br>
<div class="form-check">
<input class="form-check-input" type="radio" name="credit_card" value="option1">
<label class="form-check-label d-flex align-items-center" for="exampleRadios1">
<span class="mr-auto">Stripe</span>
</label>
</div>
</div>
<div class="text-right">
<button type="button" href="#step3" data-toggle="tab" role="tab"
class="btn btn-outline-primary prev-step">
Go back to step 2
</button>
<button type="button" data-nexttab="#step3" href="#step3"
class="btn btn-primary btn ml-2 next-step">
Go to step 3
</button>
</div>
</form>
</div>
// ajax in payment.blade.php
$('#goToStep2').on('click', function (event) {
event.preventDefault();
var custom_form = $("#" + page_form_id);
$.ajax({
method: "POST",
url: '{{ route('products.storeUserInfo',compact('id','slug') ) }}',
data: custom_form.serialize(),
datatype: 'json',
success: function (data, textStatus, jqXHR) {
setTimeout(function () {
}, 3000);
},
error: function (data) {
console.log(data);
var errors = data.responseJSON;
var errorsHtml = '';
$.each(errors['errors'], function (index, value) {
errorsHtml += '<ul class="list-group"><li class="list-group-item alert alert-danger">' + value + '</li></ul>';
});
$('#response').show().html(errorsHtml);
}
});
});
});
Use Validator facade class in your PaymentController
use Validator;
class PaymentController extends Controller
{
public function storeUserInfo(Request $request, $id, $slug = null){
$validator = Validator::make($request->all(),[
'buyer_name' => 'required|max:255|string',
'buyer_surname' => 'required|max:255|string',
'buyer_email' => 'required|max:255|string',
'name_invoice' => 'required|max:255|string',
'country_invoice' => 'required|max:255|string',
]);
if ($validator->fails()) {
if($request->ajax())
{
return response('test');
}
$this->throwValidationException(
$request, $validator
);
}
}
}

Laravel Error: MethodNotAllowedHttpException in RouteCollection.php

Having creating a code for Excel upload I am getting the below mentioned error...
MethodNotAllowedHttpException in RouteCollection.php
The codes written in VIEW is
views/items/items
#extends('layouts.master')
#section('content')
<div class="row">
<div class="col-md-4"></div>
<div class="col-md-6">
<div class="row">
<form action="{{route('items.import')}}" method="POST" enctype="multipart/form-data">
<div class="col-md-6">
{{csrf_field()}}
<input type="file" name="imported-file"/>
</div>
<div class="col-md-6">
<button class="btn btn-primary" type="submit">Import</button>
</div>
</form>
</div>
</div>
<div class="col-md-2">
<!-- <button class="btn btn-success">Export</button> -->
</div>
</div>
#endsection
The codes written in route.php is...
Route::get('/items', 'ItemController#index');
Route::post('/items/import',[ 'as' => 'items.import', 'uses' => 'ItemController#import']);
ItemController.ASPX
public function index()
{
return view('items.items');
}
/**
* Show the form for creating a new resource.
*
* #return Response
*/
public function import(Request $request)
{
if($request->file('imported-file'))
{
$path = $request->file('imported-file')->getRealPath();
$data = Excel::load($path, function($reader) {
})->get();
if(!empty($data) && $data->count())
{
$data = $data->toArray();
for($i=0;$i<count($data);$i++)
{
$dataImported[] = $data[$i];
}
}
Inventory::insert($dataImported);
}
return back();
}
Can anyone please help me what am missing in my coding that outputs the error...
Try this code instead of yours:
Route::post('/items/import',[ 'as' => 'items/import', 'uses' => 'ItemController#import']);
The trick is - the route needs to be named.
To avoid future confusion, it's better to name it as "items.import", so later you can identify for yourself that this is a "name" of a route.
So the final code would be:
Route::post('/items/import',[ 'as' => 'items.import', 'uses' => 'ItemController#import']);
and in blade template u call it like that:
<form action="{{route('items.import')}}"...

Resources