Skip to content

Instantly share code, notes, and snippets.

View bayareawebpro's full-sized avatar
✔️
Available for Consulting

Dan Alvidrez bayareawebpro

✔️
Available for Consulting
View GitHub Profile
<?php
// === Eloquent Model Filter Function === //
private function filters($query, $request)
{
if ($request->has('user')) {
$query->where('user_id', $request->get('user'))->whereNotNull('text');
//profile - show all except responses without text
} else {
@bayareawebpro
bayareawebpro / laravel-timezone-ancestor.php
Created July 11, 2017 03:48
Laravel Timezone Ancestor
<?php
public function getCreatedAtAttribute($value){
return \Carbon\Carbon::createFromTimestampUTC(strtotime($value))->timezone(User::currentUser()->timezone);
}
<?php
$user = User::first($id)
//Generate Token
do {
$token = str_random(128);
} while (User::where('token', $token)->exists());
$user->token = $token;
@bayareawebpro
bayareawebpro / LaravelCollectiveFormModelBindingArrayFields.php
Last active October 24, 2019 06:08
Filling a Model Relationship as an attribute
<?php
/**
* HasOne Affiliate Relationship
* @return \Illuminate\Database\Eloquent\Relations\hasOne
*/
function affiliate(){
return $this->hasOne(Affiliate::class, 'user_id','id');
}
@bayareawebpro
bayareawebpro / TrackAffiliates.php
Last active September 16, 2017 07:08
Affiliate Tracker Middleware
<?php namespace App\Http\Middleware;
use App\Affiliate;
use App\Referral;
use Closure;
use Cookie;
class TrackAffiliates
{
/**
* Run the request filter.
*
@bayareawebpro
bayareawebpro / StripePlanEvents.php
Last active September 16, 2017 09:35
Sync Stripe Plan with Local Model
<?php
//Your Stripe Plan Model
protected static function boot() {
parent::boot();
static::creating(function($model) { // before create() method call this
$model->stripe_id = str_slug($model->name);
Stripe\Stripe::setApiKey(env('STRIPE_SECRET'));
try{
<?php
$results = $this->model->get(array('id','column1','column2','column3', 'etc'));
$fileName = 'my-export-'.Carbon::now()->timestamp;
$export = $this->excel->create($fileName, function($excel) use ($results) {
$excel->setTitle('Our new awesome title')
->setCreator('Maatwebsite')
->setCompany('Maatwebsite')
<?php
Route::get('social/{provider}', 'Auth\SocialController@redirect');
Route::get('social/{provider}/callback', 'Auth\SocialController@handle');
class SocialController extends Controller{
public function redirect(Request $request, $provider){
return Socialite::driver($provider)->fields([
'name',
<?php
public function scopeActivePlansForUser($query, $user){
$plansArray = $user->subscriptions()->get()->sortBy('ends_at')->pluck('stripe_plan')->toArray();
$ordered = "'".implode("','",$plansArray)."'"; // format order: '1','3',
return $query->whereIn('stripe_id',$plansArray)->orderByRaw(\DB::raw("FIELD(stripe_id, $ordered)"));
}
<?php namespace App\Logs;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\FirePHPHandler;
use Monolog\Logger as Log;
use Monolog\Handler\StreamHandler;
//Example:
//Logger::write('info','Auth', 'User Logged In', $request->user()->getLoggerAttributes());
class Logger {