lundi 2 mai 2016

Class 'App\Http\Controllers\Response' not found error in Laravel

I've this issue in Laravel 5: Class 'App\Http\Controllers\Response' not found. I'm new in all this, so I need your help folks.

I'm trying to get a json response from my table 'funcionarios'. My Controller is:

<? php

namespace App\ Http\ Controllers;

use Illuminate\ Http\ Request;

use App\ Http\ Requests;

use App\ funcionario;

class funcionarioPruebaController extends Controller {
  public
  function index() {

      try {

        $response = [
          'funcionarios' => []
        ];
        $statusCode = 200;
        $funcionario = \App\ funcionario::all() - > take(9);

        foreach($funcionario as $item) {

          $response['funcionarios'][] = [
            'id_funcionario' => $item - > id_funcionario,
            'nombre' => $item - > nombre,
            'apellido' => $item - > apellido,
            'ci' => $item - > ci,
            'rango' => $item - > rango,
            'direccion' => $item - > direccion,
            'telefono' => $item - > telefono

          ];
        }


      } catch (Exception $e) {
        $statusCode = 404;
      } finally {
        return Response::json(array('error' => false, $response, $statusCode));
      }

    } //
}

and my route.php is:

Route::group(array('prefix' => 'xxx'), function() {
  Route::resource('funcionarios', 'funcionarioPruebaController');
});

How can I get all the rows in a json format?



via Chebli Mohamed

Laravel routes with unlimited parameters for RESTful API

I am building a RESTful API using Laravel 5.1. The default route would be api. The user is allowed to create a url service using as many as parameters as she wants let's say .../api/p1/p2/.../pn.

How do I make a single route to point to a single Controller, so the service will be handled in a single controller?

Note : At first the application just needs to know whether the service exists or not by comparing the url with stored service in the database. As for the service itself, it can be queried into the database later.

I read that we can use * in Laravel 4, how about Laravel 5.1 ?

I have tried :

Route::resource('/api/*', 'APIServiceController'); but it does not work for unlimited parameters

or is it possible to do it like this

Route::group(['prefix' => 'api'], function () { //what should I put in the closure, how can I redirect it to a single controller });



via Chebli Mohamed

Laravel seeder integrity constraint violation 1452

I created table Users, Products, Oders, Oder_Items. this is for a ecommerce, i want that each product have a author (user). Artisan give a error :

integrity constraint violation 1452 Cannot add or update a child row: a foreign key constraint fails ('2016', 'products', CONSTRAINT 'products_user_id_foreign' FOREIGN KEY ('user_id') REFERENCES 'users' ('id') ON DELETE CASCADE).

Migration work good. Seeder information no.

MIGRATION ORDERS_ITEMS:

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateOrderItemsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('order_items', function (Blueprint $table) {
            $table->increments('id');
            $table->decimal('price', 5, 2);
            $table->integer('quantity')->unsigned();






            //--------// ogni item ha un prodotto ID
            $table->integer('product_id')->unsigned();
            $table->foreign('product_id')
                  ->references('id')
                  ->on('products')
                  ->onDelete('cascade');

            // Ogni item ha un ORDER ID, cosi possiamo filtrare tutti gli item degli ordini   
            $table->integer('order_id')->unsigned();
            $table->foreign('order_id')
                  ->references('id')
                  ->on('orders')
                  ->onDelete('cascade');;
         });





    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('order_items');
    }
}

MIGRATION ORDERS

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateOrdersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('orders', function (Blueprint $table) {
            $table->increments('id');
            $table->decimal('subtotal', 5, 2);
            $table->decimal('shipping', 5,2);



            $table->timestamps();  





         });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('orders');
    }
}

MIGRATION PRODUCTS

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateProductsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */

    //Up creare table
    public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name', 255);
            $table->string('slug'); 
            $table->text('description');
            //mostrare una piccola descrizione del prodotto
            $table->string('extract', 300);
            $table->decimal('price', 5, 2);
            $table->string('image', 300);
            //vedere se pubblico o no
            $table->boolean('visible');
            // unsigned solo valori positivi
            //e fa riferimento al id category, 
            //Se si cancella, cancellerà tutti i prodotti con quella categoria
            //Ogni prodotto ha una categoria

            $table->integer('user_id')->unsigned();

            $table->foreign('user_id')
                  ->references('id')->on('users')
                  ->onDelete('cascade');







            $table->integer('category_id')->unsigned();

            // relazioni  
            $table->foreign('category_id')
                  ->references('id')
                  ->on('categories')
                  ->onDelete('cascade');
            //crea // Ogni prodotto ha un autore( artista)




            // ----// 
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    //eliminare table
    public function down()
    {
        Schema::drop('products');
    }
}

MIGRATION USERS

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
   public function up()

