mardi 1 août 2017

Laravel 5 Invalid argument supplied for foreach()

I'm trying to do a foreach() inside of one of my controller and I'm running into the error of Invalid argument supplied for foreach() this is my code

$subAdmins = $request->input('subAdmin');

  // Adding subAdmin
  foreach($subAdmins as $subAdmin){
    $linksubAdmin = new LayMinisterEvents;
    $linksubAdmin->user_id = $subAdmin;
    $linksubAdmin->event_id = $recordEvent->id;
    $linksubAdmin->save();
 }

This is the data that is getting sent over 'subAdmins' => '5,6'

at HandleExceptions->handleError(2, 'Invalid argument supplied for 
foreach()', '/home/vagrant/www/project-
api/app/Http/Controllers/EventsController.php', 47, array('request' => 
object(Request), 'response' => object(Response), 'authUser' => 1, 
'recordEvent' => object(Events), 'linkadminLeader' => 
object(AdminEvent), 'subAdmins' => '5,6'))
in EventsController.php (line 47)



via Chebli Mohamed

Laravel5.3:How to use pluck in relationship for bindign Form::select element?

this is my model

class Positions extends Model implements Repository
{
    protected $fillable = ['index_id', 'title', 'description'];
   public function index()
   {
       return $this->belongsTo('TEST\Indices', 'index_id');
   }
    public function getById($id)
    {
        return $this->with('index')->find($id);
    }
}

how to use pluck() in getById function for listing index relationship?



via Chebli Mohamed

extend composer packages without extending each class

I am at the point of creating my first composer package. This package, which would offer the same functionality as an existing package, with mostly some additional functionality specific to my package. Next to my own classes, all of the classes from the Vendor package still need to be accessible, but from my package's namespace. Some Vendor classes will need some adjustments, but nothing major.

Let's look at a simple example. The vendor package has a few classes, Foo and Bar, that live in the namespace Vendor\Package.

<?php

namespace Vendor\Package;

class Foo // class needs adjustments/addition
{
   private $vendorVar;

   public function fooFunction() // function is fine as it is
   {
      return 'Vendor Foo output';
   }

   public function anotherFooFunction() // function should do some additional things
   {
      $this->vendorVar = 'Vendor!';
   }
}

and

<?php

namespace Vendor\Package;

class Bar // class is fine as it is
{
   public function barFunction() // function is fine as it is
   {
      return 'Vendor Bar output';
   }
}

Since I would like to add some additional functionality and change existing once for class Foo, I inherit the vendor one as such:

<?php

namespace Me\MyPackage;

use Vendor\Package\Foo as vendorFoo;

class Foo extends vendorFoo 
{
   private $myVar;

   public function myFooFunction() // additional function to the Foo class
      {
      return 'My Foo output';
   }

   public function anotherFooFunction() // change the functionality for this function
   {
      $this->myVar = 'Me!';
      parent::anotherFooFunction();
   }
}

Now I can do the following:

<?php

use Me\MyPackage\Foo;

$foo = new Foo();

echo $foo->myFooFunction(); // output: 'My Foo output'
echo $foo->fooFunction(); // output: 'Vendor Foo output'
$foo->anotherFooFunction; // will set $vendorVar and $myVar

Since I don't want that the users need to know about the Vendor package, I want them to have access to all other functionality of the Vendor package, but via my own namespace. Basically they need to be able to do something like

<?php

use Me\MyPackage\Bar;

$bar = new Bar();

echo $bar->barFunction(); // output: 'Vendor Bar output'

I assume that I can simply add the following code in my package.

<?php

namespace Me\MyPackage;

use Vendor\Package\Bar as vendorBar;

class Bar extends vendorBar {} // Do I have to add this for each class I want to use from my namespace?

When the Vendor package has many classes and I only want to touch one, this seems overkill to me. Is this the only/best way to go on about this, or are there other/better ways?

