I am trying to create a laravel project with a simple CRUD methods First controller was created successfully with a bootstrap modal and ajax
Then I tried to do a second one using laravel functions create and store, but on submit I get 500 internal server error 1- I am new to laravel, I can't see the log and i can't tell what is the error 2- I used the csrf token
web.php
Route::get('/items', 'ItemController@index')->name('items');
Route::get('/items/create', 'ItemController@create')->name('itemscreate');
Route::POST('/items/strore', 'ItemController@store')->name('itemsstore');
ItemController.php
public function create()
{
$customers= \App\Customer::all();
return view("items.create")->with('customers', $customers);
}
public function store(Request $req)
{
$item = new \App\Item;
$item->name = $req->name;
$item->details = $req->details;
$item->customer_id = $req->customer_id;
$item->save();
return redirect()->route('items');
}
create.blade.php
@extends('layouts.app')
@section('content')
<link href="" rel="stylesheet">
<script type="text/javascript">
$(document).ready(function() {
$('#addworkshop').click(function(){
$.ajax({
type: 'POST',
url: 'newworkshop',
data: {
'_token': $('input[name=_token]').val(),
'name': $('input[name=txtName]').val(),
'details': $('textarea#txtDetails').val(),
'total_amount': $('input[name=txtAmount]').val(),
'customer_id': $('#customer').val(),
},
success: function (data){
// toastr.success('Workshop added successfully');
// setTimeout(function(){
// location.reload();
// }, 1000);
}
});
});
});
</script>
<div class="col-md-12">
<form class="form-horizontal" role="form">
<input type="hidden" name="_token" value="">
<div class="col-md-12">
<h1 class="control-label" style="text-align: center">New workshop</h1>
<div class="col-md-12">
<hr>
</div>
</div>
<div class="col-md-12 content">
<div class="container">
<div class="col-md-10">
<fieldset>
<div class="form-group">
<label class="control-label col-sm-3" for="name">Name</label>
<div class="col-sm-9">
<input type="text" class="form-control" name="txtName" placeholder="Workshop name" required>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="details">Details</label>
<div class="col-sm-9">
<textarea id='txtDetails' class="form-control" placeholder="Workshop details" rows="5"></textarea>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="customer">Customer</label>
<div class="col-sm-7">
<select class="form-control" id='customer' required>
<option value="">Choose an existing customer</option>
@foreach ($customers as $customer)
<option value=""></option>
@endforeach
</select>
</div>
<div class="col-sm-1">
<button class="btn btn-primary">Add customer</button>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="amount">Total amount</label>
<div class="col-sm-9">
<input type="text" class="form-control" name="txtAmount" placeholder="Amount" required>
</div>
</div>
</fieldset>
<button id='addworkshop' class="btn btn-primary pull-right">Save</button>
</div>
</div>
</div>
</form>
</div>
@endsection
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire