Skip to content

Instantly share code, notes, and snippets.

jimgwhit / BlogController.php
Created Jul 29, 2021 — forked from tobysteward/BlogController.php
Laravel AJAX Pagination with JQuery
View BlogController.php
class BlogController extends Controller
* Posts
* @return void
public function showPosts()
jimgwhit / db.php
Created May 16, 2021 — forked from luckyshot/db.php
PHP/MySQL (PDO Database) method with named parameters
View db.php
PHP/MySQL (PDO) method with named parameters
Last updated: 12 Sep 17
$config = [
jimgwhit /
Last active Jun 23, 2021
Fetch simple example

Just test data and uptating one field using put:

    document.getElementById('submitBtn').addEventListener('click', submitPost);
    function submitPost(e) {
        const data = {petid: document.getElementById('petid').value,
            species: document.getElementById('species').value,
            _token: document.getElementsByName("_token")[0].value,
            _method: document.getElementsByName("_method")[0].value};

First, in html if a checkbox is not checked nothing is passed for that checkbox in the request.

For example if you had a form with name and likes, (pick from the following)

Name  ________________

I like:
View Array_loop_md
On a PHP coding forum someone ask how to loop a certain array.
The data was in Json format (not shown), but this will give a good idea of how to handle looping an array.
I just named the Json data $mydata.
First json decode it:
$decoded = json_decode($mydata, true);
// I just put the json in the variable $mydata to work with.
jimgwhit /
Created Jun 30, 2020
Json response with Jquery

Just quick examples, so apply validation and authentication and authorization as needed.

For returning a single record:

    public function testc() {
        $dogid = Request::input('somevar');
        $dog = Dog::find($dogid)->toArray();
        return Response::json($dog);
// In an update method
public function add()
    // Validate as needed

At times on a shared host you may not be able to use a symbolic link. I sometimes just display from a folder outside of web folder:

A bare basic example, this file just name it DisplayImage.php

$basedir = '/some_folder/images'; // on hard drive
$imagedir = $_GET['dir'];
$image = $_GET['img'];

Anyone new to RBAC I highly suggest using laravel's or Spatie

However being familiar with RBAC I use built in authentication but have custom authorization.

I use static helper classes, but instance will also work. And these are just simple examples of making sure a required role of a method matches with one of the logged in users role.

I have a role field in users table like:

View gist:5dccf7035a29b33eaa7b
entity code:
namespace App\Model\Entity;
use Cake\ORM\Entity;
use Cake\Utility\Inflector;
* Pet Entity.