Archive

April 2018

Browsing

Today, We are going to learn Laravel Migration add new column to an existing table.  In the last Laravel Migration tutorial, We learn How to create the database table using Laravel Migrations. In this tutorial, We are going to learn How to add a new column to an existing table. Many times we need an extra column after the table has been created using Laravel migrations. Let’s see how we can manage migration files for adding the extra column to the database table.

Today, We are going to add the column for an image on the blog table. First, follow this tutorial for creating the blog table. After, creating the blog table let’s see how we can add the new column to existing blog table.

Laravel Migration add new column

First of all, We are going to create the migration file to add a new column to existing database table. Let’s follow the command for creating the migration.

Finally, We are ready with the new migration file. The new migration will generate into database/migrations directory. Let’s open the migration file and write the code to add an image column to an existing database table.

Finally, We are ready with the migration file. Now, let’s run the migration by following the command.

Finally, We add the new image column to a blog table. So, it’s too easy to add an extra column to an existing table using laravel migrations. Still, If you face any issue write the comment.

Today, We are going to learn Laravel Migrations to creating the new database table. We can manage version control for our database using Laravel Migrations. Migration is really helpful for managing database when we work with developer team. Laravel Migration allows your team to easily manage and share application database scheme. Migration is pair with Laravel database Schema builder to easily build your application database schema.

When We are working with a developer team then One of our team member require a new column to the table. When team member manually adds the column to the database table. At that time we need to share database schema with all team member and it’s hard to manage database. Laravel Migrations solve that problem now we can easily share the database scheme with team member. We just need to create the migration for adding the new column then follow the artisan migrate command for update the database schema.

Laravel Migrations

We are going to learn How to create the table using Laravel Migrations and generate the database table schema. So, First of all, we are going to create migration file by following artisan command.

I hope you install Laravel If not yet just follow this tutorial.

The new migration will generate into database/migrations directory. Each migration file name contains a timestamp which allows Laravel to determine the order of the migrations. Open the Migration file and create the columns for a table like

Finally, We are ready with the first Laravel Migration file. We cover the all basic column data types for more detail visit migration document here. Now let’s run the migration file by following the artisan command

It’s too easy to manage database tables using Laravel Migrations. We just need to create the migration file then run the migration using artisan command.

 

Today, We are going to create the Laravel File Upload tutorial. Almost, The application has a file upload functionality. Laravel provides an easy interface for File Uploading. So, Today we are going to learn Laravel File Upload Tutorial. We can easily handle file upload by a user. We focus only on the File Upload code in this tutorial. If you are beginners and want to learn File Uploading step by step then Just follow this tutorial.

Laravel File Upload Tutorial

First of all, We are going to create the Form with the Browse/File input element. Let’s open your view file and create the browse button with form like.

Finally, We are ready for the file uploading. Now we need to validate the uploaded file by user and store that image into the directory. So, Let’s open the controller file for uploading and validating image file.

As per the above code, First, we validate the file like

  • Selected file must be an image file
  • The File size must be less than 2MB
  • The File type must be JPEG, JPG, PNG

We checked the basic validation file If you want to add more restriction for Image uploading you can write your own validation rules.

The uploaded by a user are stored in the public/images directory. You can easily access the uploaded file from the images folder like.

Just replace the $imageName with image name then check the image are visible in a browser or not.

If you face any issue with the file uploading write the comment below.

Today, We are going to learn Laravel File upload step by step. Almost, the file upload functionality exists on every application. Laravel provides an easy interface for uploading a file. Every application has file upload functionality on user profile page. So, We can easily create the file upload functionality for Laravel application. This tutorial is created only for the Laravel Beginners users. Let’s see the File upload tutorial step by step.

First of all, We are going to install Laravel by following composer command

If you face any issue with Laravel installation just follow this tutorial for Laravel installation.

Laravel File Upload

After, installing Laravel We are going to create the migration file by the following command

The Migration file is generated into “database/migrations” folder. Open the migration file and create table columns like below.

Finally, We are ready with the migration file. Now, let’s create the table by follow migration command

If you face any issue something like “1071 specified key was too long max key length is 767 bytes” then just follow this tutorial to resolve this issue. Then again follow the migrate command.

Finally, We are ready with the blog table. We use blog table for storing images data.

Laravel Model

Now, let’s we are going to the creating migration file for the Blog table by the following command.

The Model file is generated into App Folder. Just open it and write the code like.

Finally, We are ready with Laravel Eloquent Model. Now, Let’s we are going for the creating the Laravel Routes. Let’s open the routes/web.php  file and write the route like.

Finally, We are ready with the route for File upload tutorial. Now, let’s we are going for the BlogController.

Laravel Controller

Create the BlogController by following the below command.

The BlogController generate by Laravel into the “app/Http/Controllers” folder. Now, let’s open the BlogController and write the below code.

Finally, We are ready with the Laravel BlogController. Now, We are going for creating the view file for the File Upload tutorial.

Laravel View

First of all, We need to create the blog directory inside the “resources/views” Folder. Then create the two views file like.

resources/views/blogs.blade.php
resources/views/uploadfile.blade.php

Now, let’s open the blogs.blade.php file and write the code like.

Now, let’s open the uploadfile.blade.php File and write the code like.

Finally, We are ready with the Laravle File upload tutorial step by step. This tutorial is created only for Laravel beginners.

If you face any issue on Laravel File Upload writes the comment below.

Today, We are going to learn How to get last inserted id  Laravel. Many times we need the last inserted record id for the further operation. When we have parent and child database table.  Laravel provides many ways for creating the Insert queryWe can easily get the last inserted record  ID using Laravel. Today, We are going to learn 4 way to get the last inserted record ID using Laravel.

When we have Two database table parent and child table. We are going to store records on both tables at that time we need the last inserted record id of the parent table. Because, When we are going to store record on the child table Also, We need to the parent record ID for maintaining a relationship with the parent and child table.

Get last inserted id Laravel

When we are using DB::table method for inserting the record in the table. Then there is two way to getting the last inserted ID. First of all, We are going to learn insertGetId method for getting the last inserted record ID like.

The second way to getting the last inserted record ID is

Now, let’s see how we can get the last inserted record ID when we are using Laravel Eloquent. Laravel Eloquent provides the easiest way for a database query.

First of all, We are going to learn the create method of Laravel Eloquent. Let’s get the last inserted record ID when we use Laravel Eloquent for insert record into a database table.

Now, We are going to learn the save Eloquent method. Let’s get the last inserted record ID When we are using Laravel Eloquent save method for insert record into a database table.

The save method insert the record into the table and return the Object of last inserted record.

Finally, We learn a different method for insert record into a database table and different way for getting the last inserted record ID with Laravel.