lundi 2 novembre 2015

Laravel queue (iron.io) keeps sending same email over and over

I am using the Iron.io API with Laravel 5.1. It sends out emails fine. However, it seems to be sending the same message over and over again. Any idea why this might happen?

The code I am using is:

Mail::queue([], [], function ($message) use ($template, $order, $filename) {
    $message
    ->to($order->email)
    ->subject($template->subject)
    ->setBody(DbView::make($template)->with($order->toArray())->render(), 'text/html');

    $message->attach(storage_path('exports/'.$filename));
});



via Chebli Mohamed

Running laravel migration on heroku server

I have a production server, and i have deployed the code into the server, but when i try to migrate the databse with this command :

heroku run php artisan

This what i have returned

Running php artisan on [app-name] ... up, run.3899
bash: php: command not found

And the BUILDPACK_URL in the Config Vars set to

http://ift.tt/Mdu4oW

any help please :) ?



via Chebli Mohamed

dimanche 1 novembre 2015

Laravel 5 multiple select() in query builder

In laravel 5.1, I want to do like

$myTable = MyTable->select('firstField');

if ($somethingTrue) {
    $myTable->select('secondField');
}

$myTable->get();

where I want both firstField & secondField to be selected.
Of course the code above is not working, i.e. only the secondField is selected in the end.

Is there any function provided by query builder class that can do exactly what I want?



via Chebli Mohamed

Laravel 5 middleware not working for group of routes

I am appling cors middleware to access json from some other domain.

This code works just fine :-

Route::get('api/authenticate', ['middleware' => 'cors', function() {
    return Response::json(array('name' => 'Steve Jobs', 'state' => 'California'));
}]);

But when I apply it to :-

Route::group(['prefix' => 'api', 'middleware' => 'cors'], function()
{
    Route::resource('authenticate', 'AuthenticateController', ['only' => ['index']]);
    Route::post('authenticate', 'AuthenticateController@authenticate');
});

it gives me error in console,

No 'Access-Control-Allow-Origin' header is present on the requested resource

I have a have added Cors middleware, cors.php :-

class Cors
{
/**
 * Handle an incoming request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Closure  $next
 * @return mixed
 */
public function handle($request, Closure $next)
{
    return $next($request)->header('Access-Control-Allow-Origin', '*')->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
}
}

Please help. Thanks.



via Chebli Mohamed

get the latest 8 records laravel 5.1

How can i get the latest 8 votes on this query by mobile number field:

results = DB::table('votes')->whereIn('votes.id', function($query){
                    $query->select(DB::raw('MAX(id) as id'))
                    ->from('votes')
                    ->groupBy('mobile_number', 'position_id','code');
                })->where(array('code'=>$code,'position_id'=>$positionId))->get();

on this query it gets the latest total votes of the users but not limited to 8.

->where(array('code'=>$code,'position_id'=>$positionId))->take(8)->get(); //it seems this is not working

here's the sample table

id  mobile_number   code    position_id
1   123123          1       1
2   123123          1       1
3   321212          2       2
4   123123          1       1
5   123123          1       1
6   123123          1       1
7   123123          1       1
8   123123          1       1
9   123123          1       1
10  123123          1       1

if i get the votes of 123123 it will get only the latest 8 so on the above table the Id 1 will not be included in the results.

Any idea? Thanks



via Chebli Mohamed

Simple websocket implementation in laravel 5

I need to implement very simple and very basic websocket in Laravel to implement data synchronization process between my phonegap app as client and my Laravel Website as server. I followed this tutorial http://ift.tt/1WsHyKP to implement and test websocket and it works. Like this one i need very simple laravel implementation where i can call my controller method from js client. Client will be my phonegap application. I found some packages for websocket in laravel with tutorials, but i found difficult to implement them. No one was interacting with controllers, they were listening to events and creating classes here and there but not in controllers. I have written all my logic in Controller and tested it with ajax request but now i will implement it by websocket because i need bidirectional communication to implement Synchronization process. I am new to Laravel so please provide me some help.



via Chebli Mohamed

How to get PUT data in laravel post using Curl

I'm trying to recieve PUT data sent using command line using curl.

curl -X PUT -H "Content-Type: application/json" -d '{"name":"Billy Bowler"}' -H "Authorization: BASIC <auth-token>" http://localhost/Bowling/public/api/leagues/1/bowlers

[auth-token] is a valid base64Encode(email + ":" + password).

I'm using Laravel 5, is there any way to get the data passed in routes/controller?

Right now I have tried with these:

Input::json->all();

Request::json()->all();  

file_get_contents("php://input");

Any help would be appreciated. Thanks.



via Chebli Mohamed