Conditional Eloquent Where Clauses

Hi there,

I didn’t wrote for a while, but today I find a very good Laravel Eloquent feature to work with – Conditional Clauses.

Think of each Eloquent model as a powerful query builder allowing you to fluently query the database table associated with the model.

So, basically you can use every Query Builder methods in your Eloquent model.
Continue reading “Conditional Eloquent Where Clauses”

Conditional Eloquent Where Clauses

Laravel array validation unique values

Laravel now have ability to check if validation array have only unique values.

It’s very helpful if you want to ensure that user enters only unique values.

In this example we ensure that user create tasks with different titles.

        $validator = Validator::make($request->all(), [
            'task.*.title' => 'required|distinct',
            'task.*.body' => 'required',
        ]);

        if ($validator->fails()) {
            return redirect('task/create')
                        ->withErrors($validator)
                        ->withInput();
        }

More at documentation:
Laravel Validation Distinct values

Laravel array validation unique values

Laravel Notifications

Notification package Laravel 5

This one is awesome. Painless notifications in your application. How I use it?

In my master blade template I just put notifications printing code:

@notification()

Then in my controllers I can send notifications to blade views like this:

\Notification::success('User Updated Successfully');

But… I’m lazy bastard so one secret.

Create file: app/Observers/NotificationsObserver.php

namespace App\Observers;

class NotificationsObserver
{

    /**
     * @param $model
     */
    public function updated($model)
    {
        \Notification::success(class_basename($model) . ' Updated Successfully');
    }

    /**
     * @param $model
     */
    public function created($model)
    {
        \Notification::success(class_basename($model) . ' Created Successfully');
    }

    /**
     * @param $model
     */
    public function deleted($model)
    {
        \Notification::success(class_basename($model) . ' Deleted Successfully');

    }
}

And in every model which you want to send notifications – use this observer.

class User extends Model {

protected static function boot()
    {
        parent::boot();

        self::observe(new NotificationsObserver);
    }
}

Screenshot 2016-01-31 21.27.04

Laravel Notifications

Laravel Debugbar

Laravel Debugbar

One more must have package in dev environment. Track application load speed, database queries, ajax responses and so on. If you want to guarantee that it not shows in production:

app/Providers/AppServiceProvider.php

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        if (env('APP_DEBUG') == true) {
            $this->app->register(\Barryvdh\Debugbar\ServiceProvider::class);

            $loader = AliasLoader::getInstance();
            $loader->alias('Debugbar', \Barryvdh\Debugbar\Facade::class);
        }
    }
Laravel Debugbar

Laravel Slack

maknz/slack

This is tool allowing you to send messages to your team slack channel. I like it to catch production errors. So from now all your team will know about errors first.

    app/Exceptions/Handler.php

    /**
     * Report or log an exception.
     *
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
     *
     * @param  \Exception $e
     *
     * @return void
     */
    public function report(Exception $e)
    {
        if (app()->environment() == 'production' && ! $e instanceof NotFoundHttpException) {
            Slack::send(\Request::url() . ' ' . (string)$e);
        }

        return parent::report($e);
    }
Laravel Slack