Additional information:

  • I am using the Laravel framework, but I guess this shouldn't matter much.
  • Forking doesn't seem like a good idea. When for example small changes in the Vendor package have been made, e.g. a security fix, I would have to rewrite my code. I am going to assume that the class structure of the Vendor package is not going to change very quickly.
  • This would be an extension of the original Vendor package, and therefor it would not be appropriate to add a pull request.
  • I tried to work with a simple example, but for those interested, I would like to extend the laravel\passport package. This question is a followup of the following one: Add or adjust functionality of installed packages
  • Additional question, how do major packages go around this? For example, how does Laravel use the Symphony functionality throughout its framework?


via Chebli Mohamed

Laravel Eloquent Query Builder Performance issue

I creating a wrapper function for Laravel Query Builder for creating dynamic query and not repeating same things. My function is given below

Class Crud{
  public function get($tbl,$field,$where){
     $query = Capsule::table($tbl);
     if($where != null)  $query = $query->where($where);
     $query = $query->select($field);
     return $query;
  }
}

Function call

$crud = new Crud();
$field = ['a','b','c'];
$where = ['x' => 1];
$query = $crud->get('tbl',$field,$where);

Question:

Is this query slow down performence?

Why I see log like this?

Select * from tbl
Select * from tbl where (x = ?)
Select a,b,c from tbl where (x = ?)



via Chebli Mohamed

Amazon Product Origin & Shipping Details

im having issue on getting Amazon Product Origin & Shipping Details from amazon. i am currently using this php library http://ift.tt/1lBjeDz and the amazon product api.

Please see attach image

shipping details and origin



via Chebli Mohamed

Laravel Query retrieves only one pair of values using relationship 'Join' between two tables

Im trying to query products table that displays the following schema in database ->

Table 1 -> products:

Table:

enter image description here

With a relationship with table products_details in model product.php ->

public function productdetails()
    {
        return $this->hasMany('App\Models\ProductDetails', 'productId', 'id');
    }

Table 2 -> products_details table displays many value pair (name / value ) for each product:

Table:

enter image description here

With a relationship with table products in model productdetails.php ->

public function product()
{
    return $this->hasOne('App\Models\Product', 'productSku', 'productId');
}

Using Tinker to query model I run:

App\Models\Product::join('products_details', 'products.productSku', '=', 'products_details.productId')->take(1)->get();

gives result:

Illuminate\Database\Eloquent\Collection {#1787
     all: [
       App\Models\Product {#1779
         id: 1,
         productName: "Samsung - Galaxy Core Prime 4G",
         productSku: "3953367",
         more info...
         created_at: "2017-04-08 18:28:46",
         updated_at: "2017-04-08 18:28:46",
         deleted_at: null,
         productId: 3953367,
         name: "Operating System",
         value: "Android 4.4 KitKat",
       },
     ],
   }

My question is, how can I query results in order to receive all name/value associated to productId, taking into consideration right now I only receive the first pair of values contained at product_details table, but not all the name/values associated with that productId.

     productId: 3953367,
     name: "Operating System",
     value: "Android 4.4 KitKat",

thank you!



via Chebli Mohamed

Laravel with fullcalendar with locale

I have a problem with locale in full Calendar in Laravel. I use "maddhatter/laravel-fullcalendar": "~1.0" and i can't pass locale in FR. If you have any idea, i take it. Thanks

My Controller

class EventController extends Controller
{
    public function index() {
        $events = [];

        $events[] = \Calendar::event(
            'Event One', //event title
            false, //full day event?
            '2017-07-31T1000', //start time (you can also use Carbon instead of DateTime)
            '2017-07-31T1200', //end time (you can also use Carbon instead of DateTime)
            0 //optionally, you can specify an event ID
        );

        $calendar = \Calendar::addEvents($events)
                  ->setOptions([ //set fullcalendar options
            'header' => array('left' => 'prev,next today', 'center' => 'title', 'right' => ''),

            'locale' => 'fr',
            'editable'=> true,
            'navLinks'=> true,
            'selectable'  => true,
            'defaultView' => 'month'
    ])->setCallbacks([ //set fullcalendar callback options (will not be JSON encoded)
        'viewRender' => 'function() {alert("Callbacks!");}'
    ]);  //add an array with addEvents



        return view('agenda', array('calendar' => $calendar));
    }



via Chebli Mohamed