mercredi 28 octobre 2015

Method 'where' not found in class (PHPStorm) - Laravel 5.1

I'm kind of new to both laravel and PHPStorm. I'm recently working on a project where I have a model and a controller like the following:

Model:

<?php
namespace App\Http\Models;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
    public $timestamps = false;
}

Controller:

<?php

namespace App\Http\Controllers;
use App\Http\Models\Category;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class CategoryController extends Controller
{ /*....*/

And when I try to query my database using the Category model inside the controller, I get the 'where' underlines by PHPStorm and saying that the method 'where' is not found in class App\Http\Models\Category:

$default_cat = Category::where("name","=",$category)->first();

I tried almost every solution I could find on the internet, and verified that I got the namespaces right. But nothing fixed it. I even tried to make a composer update command but no result.



via Chebli Mohamed

we failed to identify your request PHPUNIT

I have a test function -

class InstrumentTest extends TestCase {

    private $faker; 

    public function setUp() {
        $this->refreshApplication();
        $this->faker = Faker\Factory::create('en_EN');

        $user = App\User::find(2);

        $this->be($user);
    }

    /**
     * Test creating new instrument
     */
    public function testCreateNewInstrument() {
        $fakeName = $this->faker->name;

        $this->visit('/oem/instrument/create')
                ->type($fakeName, 'name')
                ->press('Create')
                ->seePageIs('/oem/instrument')
                ->see('Instrument Created Successfully!');

        # make sure that record is in the database
        $this->seeInDatabase('instrument', ['name' => $fakeName, 'company_id' => 1]);
    }
}

Each time I run "phpunit" my test die with following message in the console: "We failed to identify your request."

I am not sure on how to fix this and if this is related to the middleware?



via Chebli Mohamed

How to KeyBy where multiple items have the same key

I am using Laravel Collections methods and am trying to key my query results (which are a collection) by the id. The problem is I have multiple entries with the same id, but point to different countries and I want to have all of the values, not just the last one.

Here is my code that i am using so far:

   $allCountries = new Collection($allCountries);

   $offerCountries = $allCountries->keyBy('id');

    dd($offerCountries);

    foreach ($offer as $o) {

       $o->countries = $allCountries->get($o->id);

    }

To explain, my query puts the results in $allCountries which contains ids and countries and those results looks something like this

id=>225, country=>US
id=>225, country=>IT
id=>3304, country=>NZ

Just to give you a quick idea. I want to key this by the id which results in $offerCountries. I then loop thru a previous Collection that contains offers which have a certain ID that relates to the country result by id. So for the offer 225, the countries it contains are US and IT. I loop thru each offer and set the countries object equal to all the $allCountries id that it equals. The problem I have here is keyBy overwrites the value and only takes the last one. I am hoping to get some results like this:

 [
   225 => countries: {'id' => 225, 'country' => 'US'}, {'id' =>           
   '225', 'country' => 'IT'}
    3304 => ['id' => 3304, 'country' => 'NZ'],
]

Is there a laravel method to do this, or do I need to write my own keyBy so it does not overwrite. If so, how can I get started to write this method?

Thanks



via Chebli Mohamed

Laravel 5.1 - Javascript not loaded

My app gets the views from an ajax request by navigation. So when i click a link in my menu i retrieve all the Html that my view contains.

My javascript is included inside the main template and of course all my calls works. But once i need for example to create an animated filter gallery inside a specific view, the javascript for that view doesn't work.

This is how i've organized my app:

My template

<!-- !Doctype -->
@include('partials.doctype')

<!-- Menu -->
@include('partials.menu')

<!-- Cont -->
<section id="content-wrapper">
    @include('ajax.index')
</section>

<!-- Footer -->
@include('partials.footer')

<!-- Javascript -->
@include('partials.javascript')

</body>
</html>

My Controller (if there's an ajax call i retrieve the view without @extends() and @section(), otherwise i retrieve my full view):

// load page
public function loadPage($page){
    return (\Request::ajax()) ? view('ajax.'.$page)->render() : view('pages.'.$page);
}

My views:

For my purpose i've created 2 type of views, one extended and one with only the html i need from my ajax calls.

a) extended, it's inside "pages" folder:

@extends('main')
@section('cont')

@include('ajax.shop')

@stop

b) only html, for ajax calls, inside "ajax" folder:

<div class="content">
    <div class="container-fluid">
        <div class="row page-title-box">
            <h3 class="page-number">N. 67</h3>
            <h1 class="page-title">Shop</h1>
        </div>
    </div>
</div>

I don't understand where to put my javascript for this view if i need to implement an animated filter gallery. I've tried to put javascript inside the view but my app crashed.



via Chebli Mohamed

how force locale set in user settings overrule locale detected by browser - laravel

Users of my app can set up their preferred locale in their settings panel. In my case it is pl. However, when I am using the app as a logged user on a browser which preference of locales does not contain pl or has it below en, I see english translations.

My problem: how make the browser observe the locale set by user? Or... how to force the app to switch to the user-preferred locale?



via Chebli Mohamed

How can I add extra condition in laravel authentication?

I am wondering how can I add extra condition like active in laravel authentication that comes with laravel? I could do that by doing authentication manually but if is there any possibilities to do it with default auth if I can say that ?



via Chebli Mohamed

Relationship Has Many Through without firstKey

is there anyway to get result from hasManythrough relationship without the firstKey?

Right now my result is:

 return $this->hasManyThrough('App\Facility', 'App\MerchantBranchFacility', 'id,'facility_id')->select('name');

    {"facilities":[{"name":"AC","id":"13"},{"name":"Wi-Fi","id":"13"}}}

I realize that inside hasmanythrough method laravel always do this:

  return array_merge($columns, [$this->parent->getTable().'.'.$this->firstKey]);

How can i remove "id" without foreach?



via Chebli Mohamed