Skip to content

Instantly share code, notes, and snippets.

View jeffochoa's full-sized avatar

Jeff Ochoa jeffochoa

View GitHub Profile
@jeffochoa
jeffochoa / autocomplete.php
Last active May 28, 2019 10:23 — forked from imranismail/autocomplete.php
Input autocomplete Laravel + Javascript
//SearchController.php
public function autocomplete(){
$term = Input::get('term');
$results = array();
$queries = DB::table('users')
->where('first_name', 'LIKE', '%'.$term.'%')
->orWhere('last_name', 'LIKE', '%'.$term.'%')
->take(5)->get();
@jeffochoa
jeffochoa / laravel.js
Created February 24, 2016 12:36 — forked from JeffreyWay/laravel.js
Want to send a DELETE request when outside of a form? This will handle the form-creation bits for you dynamically, similar to the Rails implementation. (Requires jQuery, but doesn't have to.) To use, import script, and create a link with the `data-method="DELETE"` attribute.
/*
<a href="posts/2" data-method="delete"> <---- We want to send an HTTP DELETE request
- Or, request confirmation in the process -
<a href="posts/2" data-method="delete" data-confirm="Are you sure?">
*/
(function() {
@jeffochoa
jeffochoa / UploadMediaToWp.php
Last active January 3, 2020 13:39 — forked from s-hiroshi/WP API v2 image upload from media endpoint
WP API v2 image upload from media endpoint
<?php
/*
* WP REST API version 2.0-beta7
* API base url ishttp://www.example.com/wp-json
*
* Reference
* https://wordpress.org/support/topic/new-post-with-image
*/
/*
@jeffochoa
jeffochoa / OwnsModelTrait.php
Last active November 19, 2018 06:07
Laravel OwnsModel Trait
<?php
trait OwnsModel {
/**
* Determine whether this model owns the given model.
* Usage: if ($user->owns($article)) { ...
*
* @param Model $model
* @return bool
@jeffochoa
jeffochoa / EloquentSortableServiceProvider.php
Last active June 9, 2021 01:33
Laravel Eloquent pagination and column sorting using macros
<?php
namespace App\Providers;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\ServiceProvider;
class EloquentSortableServiceProvider extends ServiceProvider
{
@jeffochoa
jeffochoa / RouterServiceProvider.php
Created July 30, 2017 20:28
Get collection of available routes in Laravel
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Route;
use Illuminate\Routing\Route as Router;
class RouterServiceProvider extends ServiceProvider
{
@jeffochoa
jeffochoa / timer_helpers.php
Created July 31, 2017 12:51 — forked from calebporzio/timer_helpers.php
A simple helper function and macro for timing php scripts and eloquent queries
<?php
// Helper function.
if (! function_exists('timer')) {
function timer($expression)
{
$start = microtime(true);
if ($expression instanceof Closure) {
$expression();
@jeffochoa
jeffochoa / 1.ProcessClass.php
Last active April 23, 2024 21:22
Understanding Laravel pipelines
<?php
namespace App\Features;
use App\Features\FirstTask;
use App\Features\SecondTask;
use Illuminate\Pipeline\Pipeline;
// *Naming things is hard* ... So, this is a class called `ProcessClass` that `run()` some text ¯\_(ツ)_/¯
class ProcessClass
@jeffochoa
jeffochoa / img-async.html
Created October 13, 2017 22:10
Chrome image async API
<!-- if possible: -->
<!-- the decode for this image may be deferred -->
<img async=on src="space-cats.jpg">
<!-- the decode for this image should not be deferred -->
<img async=off src="space-dogs.jpg">
<!-- the browser is free to do what it feels is best for the user -->
<img src="space-pizza.jpg">
@jeffochoa
jeffochoa / Response.php
Last active April 20, 2024 12:35
Laravel HTTP status code
<?php
// This can be found in the Symfony\Component\HttpFoundation\Response class
const HTTP_CONTINUE = 100;
const HTTP_SWITCHING_PROTOCOLS = 101;
const HTTP_PROCESSING = 102; // RFC2518
const HTTP_OK = 200;
const HTTP_CREATED = 201;
const HTTP_ACCEPTED = 202;