Generate a link dynamically

If you have a form or whatever where you need to call a uri you can do it dynamically with the action Methode.
 lets imagine we have a form and we need the action of the form to go to a certain uri we could add the url to the root like this:

action=”/donor/{{ auth()->guard(‘donor’)->id() }}/profile/changePassword”

in this case the uri is simple to donor/ {{ the autheticated donor with the id }}and then profile/changePassowrd. But that would be static and if something changes this root would not work anymore so what we are doing is:

{{ action(‘DonorsDonorController@updatePassword’, [auth()->guard(‘donor’)->id() ]) }}

so this get’s us the same result, but if something changes we are still good.

<form class=”form-horizontal” action=”{{ action(‘DonorsDonorController@updatePassword’, [auth()->guard(‘donor’)->id() ]) }}” method=”POST” role=”form”>

Working with cookies Laravel

Laravel version 5.5

This article explains how to save cookie data to a browser and show them to the fronted.

The controller – in our example we’ll show to to get a variable value and store it immediately to a cookie with a given name.

First things first get the cookie functionality we’ll need to import the  cookie facades and the cookieJar, at the top of your controller outside the class declaration, we’ll write:

use IlluminateCookieCookieJar;
use IlluminateSupportFacadesCookie;

at the function declaration we send in the cookieJar, which looks like this:

public function example(CookieJar $cookieJar)
{

in that fuction depending on what we’re doing with the function and what it returns we store the cookie like this:

Cookie::queue(Cookie::make('SOME_NAME', $VARIABLE_NAME, 3000));

where SOME_NAME is the name of the cookie wich will be shown on the browser, and $variable_name the variable name of course which you want to store in the cookie.

coockie

After the cookie is saved there is just one think left, showing it on the fronted. using blade:

{{ Cookie::get('SOME_NAME') }}

Where SOME_NAME is the cookie name we declared while saving it from the controller.

Show database results on all pages using view composer

To demonstrate how to show database results on all views using of course view composer we’ll do a simple counter in our application. What that counter does is increments every time a specific form is submitted. let’s immediately get to it.

  1. we’ll need a migration file to do this we open our command window or Terminal that points of course to the application root and write:

php artisan make:migration test_Counters

on that migration file we’ll add what we want to do with it (dont forget to add the blow code to public function up(){) so in this case:

Schema::create('test_Counters', function (Blueprint $table) {
    $table->bigInteger('test_counter');
});

as you can see the Schema is very simple your can be much more complicated depending on what you want to do. don’t forget you have to do a:

php artisan migrate:refresh

to make sure you database is polluted, in this case you even have to add the first number to that database column you added with the schema.

2.  we’ll need a model, to make a model we simple add one again using our terminal and write:

php artisan make:model TestCounter

in my case we don’t have to write anything in it.

3. the Controller. You can create one or you can use an existing one where the submit button is going so in my case it’s TestController.php we’ll add the following code to it:

DB::table('test_counters')->increment('test_counter');

what this code does is go to the test_counters on the database and increments the test_counter column by one.  don’t forget to call:

use DB;

at the top of the controller in order to use the DB fasade

4. Providers, your AppServiceProvider.php file lets you to execute the code you want on all views.  so on the public function boot() methode you’ll need to add this:

View::composer('*', function($view){
    $view->with('counter', testCounter::first());

});

What that code does is simply says allow the counter to be shown on all views.

5. show it, To show the results of our little counter we go to the app.blade.php page so the main layout page. All you have to do is add this to a place you want to show your results.

{{ $counter->test_counter }}

That’s it. now you can enjoy your counter.

Fix “Syntax error or access violation: 1071 Specified key was too long;”

Laravel 5.4 made a change to the default database character set, and it’s now utf8mb4 which includes support for storing emojis. This only affects new applications and as long as you are running MySQL v5.7.7 and higher you do not need to do anything.

For those running MariaDB or older versions of MySQL you may hit this error when trying to run migrations:

[IlluminateDatabaseQueryException]
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table users add unique users_email_unique(email))

[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes

As outlined in the Migrations guide to fix this all you have to do is edit your AppServiceProvider.php file and inside the boot method set a default string length:

use IlluminateSupportFacadesSchema;

public function boot()
{
    Schema::defaultStringLength(191);
}

After that everything should work as normal.