I've just created laravel Vuejs Project using Axios for call API.
Get Api is working well but When I am going to post my data, getting Internal server error.
I am using Resourse for for controller.
I just Create Api and get data by This:
axios.get("http://localhost:8080/vuelaravel/tasks")
.then(response =>{this.tasks = response.data})
.catch(error => console.log(error.response));
vuelaravel is my project name.
Its working well. But when i am trying to post data into my database its shown 500 internal error.
Here is my controller:
<?php
namespace App\Http\Controllers;
use App\Todo;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
class TodoController extends Controller
{
public function index()
{
$tasks=Todo::orderBy('id', 'desc')->paginate(2);
return request()->json(200,$tasks);
}
public function create()
{
//
}
public function store(Request $request)
{
$todo=Todo::create($request->all());
if($todo){
$tasks=Todo::orderBy('id', 'desc')->paginate(2);
return request()->json(200,$tasks);
}
}
public function show(Todo $todo)
{
//
}
public function edit(Todo $todo)
{
//
}
public function update(Request $request, Todo $todo)
{
//
}
public function destroy(Todo $todo)
{
//
}
}
And My ADD.Vue
<template>
<div>
<div class="modal fade" id="add" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<input type="text" name="name" v-model="name" id="name" placeholder="Title Name" class="form-control" >
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" @click="addRecord">Save changes</button>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
data(){
return{
name:'',
}
},
methods:{
addRecord(){
axios.post("http://localhost:8080/vuelaravel/tasks",{
'name': this.name,
})
.then(data => console.log(data))
.catch(error => console.log(error))
}
}
}
</script>
<style scoped>
</style>
This is The Error:
app.js:285 POST http://localhost:8080/vuelaravel/tasks 500 (Internal Server Error)
at createError (app.js:699)
at settle (app.js:960)
at XMLHttpRequest.handleLoad (app.js:168)
I am using my csrf in Welcome.blade.php
<meta name="csrf-token" content="{{csrf_token()}}">
Get my data by This Api:
created(){
axios.get("http://localhost:8080/vuelaravel/tasks")
.then(response =>{this.tasks = response.data})
.catch(error => console.log(error.response));
}
Since you are using the method create in your model, please make sure you had mass assigned in your model by putting this code above.
protected $guarded = [];
please read this docs soruce