Export html table to excel javascript

Export html table to excel javascript

  • Setting up Laravel Project

  • Authentication (Login/Register)

  • CRUD Operations

  • REST API Development

  • Laravel Migrations, Models, and Relationships

  • Laravel Blade Templates

  • Working with Eloquent ORM

  • Deploying a Laravel Application

1. Setting Up a Laravel Project

To get started with Laravel, you need to have Composer installed on your machine.

  1. Install Composer: If you haven't already, download Composer and install it globally.

  2. Create a Laravel Project: Run the following command to create a new Laravel project.

    composer create-project --prefer-dist laravel/laravel my-laravel-app

    This will create a new Laravel application in the my-laravel-app directory.

  3. Navigate to your project directory:

    cd my-laravel-app
  4. Run Laravel Development Server:

    php artisan serve

    By default, the application will be accessible at http://localhost:8000.

2. Authentication (Login/Register)

Laravel comes with built-in authentication using Laravel Breeze, Jetstream, or UI for older versions.

Install Breeze for Authentication:

  1. Install Laravel Breeze:

    composer require laravel/breeze --dev
  2. Install Breeze Scaffolding:

    php artisan breeze:install
  3. Install NPM dependencies:

    npm install && npm run dev
  4. Migrate the Database (Creates tables for users):

    php artisan migrate

This will provide the basic authentication views for login, registration, password reset, and email verification.

3. CRUD Operations

CRUD stands for Create, Read, Update, and Delete operations, which are fundamental operations for interacting with a database.

Create a Model, Controller, and Migration

  1. Generate Model and Migration:

    php artisan make:model Post -m

    This will create a Post model and a migration file.

  2. Edit the Migration File to define the table structure in database/migrations/{timestamp}_create_posts_table.php:

    public function up() { Schema::create('posts', function (Blueprint $table) { $table->id(); $table->string('title'); $table->text('content'); $table->timestamps(); }); }
  3. Run the Migration:

    php artisan migrate
  4. Create Controller for Post CRUD:

    php artisan make:controller PostController
  5. Define Methods in the Controller (app/Http/Controllers/PostController.php):

    use App\Models\Post; use Illuminate\Http\Request; class PostController extends Controller { // Show all posts public function index() { $posts = Post::all(); return view('posts.index', compact('posts')); } // Show form to create a new post public function create() { return view('posts.create'); } // Store new post in database public function store(Request $request) { $request->validate([ 'title' => 'required|max:255', 'content' => 'required', ]); Post::create($request->all()); return redirect()->route('posts.index'); } // Show a specific post public function show($id) { $post = Post::findOrFail($id); return view('posts.show', compact('post')); } // Show form to edit a post public function edit($id) { $post = Post::findOrFail($id); return view('posts.edit', compact('post')); } // Update post in the database public function update(Request $request, $id) { $request->validate([ 'title' => 'required|max:255', 'content' => 'required', ]); $post = Post::findOrFail($id); $post->update($request->all()); return redirect()->route('posts.index'); } // Delete a post public function destroy($id) { Post::findOrFail($id)->delete(); return redirect()->route('posts.index'); } }
  6. Define Routes in routes/web.php:

    use App\Http\Controllers\PostController; Route::resource('posts', PostController::class);
  7. Create Views for the posts (create, index, edit, show) in resources/views/posts/. Here is an example for index (resources/views/posts/index.blade.php):

    <h1>All Posts</h1> <a href="{{ route('posts.create') }}">Create Post</a> <ul> @foreach ($posts as $post) <li> <a href="{{ route('posts.show', $post->id) }}">{{ $post->title }}</a> <a href="{{ route('posts.edit', $post->id) }}">Edit</a> <form action="{{ route('posts.destroy', $post->id) }}" method="POST"> @csrf @method('DELETE') <button type="submit">Delete</button> </form> </li> @endforeach </ul>

4. Working with Eloquent ORM

Eloquent is Laravel's built-in ORM (Object-Relational Mapping) tool that makes interacting with your database simple.

Basic Queries:

  • Create a new record:

    $post = new Post; $post->title = 'My First Post'; $post->content = 'This is the content of the post.'; $post->save();
  • Retrieve all records:

    $posts = Post::all();
  • Find a record by ID:

    $post = Post::find(1);
  • Update a record:

    $post = Post::find(1); $post->title = 'Updated Title'; $post->save();
  • Delete a record:

    $post = Post::find(1); $post->delete();

5. Laravel Blade Templates

Laravel Blade is a powerful templating engine that allows you to use clean, readable syntax in your views.

Basic Blade Syntax:

  • Display Variables:

    <h1>{{ $post->title }}</h1>
  • Control Structures:

    @if ($user) <p>Hello, {{ $user->name }}!</p> @else <p>Guest</p> @endif
  • Looping:

    @foreach ($posts as $post) <p>{{ $post->title }}</p> @endforeach

6. Deploying Laravel Application

Once you've developed your Laravel application, you can deploy it to a server. Here's how:

  1. Prepare your server (e.g., shared hosting, VPS, or cloud server).

  2. Install Composer on your server.

  3. Clone your project from Git (if applicable).

  4. Install dependencies:

    composer install
  5. Set Environment Variables (.env file):

    • Set up database, mail, and other environment configurations.

  6. Run Migrations:

    php artisan migrate --force
  7. Optimize Laravel for production:

    php artisan config:cache php artisan route:cache
  8. Set Permissions for storage and cache directories:

    chmod -R 775 storage bootstrap/cache
  9. Serve Application via web server (e.g., Apache, Nginx).


Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close