vendredi 29 septembre 2017

Laravel 5.1 - Get artisan arguments from internal call

I do

when I run artisan queue:work or artisan queue:listen it runs the current commands with their corresponding Arguments. Now my question is, how can I Access those Arguments?

As you can see in the following Picture, the Arguments are there but I have no clue how to Access them?

enter image description here



via Chebli Mohamed

How to make laravel Blueprints morphs method to add column after a specified coloum

While creating migration scripts I can do something like this

Schema::table('books', function(Blueprint $table)
        {
            $table->string('reference')->after('access');
        });

this will create my reference column after access column. But if I want to use morph how would I do this. I was thinking of doing this

Schema::table('books', function(Blueprint $table)
        {

            $table->morphs('reference')->after('access');
        });

However, this gives me a migration error when I try to run the migration. This is because morphs doesn't have a after method. I am using laravel 5.1. I am not sure how I could have the reference columns after access. Any help or suggestion would be great. Thank you



via Chebli Mohamed

Class 'Symfony\Component\HttpFoundation\ParameterBag' not found

I'm getting the following error when I try to run php artisan on a clean install of Laravel 5.1.43 on a Vagrant box. I've listed the versions below.

PHP Fatal error:  Class 'Symfony\Component\HttpFoundation\ParameterBag' not found in /var/www/vendor/symfony/http-foundation/Request.php on line 240
PHP Stack trace:
PHP   1. {main}() /var/www/artisan:0
PHP   2. Illuminate\Foundation\Console\Kernel->handle() /var/www/artisan:36
PHP   3. Illuminate\Foundation\Console\Kernel->bootstrap() /var/www/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:105
PHP   4. Illuminate\Foundation\Application->bootstrapWith() /var/www/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:208
PHP   5. Illuminate\Foundation\Bootstrap\SetRequestForConsole->bootstrap() /var/www/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:203
PHP   6. Symfony\Component\HttpFoundation\Request::create() /var/www/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/SetRequestForConsole.php:20
PHP   7. Symfony\Component\HttpFoundation\Request::createRequestFromFactory() /var/www/vendor/symfony/http-foundation/Request.php:398
PHP   8. Symfony\Component\HttpFoundation\Request->__construct() /var/www/vendor/symfony/http-foundation/Request.php:1943
PHP   9. Symfony\Component\HttpFoundation\Request->initialize() /var/www/vendor/symfony/http-foundation/Request.php:222


  [Symfony\Component\Debug\Exception\FatalErrorException]
  Class 'Symfony\Component\HttpFoundation\ParameterBag' not found

Host machine:

  • macOS 10.13 17A365
  • Vagrant 2.0.0
  • Composer 1.5.2 2017-09-11 16:59:25

Vagrant box:

  • Ubuntu 14.04.5 LTS (GNU/Linux 3.13.0-132-generic x86_64)
  • PHP 5.6.31-6+ubuntu14.04.1+deb.sury.org+1 (cli) with Xdebug v2.5.5

If it is helpful, I can upload my Vagrantfile and install.sh files, and also my composer.json file.



via Chebli Mohamed

lundi 25 septembre 2017

Laravel 5.1 - Possible incorrect route configuration

I have the following routes set in my routes.php file:

  Route::resource('eventos', 'EventosController');
    Route::group(['prefix' => 'eventos'], function () {
        Route::get('filtrar', ['as' => 'eventos.filtrar', 'uses' => 'EventosController@filtrar']);
        Route::get('/', ['as' => 'eventos.index', 'uses' => 'EventosController@index']);
    });

Using the command line tool php artisan route:list I get the following output:

        | GET|HEAD                       | eventos                                              | eventos.index        | App\Http\Controllers\EventosController@index                  | auth,s
etViewVariables |
|        | POST                           | eventos                                              | eventos.store        | App\Http\Controllers\EventosController@store                  | auth,s
etViewVariables |
|        | GET|HEAD                       | eventos/create                                       | eventos.create       | App\Http\Controllers\EventosController@create                 | auth,s
etViewVariables |
|        | GET|HEAD                       | eventos/filtrar                                      | eventos.filtrar      | App\Http\Controllers\EventosController@filtrar                | auth,s
etViewVariables |
|        | GET|HEAD                       | eventos/{eventos}                                    | eventos.show         | App\Http\Controllers\EventosController@show                   | auth,s
etViewVariables |
|        | DELETE                         | eventos/{eventos}                                    | eventos.destroy      | App\Http\Controllers\EventosController@destroy                | auth,s
etViewVariables |
|        | PUT                            | eventos/{eventos}                                    | eventos.update       | App\Http\Controllers\EventosController@update                 | auth,s
etViewVariables |
|        | PATCH                          | eventos/{eventos}                                    |                      | App\Http\Controllers\EventosController@update                 | auth,s
etViewVariables |
|        | GET|HEAD                       | eventos/{eventos}/edit                               | eventos.edit         | App\Http\Controllers\EventosController@edit                   | auth,s
etViewVariables |
|        | GET|HEAD                       | usuarios                                             | usuarios.index       | App\Http\Controllers\UsersController@index                    | auth,s
etViewVariables |

