Crud in Laravel 5.5

CRUD (Create Read Update Delete) application in Laravel 5.5 , Laravel crud example

Crud in Laravel 5.5 : In this post i am going to show you how to create a sample CRUD application in Laravel 5.5 from Scratch.
Laravel is going to release his new version 5.5 in this month, It documentation is released you can read the documentation of Laravel 5.5 from https://laravel.com/docs/5.5/.
Laravel 5.5 will require PHP version >= 7.0.0 with OpenSSL,PDO,Mbstring,Tokenizer,XML PHP Extensions installed on server.Below are step by step guide to install and create a demo Laravel application to add , delete, view and update youtube videos with title,
description in Laravel.

Steps to create CRUD (Create Read Update Delete) application in Laravel 5.5


Step 1: Installing laravel 5.5

To install Laravel 5.5 please run below on your terminal, we are using Ubuntu 16.04 for this installation.

composer create-project --prefer-dist laravel/laravel Laravel5.5

Step 2 : Configuring files permissions.

Laravel requires read write permission in “storage” and “bootstrap/cache” directories , so change the permissions of these directories by running below commands.

sudo chmod -R 777 storage
 sudo chmod -R 777 bootstrap/cache

Step 3: Configuring database connection

In order to configure your database in Laravel 5.5, Please open .env file. .env file is situated on laravel root folder.




DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=Laravel
DB_USERNAME=root
DB_PASSWORD=

Step 4 Creating tables and migartions

php artisan make:migration create_videos_table

Running above command will create a new file in database/migrations directory , open that file and put below code in that

increments('id');
            $table->string('video_title');
            $table->string('video_url');
            $table->text('description');
            $table->tinyInteger('status', 0);
            $table->timestamps();
           
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('videos');
    }
}


After saving above code in file, Run below command to create your table in database.

php artisan migrate

Step 4: Creating your Model Class .

To create a model class in Laravel you can use php artisan make model command followed by model name.
Run below command to create your migration.

php artisan make:model Video

Above command will generate a new file named Video.php in app folder.Open that file put below code in it.





Step 5: Creating Controller.

You can generate your controller by running php artisan make:controller command.

php artisan make:controller VideoController --resource

Once you have executed the above command a new file name VideoController.php is created in app/Http/Controller directory, open that file and and below code in that.

  

paginate(5);

       return view('Video.index',compact('videos'));

            
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
       
       return view('Video.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

      $this->validate($request, [

            'video_title' => 'required',

            'video_url' => 'required',

        ]);


        Video::create($request->all());

        return redirect()->route('video.index')

                        ->with('success','Video created successfully');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $video = Video::find($id);
        return view('Video.show',compact('video'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $video = Video::find($id);

        return view('Video.edit',compact('video'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $this->validate($request, [

            'video_title' => 'required',

            'video_url' => 'required',

        ]);


        Video::find($id)->update($request->all());

        return redirect()->route('video.index')

                        ->with('success','Video updated successfully');

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        Video::find($id)->delete();

        return redirect()->route('video.index')

                        ->with('success','Video deleted successfully');
    }
}


Step 6 : Configuring routes

In this step we have attach our controller to application route.Open your routes/web.php
add below code in it.

 Route::resource('video','VideoController');

Step 7: Creating layout for your application.

Open resources/view directory and create a new directory named layouts.You can create your layouts directory via command line by running below commnads on terminal;

 cd /resources/views/
 mkdir layouts.

After creating layouts directory create a new file default.blade.php inside that folder add below code in it.



	
	
	  
@yield('content')



Step 8: Creating views files for application

Create a new folder named Video and create following files index.blade.php ,create.blade.php ,edit.blade.php , show.blade.php in that.

index.blade.php


@extends('layouts.default')

 

@section('content')

	

List Videos

@if($videos->count()) @foreach($videos as $video) @endforeach @else @endif
Video Title Video URL Video Description Status View Edit Delete
{{$video->video_title}} {{$video->video_url}} {{$video->description}} {{ ($video->status) ? ' Active ' : 'Inactive' }}
{{csrf_field()}}
No Records found !!
@endsection

create.blade.php


@extends('layouts.default')

 

@section('content')

	
@if (count($errors) > 0)
Whoops! There were some problems with your input.

    @foreach ($errors->all() as $error)
  • {{ $error }}
  • @endforeach
@endif @if(Session::has('success'))
{{Session::get('success')}}
@endif

Add a Video

{{ csrf_field() }}
@endsection


edit.blade.php

@extends('layouts.default')

@section('content')

	
@if (count($errors) > 0)
Whoops! There were some problems with your input.

    @foreach ($errors->all() as $error)
  • {{ $error }}
  • @endforeach
@endif @if(Session::has('success'))
{{Session::get('success')}}
@endif

Update Video : {{$video->video_title}}

{{ csrf_field() }}
@endsection

show.blade.php

@extends('layouts.default')

 

@section('content')

{{$video->video_title}}

{{$video->description}}
@endsection
Open your browser and point your browser to below Url to preview the app.
localhost/laravel5.5/public/video

8 thoughts to “Crud in Laravel 5.5”

  1. Hello!!! i have a problem on the view edit or the view show, everything good, but dont loads css or js, because is add a url before my address public where is those pluggins

  2. Hello there!

    Why I have got an error “Property [id] does not exist on this collection instance.” when view or edit button pressed?
    And “Method delete does not exist.” when I try to delete one record?

  3. Hello this is nice Tutorial ,..
    I am follow all the steps according to your tutorial .
    but now i am face this error .. Undefined variable: video (View: C:\xampp\htdocs\Laravel_crud\resources\views\video\show.blade.php)

    so tell me how to fix this error..

Leave a Reply

Your email address will not be published. Required fields are marked *