{

    Schema::create('users', function (Blueprint $table) {

        $table->increments('id');

        $table->string('name');

        $table->string('lastname');

        $table->string('username');

        $table->string('birth');

        $table->string('profile');

        $table->string('country');

        $table->string('province');

        $table->string('address');

        $table->string('address2');

        $table->string('phone');

        $table->string('usertype');

        $table->string('email')->unique();

        $table->string('password', 60);

        $table->boolean('social');

        $table->boolean('active')->default(0);

        $table->string('confirm_token', 100);

        $table->rememberToken();

        $table->timestamps();

    });

}






    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('users');
    }
}

PRODUCTS SEEDER

<?php

use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;

//Con questo gli dico di usare il mio modello per questo SEEDER
use dixard\Product;

class ProductTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {



            DB::table('products')->insert([
            'name'          => 'Playera 1',
                'slug'          => 'playera-1',
                'description'   => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Delectus repellendus doloribus molestias odio nisi! Aspernatur eos saepe veniam quibusdam totam.',
                'extract'       => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit.',
                'price'         => 275.00,
                'image'         => 'http://ift.tt/1SX3Wjc',
                'visible'       => 1,

                'created_at'    => new DateTime,
                'updated_at'    => new DateTime,
                'category_id'   => 1,
                'user_id'   => 1,
        ]);




    }
}

I CALLED SEEDER :

<?php

use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;

class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Model::unguard();

        // $this->call(UserTableSeeder::class);
        $this->call(CategoryTableSeeder::class);
        $this->call(ProductTableSeeder::class);


        Model::reguard();
    }
}



via Chebli Mohamed

getting error, while update Composer

i have project in laravel-5.1 , and i have changed platform form win10 to Ubuntu. showing error: oading composer repositories with package information Updating dependencies (including require-dev) Your requirements could not be resolved to an installable set of packages.

  Problem 1
- laravel/framework v5.2.9 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.
- laravel/framework v5.2.8 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.
- laravel/framework v5.2.7 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.
- laravel/framework v5.2.6 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.
- laravel/framework v5.2.5 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.
- laravel/framework v5.2.4 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.
- laravel/framework v5.2.31 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.
- laravel/framework v5.2.30 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.
- laravel/framework v5.2.3 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.
- laravel/framework v5.2.29 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.
- laravel/framework v5.2.28 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.
- laravel/framework v5.2.27 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.
- laravel/framework v5.2.26 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.
- laravel/framework v5.2.25 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.
- laravel/framework v5.2.24 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.
- laravel/framework v5.2.23 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.
- laravel/framework v5.2.22 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.
- laravel/framework v5.2.21 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.
- laravel/framework v5.2.20 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.
- laravel/framework v5.2.2 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.
- laravel/framework v5.2.19 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.
- laravel/framework v5.2.18 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.
- laravel/framework v5.2.17 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.
- laravel/framework v5.2.16 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.
- laravel/framework v5.2.15 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.
- laravel/framework v5.2.14 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.
- laravel/framework v5.2.13 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.
- laravel/framework v5.2.12 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.
- laravel/framework v5.2.11 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.
- laravel/framework v5.2.10 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.
- laravel/framework v5.2.1 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.
- laravel/framework v5.2.0 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.
- laravel/framework v5.2.30 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.
- Installation request for laravel/framework 5.2.* -> satisfiable by laravel/framework[v5.2.0, v5.2.1, v5.2.10, v5.2.11, v5.2.12, v5.2.13, v5.2.14, v5.2.15, v5.2.16, v5.2.17, v5.2.18, v5.2.19, v5.2.2, v5.2.20, v5.2.21, v5.2.22, v5.2.23, v5.2.24, v5.2.25, v5.2.26, v5.2.27, v5.2.28, v5.2.29, v5.2.3, v5.2.30, v5.2.31, v5.2.4, v5.2.5, v5.2.6, v5.2.7, v5.2.8, v5.2.9].

To enable extensions, verify that they are enabled in those .ini files:
- /etc/php/7.0/cli/php.ini
- /etc/php/7.0/cli/conf.d/10-mysqlnd.ini
- /etc/php/7.0/cli/conf.d/10-opcache.ini
- /etc/php/7.0/cli/conf.d/10-pdo.ini
- /etc/php/7.0/cli/conf.d/20-calendar.ini
- /etc/php/7.0/cli/conf.d/20-ctype.ini
- /etc/php/7.0/cli/conf.d/20-exif.ini
- /etc/php/7.0/cli/conf.d/20-fileinfo.ini
- /etc/php/7.0/cli/conf.d/20-ftp.ini
- /etc/php/7.0/cli/conf.d/20-gettext.ini
- /etc/php/7.0/cli/conf.d/20-iconv.ini
- /etc/php/7.0/cli/conf.d/20-json.ini
- /etc/php/7.0/cli/conf.d/20-mysqli.ini
- /etc/php/7.0/cli/conf.d/20-pdo_mysql.ini
- /etc/php/7.0/cli/conf.d/20-phar.ini
- /etc/php/7.0/cli/conf.d/20-posix.ini
- /etc/php/7.0/cli/conf.d/20-readline.ini
- /etc/php/7.0/cli/conf.d/20-shmop.ini
- /etc/php/7.0/cli/conf.d/20-sockets.ini
- /etc/php/7.0/cli/conf.d/20-sysvmsg.ini
- /etc/php/7.0/cli/conf.d/20-sysvsem.ini
- /etc/php/7.0/cli/conf.d/20-sysvshm.ini
- /etc/php/7.0/cli/conf.d/20-tokenizer.ini
You can also run `php --ini` inside terminal to see which files are used by PHP in CLI mode.

