Showing posts with label Laravel. Show all posts
Showing posts with label Laravel. Show all posts

Monday, January 9, 2023

How to resolve laravel ui in Laravel Framework 7.29.3

I am currently using laravel 7.29.3 . i want install laravel ui auth using below command

composer require laravel/ui

php artisan ui bootstrap --auth

Problem 1
- Conclusion: remove laravel/framework v7.29.3
- Conclusion: don't install laravel/framework v7.29.3
- laravel/ui 3.x-dev requires illuminate/filesystem ^8.0 -> satisfiable by illuminate/filesystem[8.x-dev, v8.0.0, v8.0.1, v8.0.2, v8.0.3, v8.0.4, v8.1.0, v8.10.0, v8.11.0, v8.11.1, v8.11.2, v8.12.0, v8.12.1, v8.12.2, v8.12.3, v8.13.0, v8.2.0, v8.3.0, v8.4.0, v8.5.0, v8.6.0, v8.7.0, v8.7.1, v8.8.0, v8.9.0].
- laravel/ui v3.1.0 requires illuminate/filesystem ^8.0 -> satisfiable by illuminate/filesystem[8.x-dev, v8.0.0, v8.0.1, v8.0.2, v8.0.3, v8.0.4, v8.1.0, v8.10.0, v8.11.0, v8.11.1, v8.11.2, v8.12.0, v8.12.1, v8.12.2, v8.12.3, v8.13.0, v8.2.0, v8.3.0, v8.4.0, v8.5.0, v8.6.0, v8.7.0, v8.7.1, v8.8.0, v8.9.0].
- don't install
In such case we should determine version of laravel/ui package to be satiable with our laravel application's version using this command

composer require laravel/ui "^2.1" --dev
Or you can use this too: composer require laravel/ui:^2.4

then php artisan ui bootstrap --auth

Monday, October 24, 2022

Laravel Blade @include .html files | Laravel Blade Highlight Change Tags when use AngularJS along with Laravel

I am going to use AngularJS along with Laravel, and I wanted to change Laravel tags to [[ ]], it can achived by below codes:

Blade::setContentTags('[[', ']]'); // for variables and all things Blade

Blade::setEscapedContentTags('[[[', ']]]'); // for escaped data
But my solution is to have Angular and Blade works very simple, I create a another xxx.php every time instead of xxx.blade.php. I need some Angular and name this partial just '.php' and not '.blade.php'.

Lets say I have a user create form named "create.blade.php" inside "views/user" directory using below content:

@extends('layouts.user')
@section('content')
    <form>
        <h2>Create User</h2>
        @include('user.createBody');
    </form>
@endsection

Now I will create an php file named "createBody.php" inside "views/user" directory and put some html there:

<div class="row">
    <h1>User Name={{userName}}</h1>
    <div class="col-lg-4 col-sm-6">
        <label>Name</label>
        <input type="text" class="form-control" ng-model="userName">
    </div>
    <div class="col-lg-4 col-sm-6">
        <label>ID</label>
        <input type="text" class="form-control">
    </div>
</div>

So here we can use {{ }} tags both in Laravel and AngularJS

Sunday, March 14, 2021

Laravel Mix: Configure Babel for IE11 compatibility (transformations and polyfills)

So, basically the problem is with laravel mix (integration between laravel and reactjs) that compiled code of reacjs does not run in ie11 browser. Laravel mix has some gotchas so that ie11 can't run them properly. At this we need go apply some polyfills so that our laravel mix code run on ie11 browser without any issues.
So after google what I did to my project is (a) I installed npm install core-js@3 --save core-js3 into my application. After core-js3 installation done into my application, I created a file named .babelrc file inside my root application as followes

with following content in it:
{
  "presets": [
    [
      "@babel/preset-env",
      {
        "useBuiltIns": "usage",
        "corejs": {
          "version": 3,
          "proposals": false
        },
        "targets": {
          "ie": "11"
        }
      }
    ]
  ]
}
Now run npm run dev and you will find polyfills inserted, arrow functions compiled out etc. - your code may just run on IE11!
I had another problem after above process completed, its said SCRIPT438: Object doesn't support property or method 'assign'
Same process, after did some googling, I added below javascript link in my project and it's working fine till now:

<script src="https://cdn.jsdelivr.net/npm/es6-object-assign@1.1.0/dist/object-assign-auto.min.js">

Saturday, December 26, 2020

Handle click outside of React component | ReactJS Component Detect Click Outside Listener of HTML Element

Handle click outside of React component | ReactJS Component Detect Click Outside Listener of HTML Element

This will be helpful when we need to check click outside event for multiple elements on same component
The first thing you need to know is that you can attach and detach mousedown listeners on the document object itself.
This is how we will attach mousedown listener for our component.
Using this function we will let parent component that some click event triggered out of the element passed to component so parent class can take some action when click outside happened.
And we will get notified when outside click triggered of an specific element.
import React, { Component } from "react";
import "./styles.css";

class OutsideClickCheckHandler extends Component {
  constructor(props) {
    super(props);

    this.parent = props.parent;
    this.tag = props.tag;
    this.classToCHeck =
      "outside_click_check_handler_" + ++OutsideClickCheckHandler.counter;
    this.setWrapperRef = this.setWrapperRef.bind(this);
    this.handleClickOutside = this.handleClickOutside.bind(this);
  }

