Wednesday, December 26, 2018

Create simple CRUD using Laravel 5.7 (Part 4 - Modify Controller, Model and Create route)


Hey guys...Welcome back. Today we are going to create controller and modal file in Laravel. In the controller file, you can see after we generate it automatically there are created the main function for the CRUD automatically. There are seven functions include index, create, store, show, edit, update, destroy functions. In our blade templates we are link the templates with these functions using the route keyword. You can see it when you carefully look the code. OK... Now we going to modify these functions according to our requirements what we want to do with these functions.
Let’s take one by one.


  • First open the  the index function. We need to load the index page using this functions.








  • Then create function. We need to display the page that add new student details.







  • Then we have the store function to store the data in database.















  • Now we have the show function for display the data that store in the database.



  • In edit function we need to display the details edit form page.







  • Next using the update function we can update the data.














  • Finally we can destroy the data from the database.











These are the main operations that we have to include in the CRUD controller.


Now we need to modify the model page for make the connection with database.
For it you can use the file app->Student file.
Add  ' protected $fillable = ['name''age']; '  to the model class.  

OK..now work is complete with the controller and model page sides.

Now is the important thing to do. That is create the route for run the project. Basically there are many more routes for our requirements. For now we use the web route for our project. You can see the web route in your routes folder. Open the ‘ web.php ‘ file. Then create the simple route for the project.

Add line ' Route::resource('students','StudentController'); ' to the web.php file to built a route.

At last we come to the end of our session. Now we have to run the project. Open a command prompt. Got to the project folder.

' cd\'
' cd\xampp\htdocs\MyFirstPro\blogs '

Then type ' php artisan serve '. Now open any web browser and type ' localhost:8000/students ' on the address bar. bingo...you can run your CRUD now.

We are complete coding  a CRUD using Laravel 5.7. You can try a new one your own. You can download the project from https://github.com/Dilank/Simple-CRUD-using-Laravel-5.7.git. Thank you for join my tutorial blog series. Hope it will help you. Also hope your comments. We will meet another tutorial series then. Bye.. 😊👋


Create simple CRUD using Laravel 5.7 (Part 3 - Create Table)

Hey guys...Welcome back. Today we are going to create view files as well as the html files  in Laravel. In Laravel there is special view file system call Blade templates. That is a powerful template engine and simple with Laravel.  You can see the default blade template (welcome.blade.php) is in the resources->views folder.

For our project we need some templates for our CRUD. We name them as,
  • layout.blade.php – include the common html tag structure and css, js files.
  • index.blade.php – include the main view.
  • create.blade.php – include the add details form.
  • edit.blade.php – include the edit details form.
  • show.blade.php – include the data show details page.


Ok.. first we code for the layout page. For that create a new folder inside resources->views folder name ‘ students ‘. Inside the  students folder create a page name ‘ layout.blade.php’ page and code for it.

<!DOCTYPE html>
<html>
<head>
    <title>@yield('title')</title>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.css" rel="stylesheet">

    <script language="JavaScript">
        tempAlert("close",5000);
    </script>

</head>
<body>

<div class="container">
    @yield('content')
</div>

</body>
</html>


You can include bootstrap CSS fil for the code. @yield is mainly used to define a section in a layout. You define an area (@yield) within the layout that your pages which are extending the template will put their content into.

Now we code for the index.blade .php page.

@extends('students.layout')

@section('title','Students details')

@section('content')
    <div class="row">
        <div class="col-lg-12 margin-tb">
            <div class="pull-left">
                <h2>Students Details</h2>
            </div>
            <div class="pull-right">
                <a class="btn btn-success" href="{{ route('students.create') }}"> Add New Student</a>
            </div>
        </div>
    </div>

    @if ($message = Session::get('success'))
        <div class="alert alert-success" role="alert">
            <p>{{ $message }}</p>
        </div>
    @endif

    <table class="table table-bordered">
        <tr>
            <th>No</th>
            <th>Name</th>
            <th>Age</th>
            <th width="280px">Action</th>
        </tr>
        @foreach ($students as $student)
            <tr>
                <td>{{ ++$i }}</td>
                <td>{{ $student->name }}</td>
                <td>{{ $student->age }}</td>
                <td>
                    <form action="{{ route('students.destroy',$student->id) }}" method="POST">

                        <a class="btn btn-info" href="{{ route('students.show',$student->id) }}">Show</a>

                        <a class="btn btn-primary" href="{{ route('students.edit',$student->id) }}">Edit</a>

                        @csrf
                        @method('DELETE')

                        <button type="submit" class="btn btn-danger">Delete</button>
                    </form>
                </td>
            </tr>
        @endforeach
    </table>


@endsection

Now code for the create.blade.php page.

@extends('students.layout')

@section('title','Add Students Details')

@section('content')
    <div class="row">
        <div class="col-lg-12 margin-tb">
            <div class="pull-left">
                <h2>Add New Student</h2>
            </div>
            <div class="pull-right">
                <a class="btn btn-primary" href="{{ route('students.index') }}"> Back</a>
            </div>
        </div>
    </div>

    @if ($errors->any())
        <div class="alert alert-danger">
            <strong>Whoops!</strong> There were some problems with your input.<br><br>
            <ul>
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif

    <form action="{{ route('students.store') }}" method="POST">
        @csrf

        <div class="row">
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>Name:</strong>
                    <input type="text" name="name" class="form-control" placeholder="Name">
                </div>
            </div>
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>Age:</strong>
                    <input type="text" name="age" class="form-control" placeholder="age">
                </div>
            </div>
            <div class="col-xs-12 col-sm-12 col-md-12 text-center">
                <button type="submit" class="btn btn-primary">Submit</button>
            </div>
        </div>

    </form>
