DEV Community

Cover image for Laravel for Beginners : a Quick Guide - 5
Kartik Bhat
Kartik Bhat

Posted on

Laravel for Beginners : a Quick Guide - 5

Yeah, I hope you understood my previous post on creating & configuring a database in to our Laravel Application and accessing through Controller via Model; Ok

(I believe you have some knowledge on MySQL - at least what it is; else do learn it before proceeding further)

Now, let's see about accessing database table to

  • insert a data
  • read a data

in my previous article you might noticed this instruction,

$dataValues = Data::get();
Enter fullscreen mode Exit fullscreen mode

this will fetch all data present in the table 'data' from our database.

Let's create two routes now, (you know where to do it I suppose)

  1. One for inserting data
  2. One for reading it

Usually, in any kind of application request raises from User Interface(a front-end) part and that request processed in the another part (a back-end)

here in our laravel application, request raises from .blade.php files(usually) then those processed in the Controller ; a basic pinch.

Eg: to insert a data, it usually comes from user interface to save it (a form submit :)), and also to read data , request should come from user interface itself (a page load, a button click :) ) Got ?!!

to make the concept easy here I am creating some dummy data in our controller itself and then I read it.
(don't worry, again I will revisit a concept of getting data from user interface , saving it to the database and sending response back to user interface ;)

Now, Lets create two routes,

Route::get('saveData', [DataController::class,'saveData']);

Route::get('readData', [DataController::class,'readData']);

Enter fullscreen mode Exit fullscreen mode

I think you can define these, you are fit enough I suppose ;)

  • 'saveData' route calls saveData() function written under DataController Class
  • 'readData' route calls readData() function written under DataController Class

Correct ?

So, Lets write those function under DataController,

public function saveData() {
      $insertData = [
          'name' => 'Kartik',
          'age' => 27
      ];

      $insertedData = Data::create($insertData);
 }
Enter fullscreen mode Exit fullscreen mode

here I am creating an array $insertData, adding two indexes/keys 'name' and 'age', then adding values for those those indexes/keys, You observed it right :)

then I am calling Data model's object to insert data into the database, for that I am using create() method, its a laravel feature.

Notice that I am taking indexes/keys of $insertData array same as 'data' tables attributes and those should be present inside a $fillables array declared within 'Data' Model Class as well, (Open 'Data' model class and verify it once)

Doubt ? Where I am taking 'id' , 'created_at' , 'updated_at' !!!
Do I really need to add it ?
I say no, because while creating migration of the 'data' table, I have mentioned 'id' as an auto incrementing attribute so it will get a new value automatically when new data arises at the table then it gets incremented, then for 'created_at' and 'updated_at' attributes, laravel migration made them timestamp values and they receive current timestamp as there default values.

It's clear right ?

Ok, Now to insert data; open you browser hit an url with a route saveData (I hope you are already running WAMP/XAMPP server and done with php artisan serve command)

http://127.0.0.1:8000/saveData

Hurray, data got inserted to the database;
Alt Text

Try to hit same url again, it again inserts another data with same 'name' and 'age' but different 'id'... observe that 'id' got incremented.

you are seeing a blank white window !!!?

Ok, let's show some status of insert operation then,

in our saveData() method, after 'create' statement add ;

dd($insertedData);
Enter fullscreen mode Exit fullscreen mode

like,
Alt Text

** What is this dd() !!! **

simply; dump and die :)

this method stops execution there itself and displays parameter passed to it on our browser window... simple right ?

Lets try it,
Hit same url again;
I am assigning status of create() method to a variable called $insetedData and passing it as a parameter to the dd() method, so it show contents of $insetedData on our browser window.

You can see this on your browser screen,
Alt Text

open "attributes" among those data, you can observe data inserted by you along with other attributes.

Alt Text

Bit Easy !!! correct or not ?

Similarly, read data too an easy concept, let's see about it;

there are some data present in the table 'data', that you have added using 'route' call, now we can list out them all;

to do that; you already came across this statement !!!

Yes, that is nothing but,

$fetchedData = Data::get();
Enter fullscreen mode Exit fullscreen mode

to view this fetched data, just do again that dd(); :)

pass $fetched data array to dd();

Now, hit 'readData' route from your browser,

http://127.0.0.1:8000/readData

you can see this result on your browser window,
Alt Text

you are seeing 3 records present in our 'data' table, correct ?
this is too a simple one right ?

Ok, Hope you understood saving and reading data from database and getting its status on our browser..

Let's enter into the User Interface part in my next article,

Bye :)

Top comments (0)