Posts

Showing posts from March, 2024

Laravel Join with two coloums

 <?php namespace App\Http\Controllers; use App\Models\User; use Illuminate\Support\Facades\DB; class UserController extends Controller {     public function getUserOrders()     {         // Using Eloquent with inner join (default)         $usersWithOrders = User::join('orders', function ($join) {             $join->on('users.id', '=', 'orders.user_id');             $join->on('users.email', '=', 'orders.email'); // Additional join condition         })->get();         // Using DB facade with a more complex join (optional)         $complexJoin = DB::table('users')             ->select('users.id', 'users.name', 'orders.id as order_id', 'orders.product_id', 'orders.created_at')             ->join('orders', function ($join) {   ...

Second Highest Salary In SQL

 SELECT MAX(salary) AS second_highest_salary FROM employees WHERE salary < (   SELECT MAX(salary)   FROM employees ); select salary AS second_highest_salary  from emp order by desc limit 1,1 SELECT salary FROM (SELECT salary, ROW_NUMBER() OVER(ORDER BY salary DESC) as rank FROM `emp` ) emp where rank = 2; SELECT salary FROM (SELECT salary, DENSE_RANK() OVER(ORDER BY salary DESC) as rank FROM `emp` ) emp where rank = 2;

Add Css And Js Files on Specific Page In laravel

  1. Using Blade Layout Sections: Laravel's Blade templating engine provides a powerful way to manage layouts and include specific content on different pages. Here's how to achieve this: a. Base Layout ( layouts/app.blade.php ): <!DOCTYPE html> <html> <head> <title>My Website</title> @yield('page-css') <link rel="stylesheet" href="{{ asset('css/app.css') }}"> </head> <body> @include('partials.header') <main>@yield('content')</main> @include('partials.footer') @yield('page-js') <script src="{{ asset('js/app.js') }}"></script> </body> </html> b. Specific Page View ( views/home.blade.php ): @extends('layouts.app') @section('content')   @endsection @section('page-css')   <link rel="stylesheet" href="{{ asset('css/home.css') }}">  @endsection @se...

Diffrence between Put and Patch

PUT: Imagine you're replacing the entire contents of a document. With PUT, you send the complete updated version of the data to the server. It's like rewriting the whole document. PATCH: This is like editing specific parts of a document. You only send the changes you want to make, leaving the rest untouched. Example: Let's say you have a user profile with information like name, email, and phone number. PUT: If you want to update the entire profile with new data for all fields (name, email, phone), you'd use PUT and send the complete new user information. PATCH: If you only want to change the user's email address, you'd use PATCH and send just the updated email information. Here's the key difference: PUT: Replaces the entire resource. PATCH: Updates specific parts of a resource. 

Print Laravel Query Or Debug Laravel Query

Method 1:-  $posts = Post::where('id',' '> ,10)->toSql(); dd($posts); Method 2:- DB::enabLeQueryLog(); // Enable query tog $posts = Post::with('comments')->where('id',' '> ,10)->get(); dd(DB::getQueryLog()); // Show results of tog Method 3:- $posts = Post::with('comments')->where('id',' '> ,10)->get(); $comments = Comments::all(); DB::listen (function($query) { print_r(value: "DB:" . $query->sql ."[".implode(separator. " , " , $query->bindings) . "]"); });

Find Small number From array

 $numbers=array(12,23,45,20,5,6,34,17,9,56); $min = $numbers[0]; foreach ($numbers as $number) { if ($number<$min) { $min = $number; } } for($a=1; $a< count($numbers); $a++){  if ($numbers[$a]< $min) {   $min = $numbers[$a];  } } echo $min;