Author

Admin

Browsing

Laravel provides an easy interface for mail sending. One day, I am working with Laravel Mail function. Suddenly, I had some issues with mail function of Laravel. When I check the error log I found like “expects parameter 1 to be string, array given error exception”

Next exception ‘ErrorException’ with message ‘htmlentities() expects parameter 1 to be string, object given (View: BASE_BATH/resources/views/emails/users.blade.php)’ in BASE_BATH/vendor/laravel/framework/src/Illuminate/Support/helpers.php:469

So, Somewhere in my blade template, which I use “message” for the content. When I am trying to print “message” at that time I got the exception like

expects parameter 1 to be string, array given error exception

Next exception ‘ErrorException’ with message ‘htmlentities() expects parameter 1 to be string, object given

My code for sending mail looks like

Laravel Mail Code

Laravel Mail Blade template “users.blade.php” looks like

When I check my code everything seems to look fine. As you can see my code everything looking fine. Finally, I got a solution after spending 5 hours. I made a silly mistake on my code.

The Solution

The solution is quite simple as I create a silly mistake on my code. It is also mention in Laravel Mail Documentation.

Note: A $message variable is always passed to e-mail views, and allows the inline embedding of attachments. So, it is best to avoid passing a message variable in your view payload.

As per Laravel Mail documentation $message variable is always passed to e-mail blade template. When I am trying to pass and access the $message variable from view/blade template.

Finally, I found the solution from the Laravel documentation.

The solution is We need to rename $message variable to $text and everything should work perfectly. Just rename the $message variable.

 

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.

Laravel 5 get client IP address – Many times we have to store the client IP address. In my project I working on rating functionality. I need to store the IP address to avoid repeating rating. We can easily get client IP address using Laravel 5. When we are using PHP we get client IP address using $_SERVER. Laravel 5 provides a very easy way to Laravel get IP address.

Laravel 5 get client IP address

Finally, We are going to learn Larave get IP address. We can get client IP address using Laravel Facades and Helpers like

You can use any method for getting the client IP address. If you face any issue with getting IP address write the comment below. We always most welcome for suggestion or improvements write the comment.

Today, We are going to learn Laravel print validations error messages on blade file. All Laravel application use Validation library So, every time we need to print error messages on blade view file. Let’s see how we can print the all laravel validation messages.

Laravel print validations error messages

Normally We are using the validation library with the form like create, edit, contact page. So, let’s see how we can print validations error messages like

The validation error message will work with the Laravel validation library. When we write the validation on the controller like

Then Laravel checks all the incoming request. If incoming request does not match with all conditions. Then Laravel auto redirects a user to the previous page with the validation messages.

Finally, We are ready to Laravel print validations error messages to blade file.

When I am starting new Laravel project I get an error “laravel 5 Class Input not found”. When I am going to find the solution for this error. What I see Laravel remove Input aliases from “config/app.php” file. So, If you are using Laravel 5.2 or greater version Alos, you can get this error when you are trying to use Input Class.

laravel 5 Class Input not found

Here you can check the Laravel commit to remove Input facade definition from “config/app.php”. Hence you have to manually add that to aliases array like

Also, You can import Input Facades directly from Controller as required.

Finally, We are ready for Input class. Still, If you face any issue write the comment below.