@endsection


Now code for the edit.blade.php page.

@extends('students.layout')

@section('title','Edit Students details')

@section('content')
    <div class="row">
        <div class="col-lg-12 margin-tb">
            <div class="pull-left">
                <h2>Edit Student Details</h2>
            </div>
            <div class="pull-right">
                <a class="btn btn-primary" href="{{ route('students.index') }}"> Back</a>
            </div>
        </div>
    </div>

    @if ($errors->any())
        <div class="alert alert-danger">
            <strong>Whoops!</strong> There were some problems with your input.<br><br>
            <ul>
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif

    <form action="{{ route('students.update',$student->id) }}" method="POST">
        @csrf
        @method('PUT')

        <div class="row">
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>Name:</strong>
                    <input type="text" name="name" value="{{ $student->name }}" class="form-control" placeholder="Name">
                </div>
            </div>
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>Age:</strong>
                    <input type="text" name="age" value="{{ $student->age }}" class="form-control" placeholder="Age">
                </div>
            </div>
            <div class="col-xs-12 col-sm-12 col-md-12 text-center">
                <button type="submit" class="btn btn-primary">Submit</button>
            </div>
        </div>

    </form>
@endsection


Now code for the show.blade.php page.

@extends('students.layout')

@section('title','View Students details')

@section('content')
    <div class="row">
        <div class="col-lg-12 margin-tb">
            <div class="pull-left">
                <h2> Show Student</h2>
            </div>
            <div class="pull-right">
                <a class="btn btn-primary" href="{{ route('students.index') }}"> Back</a>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>Name:</strong>
                {{ $student->name }}
            </div>
        </div>
        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>Age:</strong>
                {{ $student->age }}
            </div>
        </div>
    </div>
@endsection

OK..Now we are completing the blade template design. In next tutorial we are going to code for the most important controller and modal files. Thank you. Hope your comments.




Create simple CRUD using Laravel 5.7 (Part 2 - Create Controller and Model)


Hey guys...Welcome back. Today we are going to create controller and model files in Laravel.
For this we use the artisan commands. It’s easy.


First open command prompt. Go to the project directory.

cd\
cd\xampp\htdocs\MyFirstPro\blogs


To create controller, use command ‘ php artisan make:controller studentController
Also, we can set the modal also to this code.

Finally, we can make a command as ‘ php artisan make:controller StudentController --resource  --model=Student

When you fire this command there will ask for generate the Student model. Hit yes to that. Then you can create the controller file and the model file successfully.

Note : When you name the model file you must put the singular word of the table name for the file name. Here we use ‘students’ as the table name and you must put the ‘Student’ as the model file name. This is concept of the Laravel.

So.. Now we are creating the controller file and model file. You can see the controller file project folder->app->Http->Controllers folder and the model file in app folder.OK..

Now we have to create the view files now. In the next tutorial we will Do it. Thank you. Hope this will helpful to you. Hope your comments.

Create simple CRUD using Laravel 5.7 (Part 1 - Create Database Table)


Hey guys...Welcome back. Today we are going to create a CRUD (Create, Read, Update, Delete) using Laravel 5.7.

In this tutorial as a first step we are going to setup the database to store our data. First open the xampp server and start Apache & MySQL server. Open web browser and call ‘localhost/phpmyadmin’ in address bar. Then create a new database using any name. I created database named ‘institute’.

OK. Now we have to do configuration in Laravel project file. In your Laravel project folder there is a file named .env. Open that file.  In that file there are three things ‘DB-DATABASE’, ‘DB_UASERNAME’, ‘DB_PASSWORD’. We need to change this. Change the DB-DATABASE into institute (or what ever you put as your DB name). Then change DB_UASERNAME into root (most commonly the username is ‘root’ and if you have change privileges change according to that). Normally the DB_PASSWORD is null for root. (If you have it put it).

DB_DATABASE=institute
DB_USERNAME=root
DB_PASSWORD=null

OK. Now we need a table to store the data. Yep... In larval, there is command call ‘migration’ to deal with the database and tables. We use this to create a table.
Open command prompt. Go to the project directory.

cd\
cd\xampp\htdocs\MyFirstPro\blogs

Now fired up the code ‘php artisan make:migration create_students_table  --create=students’. Then create the table migration file. You can see it in database->migrations folder inside the project folder.

Note: When you named the table use plural words as the table name. There is a concept that using in Laravel.

Now we need to create the table. Before that we need to add the table fields to the migration file. I add two fields name(string) and age(integer) that different data types. 






































OK.. Then using  the command prompt (in project directory) we can create the table using the command ‘php artisan migrate’.

Note: If there is error in migration you need to set the default leangth of string. For that you need to edit the ' app->Providers->AppServiceProvider.php ' file. Import Schema file to it. For it add  ' use Illuminate\Support\Facades\Schema;line on the top of the page. Then edit the boot funtion with adding the code lineSchema::defaultStringLength(191); ' . Then type ' php artisan:migrate fresh '.
 
OK.. now our table is ready. You can see it php myadmin in xampp. In next  tutorial we will discuss about how to create the controller and the model in Laravel. Hope this is this is help to you. Also hope your  comments. Thank you. 
 


Create simple CRUD using Laravel 5.7 (Part 4 - Modify Controller, Model and Create route)

Hey guys...Welcome back. Today we are going to create controller and modal file in Laravel. In the controller file, you can see after we g...