vendredi 27 novembre 2015

Laravel 5.1 suggested relationship setup

I am building a timesheet system and have setup a model for timesheets. Timesheet can have many rows - for example when I add a timesheet, I can add many days (rows) to the timesheet.

I want to be able to sync rows when a timesheet gets saved. For example, new rows will be added to the database, missing rows from the given array will be removed from the database.

I understand I can use sync method which works like this, however, I do not think I need a belongsToMany relationship. Currently I have my row relationship setup as a hasMany. The timesheet model looks like this:

<?php

namespace App\Models\Timesheet;

use Illuminate\Database\Eloquent\Model;

class Timesheet extends Model
{    
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'timesheet';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['user_id', 'week', 'year', 'token', 'total_hours'];

    /**
     * Define that we want to include timestamps.
     *
     * @var boolean
     */
    public $timestamps = true;

    /**
     * Boot the model.
     *
     */
    public static function boot()
    {
        parent::boot();

        static::deleting(function($timesheet)
        {
            $timesheet->row()->delete();
        });
    }

    /**
     * The rows that belong to the timesheet.
     *
     * @return Object
     */
    public function row()
    {
        return $this->hasMany('App\Models\Timesheet\RowTimesheet');
    }

}

The row_timesheet model looks like this:

namespace App\Models\Timesheet;

use Illuminate\Database\Eloquent\Model;

class RowTimesheet extends Model
{    
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'row_timesheet';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['timesheet_id', 'activity_category', 'description', 'eri_number', 'ewn_number'];

    /**
     * Define that we want to include timestamps.
     *
     * @var boolean
     */
    public $timestamps = true;

What do I need to do in order to make something like this work:

$this->timesheet->find($id)->row()->sync($data);

Thanks in advance.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1SnCkz3
via IFTTT

Php global variable to param-function

I am can't use variable $data in inner function:

$data = array(...);
Excel::create('Filename',function($excel){
  foreach($data as $v){
     //...
  }
});

I get error: "Undefined variable: $data"

Also:

 $data = array(...);
 Excel::create('Filename',function($excel){
   global $data;
   foreach($data as $v){
      //...
   }
 });

I get error "Invalid argument supplied for foreach()"

How I can use $data in function?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1Imlxqu
via IFTTT

PHP -> NumberFormatter not working

I have php file test.php:

use App\Classes\test_class;

$xml = new DOMDocument( "1.0", "ISO-8859-15" );
echo 'DOMDocument-ok<br/>';

$formatter = new NumberFormatter('en_US', NumberFormatter::DECIMAL);
echo 'NumberFormatter-ok<br/>';
new test_class();

And test_class.php:

class test_class{

    public function __construct()
    {
        $xml = new \DOMDocument( "1.0", "ISO-8859-15" );
        echo 'DOMDocument-ok<br/>';

        $formatter = new \NumberFormatter('en_US', \NumberFormatter::DECIMAL);
        echo 'NumberFormatter-ok<br/>';
    }

}

When I run this code output is:

DOMDocument-ok
NumberFormatter-ok
DOMDocument-ok
NumberFormatter-ok

But in plugin "sebastian/money" when I use plugin I get this error "Symfony \ Component \ Debug \ Exception \ FatalErrorException (E_UNKNOWN) HELP Class 'NumberFormatter' not found"

For code:

<?php
namespace SebastianBergmann\Money;

use NumberFormatter;

class IntlFormatter implements Formatter
{
    private $numberFormatter;
    public function __construct($locale)
    {
        $this->numberFormatter = new NumberFormatter(
            $locale,
            NumberFormatter::CURRENCY
        );
    }

For line :

$this->numberFormatter = new NumberFormatter(
            $locale,
            NumberFormatter::CURRENCY
        );

EDITED:

Apparently NumberFormatter not working inside all Laravel app, but I don't know why, can someone help?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1Ny2gds
via IFTTT

Laravel 5.1 firstOrNew with relationships

How can I use firstOrNew method with Laravel relationships.

I have the following relationship:

/**
 * The rows that belong to the timesheet.
 *
 * @return Object
 */
public function rowTimesheet()
{
    return $this->hasMany('App\Models\Timesheet\RowTimesheet');
}

I am trying to create a new row for the timesheet in the database using the following. I don't get any errors, but the row doesn't get inserted.

Any ideas?

/**
 * Create timesheet rows.
 *
 * @param $id
 * @param $row
 */
public function createTimesheetRow($id, $row) 
{
    return $this->timesheet->find($id)->rowTimesheet()->firstOrNew($row);
}



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1Q25Lrc
via IFTTT

Laravel 5.1: Get ID from firstOrNew method

I have the following function which creates a new record in the database if one doesn't already exists - if one exists, it updates it. The problem is that it returns true and therefore I can't get the ID of the inserted or updated record.

/**
 * Save timesheet.
 *
 * @param $token
 * @param $data
 */
public function saveTimesheet($token, $data) 
{
    return $this->timesheet->firstOrNew($token)->fill($data)->save();
}



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1Ra6ZSb
via IFTTT

Returning the custom validation method parameter in error message

I have a created a custom validation function along with a custom error message for it. How can I show the value "1000" in my error message?

  // in my request file
  function rules() 
  {
    return [
        'my_field' => 'myValidator:1000',
    ];    
  }

  // in my custom validator file
  public function validateMyValidator($attribute, $value, $parameters)
  {
      return true;
  }

  // in resources/lang/eng/validation.php
  'custom' => [
    'my_field' => [
        'my_validator' => 'Value must be 1000',
    ],
  ]



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1MHDELK
via IFTTT

jeudi 26 novembre 2015

Laravel relation with 2 different models in same relation

I'm trying to do this in Laravel5: I have model order_item with attributes: order_item_id, item_id and item_type. Item_id is ID of related item. And item_type is "product" or "service". Order_item has function item() where I want to put relation with product or service in dependence on item_type. For example:

order_item: item_id = 1; item_type = 'product' => item() should return model App\Models\Product

if order_item is like this:

`order_item: item_id = 1; item_type = 'service' => item() should return model App\Models\Service`

Here is example of my model:

<?php namespace App\Models;

use App\Models\Model;

class OrderItem extends Model
{

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'orders_items';

    protected $primaryKey = 'order_item_id';

    /**
     * The attributes that are mass assignable.
     *
     *  item_type: product | service
     *
     * @var array
     */
    protected $fillable = ['order_item_id', 'order_id', 'item_id', 'item_type'];



    public function order() {
        return $this->hasOne('App\Models\Order', 'order_id', 'order_id');
    }

    public function item() {
    }

}



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1Nxxi54
via IFTTT