lundi 18 juillet 2016

Populating select options dynamically based on previous choices

I am having issues prepopulating select boxes based on an array. Now I am passing my view an array which looks like the following

array:4 [▼
  "folder1" => array:1 [▶]
  "folder2" => array:2 [▶]
  "folder3" => array:1 [▶]
  "folder4" => array:1 [▼
    "product1" => array:1 [▼
      2016 => array:1 [▼
        "March" => array:1 [▼
          0 => "image1.png"
        ]
      ]
    ]
    "product2" => array:1 [▼
      2015 => array:1 [▼
        "June" => array:1 [▼
          0 => "image1.png"
        ]
      ]
    ]
  ]
]

Now for this part I am only interested in folder4. I plan on having 4 select inputs. The first one should display the products. The products are folders within the root of folder4.

So in the example above, this select should show product1 and product2.

The second select should show the years. The years are folders within the products. The third select should show the months. The months are folders within the years folders.

The last select should show the image names.

All of this will probably need to be within the foreach loop. Reason I say this is because if they select product2 in the first select box, the next select box should only have the year 2015, not the year from product1 folder. This is where I am at this moment in time, but it is quite a way off

<select class="selectpicker" name="productType">
    @foreach($fileData as $type => $product)
        @if($type == "folder4")
            @foreach($product as $name => $year)
                <option value=""></option>
            @endforeach
        @endif
    @endforeach
</select>

At the moment I am getting htmlentities() expects parameter 1 to be string, array given. This select should display product1 and product2. Also, have do I make the other select options dynamic based on what has been previously chosen? So if they choose product 1, the next select box should display the year 2016 only.

Any advice appreciated.

Thanks



via Chebli Mohamed

Error using two or more customs service provider - Laravel 5.1

time ago I made a facade to handle emails called 'MyMailer'.

It works good, but now, I need to add a new facade that I called 'Message'.

I made the new facade such as I made the 'MyMailer' facade. Below I will copy the code:

App/Components/MyMailer.php

<?php

namespace App\Components;

use Mail;


class MyMailer{

    public function sendMailForAlert($email, $data, $message){

        Mail::send('emails.newData', ['data' =>$data],         function($message) use ($email){
            $message->to($email)
                ->subject('Sending data');
        });

    }


}

App/Facades/MyMailer.php

<?php

namespace App\Facades;

use Illuminate\Support\Facades\Facade;

class MyMailer extends Facade {

    protected static function getFacadeAccessor()
    {
        return 'build';
    }
}

App/Providers/MyMailerServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class MyMailerServiceProvider extends ServiceProvider

{

    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        \App::bind('build', function()
    {
        return new \App\Components\MyMailer;
    });
    }
}

App/Components/MyMessage.php

<?php

namespace App\Components;


use Illuminate\Http\Request;
use Exception;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class MyMessage{

    public function getMessage()
    {
        return 'hello';
    }
}

App/Facades/MyMessage.php

<?php

namespace App\Facades;

use Illuminate\Support\Facades\Facade;

class MyMessage extends Facade {

    protected static function getFacadeAccessor()
    {
        return 'build';
    }
}

App/Providers/MyMessageServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class MyMessageServiceProvider extends ServiceProvider
{


    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        \App::bind('build', function()
        {
            return new \App\Components\MyMessage;
        });
    }
}

I had registrated the alias and service provider in config/app.php. The problem is that I get an error when I use a facade. Example:

<?php
namespace App\Http\Controllers\Example;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use MyMailer;

class ExampleController extends Controller
{

   public function executeExample(){

      MyMailer::sendMailForAlert('foo@bar.com', 'data', 'new data');


   }


}

I am getting this message: call_user_func_array() expects parameter 1 to be a valid callback, class 'App\Components\MyMessage' does not have a method 'sendMailForAlert'. I don't know why because I am not using 'MyMessage' facade.

Any help is welcome. Thank you



via Chebli Mohamed

dimanche 17 juillet 2016

Laravel :: Best way to update foreign key

I have this migration file

Schema::create('table_one', function(Blueprint $table) 
{ 
    $table->increments('id'); 
    $table->string('name'); 
    $table->integer('table_two_id')->unsigned(); 
    $table->foreign('table_two_id')->references('id')->on('table_two'); 
    $table->timestamps(); 
});

and I want to update to make it ->onDelete('cascade');

$table->foreign('table_two_id')->references('id')->on('table_two')->onDelete('cascade');

What is the best way to do this?

Is there something like ->change();

Thanks



via Chebli Mohamed

Get averages across multiple columns with GroupBy

I want to compile statistics on how users are answering questions for my app. I am using Laravel 5.1 for my API which will handle this.

I have x entries in my questions_user table, and want to first group by question_id, then get the averages per each group for user_timing, and other potential fields.

enter image description here

    $user_questions = DB::table('question_user')
        ->select('question_id', 'user_timing', DB::raw('count(*) as total'))
        ->groupBy('question_id')
        ->get();

Gets me:

[
 {
  "question_id": 1,
  "user_timing": "5",
  "total": 2
 },
 {
  "question_id": 2,
  "user_timing": "10",
  "total": 1
 },
 {
  "question_id": 3,
  "user_timing": "4",
  "total": 3
 }
]

If I add ->avg('user_timing') just before the get(), it gives error:

"Call to a member function get() on double"

I'm assuming this is because avg computes the value and returns it to the get(), which is expecting a query object.

if I add ->avg('user_timing') in place of get(), it returns a single value 4, which is not the average of anything, let alone groups.

How can I return aggregate values across multiple fields in multiple groups?



via Chebli Mohamed

xero api integration in php for a public type application (code conversion)

I want to integrate "XERO API" for public application in PHP. I stuck with Oauth application authorisation I have download code from GITHUB (find on "XERO API" code sample for public application) and i want to convert the public file code in to class PHP



via Chebli Mohamed

These credentials do not match our records. laravel 5.1

hi I'm using Laravel authentication. in my login form if i submit without filling fields i see an error that says the filed is required . but when i try to fill the form with wrong username and password it has to say "These credentials do not match our records." but it wont say
I'm using laravel 5.1



via Chebli Mohamed

samedi 16 juillet 2016

Unable to access object properties from URL params in Laravel

I am trying to access object properties from a request from my Angular app. I am using Laravel 5.1

Angular:

console.log('getQuestionAnswers', params);
return $http({
    method: 'GET',
    url: url + ver + '/questions/checkMany',
    params: {
        'questions[]' : params
    },
    headers: {
        'Content-Type': 'application/json',
        Authorization: 'Bearer ' + $rootScope.access_token
    },
    cache: true
});

Console.log of params:

enter image description here

Laravel:

public function getAnswers(Request $request)
{
    $input = $request->all();

    $question_objs = $input['questions'];

    foreach ($question_objs as $question_answer_object) {
        return $question_answer_object;

enter image description here

Looks like so far so good!


But if I try to access a property within laravel, like question_id:

return $question_answer_object['question_id'];

I get error:

"Illegal string offset 'question_id'

Laravel already parses the JSON, and when I return it, I can see it's an object. Why can't I access the properties? I've also tried json_decode without luck.



via Chebli Mohamed