mercredi 28 octobre 2015

Laravel 5 where clause from another table

I have a simple code and what I want to do is to access a field from another table and put it in my where clause. This is my code:

ReportController.php

$reservations = Reservation::with('room')
-> whereBetween('reservation_from', [$from, $to])
-> where('room.type', \Request::input('type')) //what should this be
-> orderBy('created_at')
-> get();

Room.php

class Room extends Model
{
    use SoftDeletes;
    protected $table = 'rooms';

    protected $fillable = ['id', 'roomNumber', 'type', 'price', 'description'];

    public function reservations() {
        return $this->hasMany('App\Reservation', 'id', 'room_number');
    }
}

Reservation.php

class Reservation extends Model
{
    use SoftDeletes;
    protected $table = 'reservations';

    protected $fillable = ['roomNumber', 'clientId', 'reservation_from', 'reservation_to'];

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

}

Schema: enter image description here

As you can see in the ReportController.php, there is a comment saying "what should this be", that's the part that I want to fix. What I wanted to do is access the type field in the rooms table in my eloquent query.

Is there a way to do this? Thank you.



via Chebli Mohamed

How can I install laravel framework for NetBeans 7.4

I have installed Laravel and I want to use use framework annotations and code completion in the NetBeans 7.4 IDE, there is no Laravel plugin in the section "Install plug-ins". I have found a plug in for Netbeans on github but I am unsure how I install it, here is the link http://ift.tt/1MujTKL



via Chebli Mohamed

Facebook PHP SDK/Laravel 5: Check if a user has granted a certain set of permissions

I want to check that a user has authorized the following permissions; 'manage_pages', 'publish_pages', 'read_insights', for my app. In short, if they decline one or more permissions, I need to know, as all are required.

Here's my callback method once I get the user's access token. How do I verify they approved all permission requests?

Note: I'm using the SammyK Laravel Facebook SDK package.

public function handleFacebookCallback()
    {
        try {
            $token = Facebook::getAccessTokenFromRedirect();

            $user = Facebook::get('/me?fields=permissions', $token)->getGraphUser();
            $permissions = $user['permissions']->asArray();

            // permissions: 'manage_pages', 'publish_pages', 'read_insights'

            if (array_key_exists('publish', $permissions['data'][0]))
            {
                // permissions exist, proceed
            }
            else 
            {
                // user denied permission(s), redirect
            }
        }
        ....



via Chebli Mohamed

Laravel and remote mount points (sshfs)

I've mounted a drive with sshfs to use with my web app, specifically for a directory listing part of my site. (i.e listing all directories / files in a folder)

As I don't store my files where my laravel installation is, I decided to use sshfs to mount my drive where laravel scans the dir.

An ls on the mounted directory shows all my files where they're suppose to be, mounted correctly.

However with laravel it throws the error "Links are not supported, encountered link at //initrd.img"

Any input would be appreciated. Thanks!



via Chebli Mohamed

Laravel, change .env with php

I am trying to make an installer script, a little bit like wordpress installer script. I am using laravel 5.1 so i would like to write a change to my .env file so my database was set up. But i can't figure out how. Can you help me?



via Chebli Mohamed

laravel apache server issue

I am getting AWS apache following issues ERROR BadMethodCallException HELP Call to undefined method Illuminate\Database\Query\Builder::isCustomerEmailIdUnique()

this is model function

public function isCustomerEmailIdUnique($email,$customerId)
{
    $dataCustomerId = DB::table($this->table)->where('email_id',$email)->get(['customer_id']);
    // If customerId of email exist , return true. if email id not exist return false

     $data = json_decode(json_encode($dataCustomerId), true);       
     if(empty( $data) || $data['customer_id'] == $customerId ){
        return true;
    }else{
        return false;
    }
}



via Chebli Mohamed

Laravel does not want to auto-inject dependencies in Service Provider

I have a service provider:

<?php namespace App\Providers;

use Carbon\Carbon;

use Illuminate\Support\Collection;
use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Cache\Store;

use File;

use App\Models\Translation;

class ModuleTranslationsServiceProvider extends ServiceProvider {

    protected $cache;

    public function __construct (Store $cache) {
        $this->cache = $cache;
    }

    /**
     * Load the config from the database.
     *
     * @return void
     */
    public function register()
    {
        $translations = $this->cache->remember('translations', function() {
            //Load and return translations.
        });
        $this->app->singleton('translations', function($app) use ($translations) {
            return $translations;
        });
    }

}

However I get the following error when the app is run (artisan or browser):

ErrorException]                                                                                                                                                                                                                                                                        
  Argument 1 passed to App\Providers\ModuleTranslationsServiceProvider::__construct() must be an instance of Illuminate\Contracts\Cache\Store, instance of Illuminate\Foundation\Application given, called in /home/user/projects/AppPlatform/vendor/laravel/framework/  
  src/Illuminate/Foundation/ProviderRepository.php on line 150 and defined                                                                                                                                                                                                                

Normally, Laravel auto-injects contracts via the constructor. Is this a limitation of Service Providers? Or am I doing it wrong?



via Chebli Mohamed