  componentDidMount() {
    document.addEventListener("mousedown", this.handleClickOutside);
  }

  componentWillUnmount() {
    document.removeEventListener("mousedown", this.handleClickOutside);
  }

  setWrapperRef(node) {
    this.wrapperRef = node;
    if (node !== undefined && node !== null) {
      node.classList.add(this.classToCHeck);
    }
  }

  handleClickOutside(e) {
    if (!e.target.closest("." + this.classToCHeck)) {
      this.parent.someOneClickedOutsideMe(this.tag);
    }
  }

  render() {
    return <div ref={this.setWrapperRef}>{this.props.children}</div>;
  }
}

export default OutsideClickCheckHandler;

if (OutsideClickCheckHandler.counter === undefined) {
  OutsideClickCheckHandler.counter = 0;
}
This is how will receive notification when outside click triggered
This is how we will use component to detect oustide click of an element, you can set multiple child component from a single component with different tag so that you know which element is clicked outside.
Full example at CodeSandBox.IO

Sunday, July 12, 2020

How to remove build files before another new build start on Laravel-Mix Webpack config file | Delete unused laravel chunk files | Clean dist folder before generating a new build - Laravel with ReactJS

Since we generate lot of builds with different hashed filenames, it is a good practice to delete the dist directory before running each build.
In laravel-mix webpack.mix.js is the configuration file we used for reactjs webpack configuration. What we have to do write some code to webpack.mix.js (located in based directory of project in my case)
const mix = require('laravel-mix');
const date = (new Date()).getTime();

const fs = require('fs');
const buildDir = './public/js/chunk/';
fs.readdir(path.resolve(buildDir), (err, files) => {
    if (err) {
        console.log(err);
    }
    else {
        files.forEach(function (file) {
            fs.unlink(path.resolve(buildDir + file), function () {
                console.log(buildDir + file + ' - deleted');
            });
        });
    }
});

/*
 |--------------------------------------------------------------------------
 | Mix Asset Management
 |--------------------------------------------------------------------------
 |
 | Mix provides a clean, fluent API for defining some Webpack build steps
 | for your Laravel application. By default, we are compiling the Sass
 | file for the application as well as bundling up all the JS files.
 |
 */

mix.react('resources/js/app.js', 'public/js')
   .sass('resources/sass/app.scss', 'public/css');

mix.webpackConfig({
    output: {
        // Directory for junk files to {ROOT_DIR}/public/js
        chunkFilename: 'js/chunk/[name]-' + date + '.js'
    },
});

Saturday, February 29, 2020

Laravel Migrations with MySQL 8.0.x - How to resolve the error: SQL authentication method unknown in Laravel-MySql

By default and for some reason, mysql 8 default plugin is auth_socket. Applications will most times expect to log in to your database using a password.
If you have not yet already changed your mysql default authentication plugin, you can do so by:

1. Log in as root to mysql
2. Run this sql command:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
Replace 'password' with your root password. In case your application does not log in to your database with the root user, replace the 'root' user in the above command with the user that your application uses.

Friday, December 27, 2019

Laravel 5 | Start Session Manually | Dynamically Set Session Id on Creating Session | Extending the StartSession Middleware | Session Handler Extending

Its really easy using the middleware in Laravel 5, I needed any request with an API key not to have a session and I simply did :
For this case you can create a StartSession middleware for your application that extends the Illuminate and need to override few methods to manage as you want.

<?php
namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Session\Middleware\StartSession as BaseStartSession;

class StartSession extends BaseStartSession
{

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        return parent::handle($request, $next);
    }

    /**
     * Get the session implementation from the manager.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Contracts\Session\Session
     */
    public function getSession(Request $request)
    {
        return tap($this->manager->driver(), function (\Illuminate\Contracts\Session\Session $session) use ($request) {
            // getting session id from request
            // then using the same id for session
            $token = request()->header("token", null);
            if (is_null($token)) {
                $token = "custom" . \Str::random(34);
            }

            $session->setId($token);
        });
    }

    /**
     * Start the session for the given request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Contracts\Session\Session
     */
    protected function startSession(Request $request)
    {
        return tap($this->getSession($request), function (\Illuminate\Contracts\Session\Session $session) use ($request) {
            $session->setRequestOnHandler($request);

            $session->start();
        });
    }
}
Now need to replace session middleware in kernel file: app\Http\Kernel.php as below:

<?php

namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
    /**
     * The application's global HTTP middleware stack.
     *
     * These middleware are run during every request to your application.
     *
     * @var array
     */
    protected $middleware = [
        \App\Http\Middleware\CheckForMaintenanceMode::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
        \App\Http\Middleware\TrustProxies::class,
    ];

    /**
     * The application's route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \App\Http\Middleware\StartSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],

        'api' => [
            \App\Http\Middleware\StartSession::class,
            'throttle:60,1',
            'bindings',
        ],
    ];

    /**
     * The application's route middleware.
     *
     * These middleware may be assigned to groups or used individually.
     *
     * @var array
     */
    protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
        'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
    ];

    /**
     * The priority-sorted list of middleware.
     *
     * This forces non-global middleware to always be in the given order.
     *
     * @var array
     */
    protected $middlewarePriority = [
        \App\Http\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\Authenticate::class,
        \Illuminate\Session\Middleware\AuthenticateSession::class,
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
        \Illuminate\Auth\Middleware\Authorize::class,
    ];
}