Skip to content
Advertisement

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:

Controller

<?php
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;]);

        DB::table('books')->insert($data);
    }
}

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>
    <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>
    <div class="col-md-3">
        <label for="author" class="form-label">Author : </label>
        <input type="text" class="form-control" name="author" id="author">
    </div>
    <div class="col-md-6">
        <label for="publishers" class="form-label">Publisher : </label>
        <input type="text" class="form-control" name="publishers" id="publishers" >
    </div>
    <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>
    <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>
    <div class="col-12">
        <label for="Summary" class="form-label">Summary :</label>
        <textarea class="form-control" name= "Summary" id="Summary"></textarea>
        <br><br>
    </div>
    <div class="col-12">
        <button type="submit" class="btn btn-primary">Add Book</button>
    </div>
</form>

Here’s the route

Route::post('/addbook', 'BooksController@addBooks')->name('addbook');

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

'name' => $request->bookName

but that also gives an error.

Advertisement

Answer

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
];

DB::table('books')->insert($data);
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement