vendredi 29 janvier 2016

How to create two separate login/register sections in Laravel 5

How can I create user login/register pages and business login/register pages in Laravel 5. My AuthController uses:

use AuthenticatesAndRegistersUsers, ThrottlesLogins;

My routes.php is using:

Route::auth();

This is fine for user registration. How can I add a seperate registration for another entity that is different from the user?



via Chebli Mohamed

Elequent Relationships Error

I have created a posts table & comments table for posts comments. I want to get my posts with their comments ...

In my Post Model :

public function comments()
{
     return $this->hasMany('App\Comment');
}

In my Comment Model :

public function post()
{
  return $this->belongsTo('App\Post');
}

And this is my Controller :

$post = Post::findOrFail($ID)->comments;
return view('show', compact('post'));

But I get this error : Undefined property: Illuminate\Database\Eloquent\Collection::$ID

What is my problem ?



via Chebli Mohamed

Laravel 5.2, Input::all() is empty

I have strange problem. On localhost evrything is ok. But on my production website function Input::all() return empty Array.

For example http:://mypage.com?action=run etc.

nginx configuration

    server {
root /usr/share/nginx/www/g2g/public;
index index.html index.htm index.php;
server_name mypage.com www.mypage.com;

location / { 
    try_files $uri $uri/ /index.html /index.php;
}   

location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;

    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
}   

}



via Chebli Mohamed

Laravel Time Difference Sum

I have a basic table which captures all sign in and out of a member of staff.

I am using laravel for my back-end and im struggling how to get the total number of hours on site.

ID | in_time   | out_time | in_date    | out_date
1  | 21:22:49  | 21:46:05 | 2016-01-28 | 2016-01-28
2  | 08:12:12  | 14:12:01 | 2016-01-28 | 2016-01-28

See my query so far

$date1 = '2015-01-28';
$date2 = '2015-01-28';

$attendancehours = DB::table('staff_attendances')->whereBetween('in_date', array($date1, $date2))->where('id', $sID)
        ->get();

How would I output total hours on site for that daterange?



via Chebli Mohamed

laravel update from 5.1 to 5.2 composer update error

I am trying to update laravel from 5.1 to 5.2 . I followed the instruction as according to update doc but I am getting this error when running composer udpate:

Error Output: PHP Fatal error: Call to undefined method Illuminate\Foundation\Application::bindShared() in /my_app/vendor/illuminate/html/HtmlSe rviceProvider.php on line 36

I've looked up this issue and saw that people were getting this issue when updating to version 5.1 and saw that the method bindShared has been replaced with singleton(), but I don't want to mess with vendor files. Here is my config/app.php providers and aliases arrays:

'providers' => [

    /*
     * Laravel Framework Service Providers...
     */
    Illuminate\Auth\AuthServiceProvider::class,
    Illuminate\Broadcasting\BroadcastServiceProvider::class,
    Illuminate\Bus\BusServiceProvider::class,
    Illuminate\Cache\CacheServiceProvider::class,
    Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
    Illuminate\Cookie\CookieServiceProvider::class,
    Illuminate\Database\DatabaseServiceProvider::class,
    Illuminate\Encryption\EncryptionServiceProvider::class,
    Illuminate\Filesystem\FilesystemServiceProvider::class,
    Illuminate\Foundation\Providers\FoundationServiceProvider::class,
    Illuminate\Hashing\HashServiceProvider::class,
    Illuminate\Mail\MailServiceProvider::class,
    Illuminate\Pagination\PaginationServiceProvider::class,
    Illuminate\Pipeline\PipelineServiceProvider::class,
    Illuminate\Queue\QueueServiceProvider::class,
    Illuminate\Redis\RedisServiceProvider::class,
    Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
    Illuminate\Session\SessionServiceProvider::class,
    Illuminate\Translation\TranslationServiceProvider::class,
    Illuminate\Validation\ValidationServiceProvider::class,
    Illuminate\View\ViewServiceProvider::class,
    Illuminate\Html\HtmlServiceProvider::class,

    /*
     * Application Service Providers...
     */
    App\Providers\AppServiceProvider::class,
    App\Providers\EventServiceProvider::class,
    App\Providers\RouteServiceProvider::class,
    App\Providers\ViewComposerServiceProvider::class,
    Laracasts\Flash\FlashServiceProvider::class,
    Baum\Providers\BaumServiceProvider::class

],

/*
|--------------------------------------------------------------------------
| Class Aliases
|--------------------------------------------------------------------------
|
| This array of class aliases will be registered when this application
| is started. However, feel free to register as many as you wish as
| the aliases are "lazy" loaded so they don't hinder performance.
|
*/

'aliases' => [

    'App'       => Illuminate\Support\Facades\App::class,
    'Artisan'   => Illuminate\Support\Facades\Artisan::class,
    'Auth'      => Illuminate\Support\Facades\Auth::class,
    'Blade'     => Illuminate\Support\Facades\Blade::class,
    'Bus'       => Illuminate\Support\Facades\Bus::class,
    'Cache'     => Illuminate\Support\Facades\Cache::class,
    'Config'    => Illuminate\Support\Facades\Config::class,
    'Cookie'    => Illuminate\Support\Facades\Cookie::class,
    'Crypt'     => Illuminate\Support\Facades\Crypt::class,
    'DB'        => Illuminate\Support\Facades\DB::class,
    'Eloquent'  => Illuminate\Database\Eloquent\Model::class,
    'Event'     => Illuminate\Support\Facades\Event::class,
    'File'      => Illuminate\Support\Facades\File::class,
    'Hash'      => Illuminate\Support\Facades\Hash::class,
    'Input'     => Illuminate\Support\Facades\Input::class,
    'Inspiring' => Illuminate\Foundation\Inspiring::class,
    'Lang'      => Illuminate\Support\Facades\Lang::class,
    'Log'       => Illuminate\Support\Facades\Log::class,
    'Mail'      => Illuminate\Support\Facades\Mail::class,
    'Password'  => Illuminate\Support\Facades\Password::class,
    'Queue'     => Illuminate\Support\Facades\Queue::class,
    'Redirect'  => Illuminate\Support\Facades\Redirect::class,
    'Redis'     => Illuminate\Support\Facades\Redis::class,
    'Request'   => Illuminate\Support\Facades\Request::class,
    'Response'  => Illuminate\Support\Facades\Response::class,
    'Route'     => Illuminate\Support\Facades\Route::class,
    'Schema'    => Illuminate\Support\Facades\Schema::class,
    'Session'   => Illuminate\Support\Facades\Session::class,
    'Storage'   => Illuminate\Support\Facades\Storage::class,
    'URL'       => Illuminate\Support\Facades\URL::class,
    'Validator' => Illuminate\Support\Facades\Validator::class,
    'View'      => Illuminate\Support\Facades\View::class,
'Form'      => Illuminate\Html\FormFacade::class,
'Html'      => Illuminate\Html\HtmlFacade::class,
'Carbon'    => 'Carbon\Carbon',
'Flash'     => Laracasts\Flash\Flash::class,

],

and here is my composer.json:

{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"type": "project",
"repositories": [{
    "type": "vcs",
    "url": "http://ift.tt/1WRC127"
    }],
"require": {
    "php": ">=5.5.9",
    "laravel/framework": "5.2.*",
    "doctrine/dbal": "^2.6@dev",
    "illuminate/html": "^5.0@dev",
    "laracasts/flash": "dev-master",
    "fairholm/elasticquent": "dev-feature/laravel-5",
    "baum/baum": "~1.1"
},
"require-dev": {
    "fzaninotto/faker": "~1.4",
    "mockery/mockery": "0.9.*",
    "phpunit/phpunit": "~4.0",
    "phpspec/phpspec": "~2.1",
    "symfony/dom-crawler": "~3.0",
    "symfony/css-selector": "~3.0"

},
"autoload": {
    "classmap": [
        "database"
    ],
    "psr-4": {
        "App\\": "app/"
    }
},
"autoload-dev": {
    "classmap": [
        "tests/TestCase.php"
    ]
},
"scripts": {
    "post-install-cmd": [
        "php artisan clear-compiled",
        "php artisan optimize"
    ],
    "post-update-cmd": [
        "php artisan clear-compiled",
        "php artisan optimize"
    ],
    "post-root-package-install": [
        "php -r \"copy('.env.example', '.env');\""
    ],
    "post-create-project-cmd": [
        "php artisan key:generate"
    ]
},
"config": {
    "preferred-install": "dist"
},
"minimum-stability": "dev",
"prefer-stable": true
}

any idea what I should do to fix this? I started with a 5.1 version install of laravel.



via Chebli Mohamed

Laravel 5.2: Is it possible to use where clause condition in eloquent object

Fetched the initial data by joining the other table

$results = Model::join('joining other table')->where('initial condition')->limit(100)->get();

Now, i need to filter the data by some additional condition.

$new = $results->where('column',value); // Additional Condition

I had tried this but it returns empty collection, even though persist in the $results collection. Is it possible to use the where condition in later part ?.



via Chebli Mohamed

Laravel php's date_add function don't work

Made this Laravel seeder function. Problem is the dates dosen't change as expected. If I run the code in Tinker it works.

What am I doing wrong?

public function run() {
    $faker = Faker::create();

    foreach(range(1, 10) as $index)
    {
        $myDateCalled = $faker->dateTime($max = 'now');

        $myDateAllocated = $myDateCalled;
        $i = $faker->numberBetween($min = 5, $max = 300);
        date_add($myDateAllocated, date_interval_create_from_date_string("{$i} seconds"));

        $myDatePickedUp = $myDateAllocated;
        $i = $faker->numberBetween($min = 5, $max = 30);
        date_add($myDatePickedUp, date_interval_create_from_date_string("{$i} minutes"));

        $myDateDelivered = $myDatePickedUp;
        $i = $faker->numberBetween($min = 10, $max = 90);
        date_add($myDateDelivered, date_interval_create_from_date_string("{$i} minutes"));


        dd($myDateCalled, $myDateAllocated, $myDatePickedUp, $myDateDelivered);

        Hhtransport::create([
            'Called' => $myDateCalled,
            'Allocated' => $myDateAllocated,
            'PickedUp' => $myDatePickedUp,
            'Delivered' => $myDateDelivered,
            'From' => $faker->numberBetween($min = 1, $max = 99),
            'To' => $faker->numberBetween($min = 1, $max = 99),
            'Put' => $faker->numberBetween($min = 1, $max = 99),
            'Drev' => $faker->numberBetween($min = 1, $max = 99),
            'Container' => $faker->numberBetween($min = 1, $max = 99)
        ]);
    }
}



via Chebli Mohamed