Tuesday, March 10, 2020

Vue/Laravel CRUD

PHP : ajax form submit



    $(document).ready(function() {

       $("FORMIDORCLASS").submit(function(e){

            // FORMIDORCLASS will your your form CLASS ot ID

            e.preventDefault();

       $.ajaxSetup({

            headers: {

                 'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')

                // you have to pass in between tag

            }

    })

     var formData = $("FORMIDORCLASS").serialize();

    $.ajax({

          type: "POST",

         url: "",

         data : formData,

         success: function( response ) {

              // Write here your sucees message

         }, error: function(response) {

            // Write here your error message


         },

    });

    return false;

   });

});



Laravel join query examples

  • nner Join
    DB::table('admin') ->join('contacts', 'admin.id', '=', 'contacts.user_id') ->join('orders', 'admin.id', '=', 'orders.user_id') ->select('users.id', 'contacts.phone', 'orders.price') ->get();
  • Left Join / Right Join
    $users = DB::table('admin') ->leftJoin('posts', 'admin.id', '=', 'posts.admin_id') ->get();
    $users = DB::table('admin') ->rightJoin('posts', 'admin.id', '=', 'posts.admin_id') ->get();
  • Cross Join
    $user = DB::table('sizes') ->crossJoin('colours') ->get();
  • Advanced Join
    DB::table('admin') ->join('contacts', function ($join) { $join->on('admin.id', '=', 'contacts.admin_id')->orOn(...); }) ->get();
  • Sub-Query Joins
    $admin = DB::table('admin') ->joinSub($latestPosts, '

Laravel : How to use Stored Procedures?



To create a Stored Procedure you can execute given code in your MySQL query builder directly or use phpmyadmin for this.
DROP PROCEDURE IF EXISTS `get_subcategory_by_catid`;
delimiter ;;
CREATE PROCEDURE `get_subcategory_by_catid` (IN idx int)
BEGIN
SELECT id, parent_id, title, slug, created_at FROM category WHERE parent_id = idx AND status = 1 ORDER BY title;
END
;;
delimiter ;
After this, you can use this created procedure in your code in Laravel.
How to use stored procedure in Laravel
$getSubCategories = DB::select(
   'CALL get_subcategory_by_catid('.$item->category_id.')'
);

Monday, March 9, 2020

Laravel : How to send email after user click verify link


I implemented auth system by php artisan make:auth and already setup user email verify by MustVerify from laravel feature
I want to send another email (Greeting mail) after user click verify link. How can I do that?
Solution : 
When a user is registered a Illuminate/Auth/Events/Verified event is broadcast. 

You can use this artisan command to generate a listener
php artisan make:listener SendWelcomeMail
In the listener you can add logic to the handle($event) function.
php artisan make:mail Greeting
or markdown mail :  
php artisan make:mail OrderShipped --markdown=emails.greeting 
public function handle(Verified $event)
{
    Mail::to($event->user->email)->send(new Greeting());
}
Then you register the listener with the event in the EventServiceProvider
protected $listen = [
    Registered::class => [
        SendEmailVerificationNotification::class,
    ],
    Verified::class => [
        SendWelcomeMail::class
    ],
];
Links : https://stackoverflow.com/questions/55204228/laravel-5-8-how-to-send-email-after-user-click-verify-link



Wednesday, March 4, 2020

Laravel minimal pagination

$currentPage=2,//from query or param
$perPage=3;//from env or default
$supplierObj = \App\Supplier::query();
$total=$supplierObj->count();
dd($supplierObj->forPage($currentPage, $perPage)->get());

vue js plugin for pagination buttons
https://vuejsexamples.com/pagination-component-for-vue-js-2/


npm install vue-plain-pagination







Sunday, March 1, 2020

Laravel - How to access image uploaded in storage within View?

The best approach is to create a symbolic link like @SlateEntropy very well pointed out in the answer below. To help with this, since version 5.3, Laravel includes a command which makes this incredibly easy to do:
php artisan storage:link
That creates a symlink from public/storage to storage/app/public for you and that's all there is to it. Now any file in /storage/app/public can be accessed via a link like:
http://somedomain.com/storage/image.jpg

If, for any reason, your can't create symbolic links (maybe you're on shared hosting, etc.) or you want to protect some files behind some access control logic, there is the alternative of having a special route that reads and serves the image. For example a simple closure route like this:
Route::get('storage/{filename}', function ($filename)
{
    $path = storage_path('public/' . $filename);

    if (!File::exists($path)) {
        abort(404);
    }

    $file = File::get($path);
    $type = File::mimeType($path);

    $response = Response::make($file, 200);
    $response->header("Content-Type", $type);

    return $response;
});
You can now access your files just as you would if you had a symlink:
http://somedomain.com/storage/image.jpg
If you're using the Intervention Image Library you can use its built in response method to make things more succinct:
Route::get('storage/{filename}', function ($filename)
{
    return Image::make(storage_path('public/' . $filename))->response();
});
https://stackoverflow.com/questions/30191330/laravel-5-how-to-access-image-uploaded-in-storage-within-view