The problem is that the route eventos.filtrar is not redirecting to the filtrar method in EventosController.

Here's the form that should redirect when submitted to the route in question:

{!! \Form::open(['route' => 'eventos.filtrar', 'method' => 'GET']) !!}

In EventosController@filtrar all I'm doing is dding the request, but all I get is a blank page. Instead of the request object.

public function filtrar(Request $request)
{
    dd($request->all());
}

All the RESTful routes seems to be working fine as well as the route that redirects to the index method.

Any suggestions?



via Chebli Mohamed

samedi 23 septembre 2017

Generate random number onclick and count generated numbers?

Hi i want go generate 6 digits unique random numbers on button click, and i want to controll how numbers was generate using select list. I make controller and i make few methods that generate the numbers but i don't know how to run methods on button click.

This is my controller:

 public function index(){
    $numbers = Number::all();
    return view('authUser.generatedNumbers', compact('numbers'));
}

public function store(Request $request){

    $number = new Number;

    $number->number = $this->getGenaratedNumber();
    $number->save();
}


public function getGeneratedNumber(){
    do{
        $rand = $this->generateRandomNumber(6);
    }while(!empty(Number::where('number',$rand)->first()));
    return $rand;
}



public function generateRandomNumber($length) {
    $characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomNumber = '';
    for ($i = 0; $i < $length; $i++) {
        $randomNumber .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomNumber;
}

and this is my form:

 <form class="form-horizontal" method="POST" action="">
                            

                            <div class="form-group">
                                <div class="col-md-6">
                                    <label for="prefix">Prefix</label>
                                    <select name="prefix" class="form-control">
                                        <option value="0888">0888</option>
                                        <option value="0877">0877</option>
                                        <option value="0889">0889</option>
                                    </select>
                                </div>
                                <div class="col-md-6">
                                    <label for="count">Count</label>
                                    <select name="count" class="form-control">
                                        <option value="100">100</option>
                                        <option value="200">200</option>
                                        <option value="300">300</option>
                                        <option value="400">400</option>
                                    </select>
                                </div>
                            </div>

                            <div class="form-group">
                                <div class="col-md-12">
                                    <button type="submit" class="btn btn-primary btn-block">
                                        Generate Numbers
                                    </button>
                                </div>
                            </div>
                        </form>



via Chebli Mohamed

vendredi 22 septembre 2017

¿Create model with multiple relationships?

Basically I have the following models

  • Applications
  • Products
  • Users

These models are related in the following way

A request contains a type of product and this request is made by a user

Then the relationships for model requests are

//Usuario que hace la solicitud
public function user(){
  return $this->belongsTo(User::class);
}


public function product(){
      return $this->belongsTo(Product::class);
    }

Relationships for the user model

public function solicitudes(){
  return $this->hasMany(Solicitud::class);
}

Relationships for the model Product

  public function solicitudes(){
  return $this->hasMany(Solicitud::class);
}

The doubt arises since the request model has two relations with user and product then what would be the best way to create the request model since now as I am doing it is as follows.

$user->solicitudes()->create(['product_id'=>1,'size_dd'=>'22','snapshot'=>1]);

as it is basically observed I am passing the product id to the * create * method so that it does not generate errors, but I want to know what would be the best way to do it considering that it has double relation.



via Chebli Mohamed

jeudi 21 septembre 2017

How can I enable a certain route access only when a certain condition is true?

GOAL

I'm trying to block my main login page base on a flag of my .env file. I'm not sure if this is the right approach.


Steps

In my .env I've added

LOGIN_PAGE=true

Then in my route file

I add that if check

if(env('LANDING_PAGE') == true){

    Route::get('/',['as' => 'login', 'uses'=>'AuthController@getSignIn']);
    Route::post('/', 'AuthController@postSignIn');
}


Result

LOGIN_PAGE=fasle

I go to my log-in page, I got 404 Page not found which is good.

LOGIN_PAGE=true

I go to my log-in page, I still got 404 Page not found which is not good. I should see my log-in page.

What did I forget ? How would one and go about and #GOAL

I'm trying to block my main login page base on a flag of my .env file. I'm not sure if this is the right approach.


Steps

In my .env I've added

LOGIN_PAGE=true

Then in my route file

I add that if check

if(env('LANDING_PAGE') == true){

    Route::get('/',['as' => 'login', 'uses'=>'AuthController@getSignIn']);
    Route::post('/', 'AuthController@postSignIn');
}


Result

LOGIN_PAGE=fasle

I go to my log-in page, I got 404 Page not found which is good.

LOGIN_PAGE=true

I go to my log-in page, I still got 404 Page not found which is not good. I should see my log-in page.

What did I forget ? How would one and go about and enable a certain route access only when a certain condition is true ?

Any hints / suggestions will be much appreciated !



via Chebli Mohamed