Composer/json.

{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"type": "project",
"require": {
  "php": ">=5.5.9",
  "laravel/framework": "5.2.*",
  "infyomlabs/laravel-generator": "dev-master",
  "laravelcollective/html": "5.2.*",
  "infyomlabs/core-templates": "dev-master",
  "infyomlabs/swagger-generator": "dev-master",
  "jlapp/swaggervel": "dev-master",
  "doctrine/dbal": "~2.3",
  "infyomlabs/generator-builder": "dev-master",
  "yajra/laravel-datatables-oracle": "~6.0",
  "barryvdh/laravel-ide-helper": "^2.1",
  "infyomlabs/adminlte-templates": "dev-master"
},
"require-dev": {
    "fzaninotto/faker": "~1.4",
    "mockery/mockery": "0.9.*",
    "phpunit/phpunit": "~4.0",
    "symfony/css-selector": "2.8.*|3.0.*",
    "symfony/dom-crawler": "2.8.*|3.0.*"
},
"autoload": {
    "classmap": [
        "database"
    ],
    "psr-4": {
        "App\\": "app/"
    }
},
"autoload-dev": {
    "classmap": [
        "tests/TestCase.php"
    ]
},
"scripts": {
    "post-root-package-install": [
        "php -r \"copy('.env.example', '.env');\""
    ],
    "post-create-project-cmd": [
        "php artisan key:generate"
    ],
    "post-install-cmd": [
        "Illuminate\\Foundation\\ComposerScripts::postInstall",
        "php artisan optimize"
    ],
    "post-update-cmd": [
        "Illuminate\\Foundation\\ComposerScripts::postUpdate",
        "php artisan optimize"
    ]
},
"config": {
    "preferred-install": "dist"
}

}



via Chebli Mohamed

Pagination In laravel Data table

I am newbie to Laravel, I need to implement Pagination on query. Below is the query I am sending, what next to achieve it.

$arrAirlines = DB::table("(SELECT air.airline_id, air.title FROM `tbl_com_airlines_airports` as aa
                        left join tbl_com_airlines as air ON air.airline_id = aa.airline_id
                        where aa.country_id ='".$countryId."' and aa.is_deleted ='0' and air.org_id ='".$orgId."'
                        and (Select count(*) from tbl_com_airlines_airports  where aa.is_domestic ='1')>1)")
                       ;



via Chebli Mohamed

dimanche 1 mai 2016

Laravel 5.2 + Modify default login Behavior + Throllting stopped working

I have created new project in Laravel 5.2 and tried to modify default behavior of Auth-Login.

Created new method called postLogin which is being called when login form is submitted.

Below is the code I have written to achieve login throttling in postRegister Method.

protected function postLogin(AuthLoginRequest $request){

    $credentials = $this->getCredentials($request);
    $credentials['is_activated'] = "Yes";

    $remember = $request->has('remember');

    $throttles = $this->isUsingThrottlesLoginsTrait();


    if ($throttles && $this->hasTooManyLoginAttempts($request)) {
        return $this->sendLockoutResponse($request);
    }

    if (Auth::guard($this->getGuard())->attempt($credentials, $remember)) {
        // add login in case of success
        return $this->handleUserWasAuthenticated($request, $throttles);
    } else {
        if ($throttles) {
            $this->incrementLoginAttempts($request);
        }

        return redirect("/login")
            ->withInput($request->only('email', 'remember'))
            ->withErrors([
                $this->loginUsername() => $this->getFailedLoginMessage(),
            ]);
    }
}

While debugging, I found that $this->hasTooManyLoginAttempts($request) method never returns true.

Can someone please notify me what I am lacking in order to make this work?



via Chebli Mohamed

Convert array to Request object in laravel 5

I have store method in user controller like this

public function store(Request $request)
{
    User::create($request->all()); 

}

and in another controller I want use this method

 public function test(Request $request)
{
  .....
   app('\App\Http\Controllers\UserController')->store($request);
  ...
}

and $request is:

{"name":"test","email":"test@gmail.com"}

and finally show me error this

Argument 1 passed to App\Http\Controllers\UserController::store() must be an instance of Illuminate\Http\Request,

How can I convert array to Request object?



via Chebli Mohamed