How to Add data to Database in Laravels

I will to add data to database, but I have an error when I write the code to add data to array, and the error like this,

syntax error, unexpected ‘=’, expecting ‘]’

I am using the follwing code:


namespace AppHttpControllers;

use DateTime;
use IlluminateHttpRequest;
use IlluminateSupportFacadesDB;

class BooksController extends Controller
    public function addBooks(Request $request)
        $data = array([
            'name' = $request->input('bookName');
        'year' = $request->input('tahunT');
        'author' = $request->input('author');
        'summary' = $request->input('Summary');
        'publisher' = $request->input('publishers');
        'pageCount' = $request->input('pageCount');
        'readPage' = $request->input('ReadPage');
        'finished' = $pageCount == $readPage ? true : false;
        'reading' = $readPage > 0 ? true : false;
        'insertedAt' = new DateTime();
        'updatedAt' = $insertedAt;]);


Here’s the view

<form class="row g-3" action="{{ route('addbook') }}" method="post">
    <input type = "hidden" name = "_token" value = "<?php echo csrf_token(); ?>">
    <div class="col-md-6">
        <label for="bookName" class="form-label">Nama Buku:</label>
        <input type="text" class="form-control" name="bookName" id="bookName">
    <div class="col-md-3">
        <label for="tahunT" class="form-label">Tahun Terbit : </label>
        <input type="number" class="form-control" name="tahunT"  id="tahunT">
    <div class="col-md-3">
        <label for="author" class="form-label">Author : </label>
        <input type="text" class="form-control" name="author" id="author">
    <div class="col-md-6">
        <label for="publishers" class="form-label">Publisher : </label>
        <input type="text" class="form-control" name="publishers" id="publishers" >
    <div class="col-md-3">
        <label for="pageCount" class="form-label">Page Count :</label>
        <input type="number" class="form-control" name= "pageCount" id="pageCount">
    <div class="col-md-3">
        <label for="ReadPage" class="form-label">Read Page :</label>
        <input type="number" class="form-control" name="ReadPage" id="ReadPage">
    <div class="col-12">
        <label for="Summary" class="form-label">Summary :</label>
        <textarea class="form-control" name= "Summary" id="Summary"></textarea>
    <div class="col-12">
        <button type="submit" class="btn btn-primary">Add Book</button>

Here’s the route
Route::post('/addbook', '[email protected]')->name('addbook');

Do you have any solution to this? I have also tried:

'name' => $request->bookName

but that also gives an error.


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You seem to have mixed the old array syntax array() and the new syntax []. Keep in mind each entry of the array has to end with an , instead of a ;.

You could use the following:

$data = [
    'name'=> $request->input('bookName'),
    'year'=> $request->input('tahunT'),
    'author'=> $request->input('author'),
    'summary'=> $request->input('Summary'),
    'publisher'=> $request->input('publishers'),
    'pageCount' => $request->input('pageCount'),
    'readPage'=> $request->input('ReadPage'),
    'finished' => $pageCount == $readPage ? true : false,
    'reading' => $readPage > 0 ? true : false,
    'insertedAt'=> new DateTime(),
    'updatedAt'=> $insertedAt


All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x