dimanche 1 mai 2016

Eloquent - biased random selection of a record

I have a model Quote, which has two properties: quote and probability. I want to select a random quote, but quotes with higher probability must be selected more often. For example, if we have

$q1->probability == 0.15

and

$q2->probability == 0.75

the latter must be 5 times more likely to be selected. The following command makes a random quote selection:

$quote = Quote::orderByRaw('RAND()')->first();

But I need the selection to be biased, as it was mentioned above. How to achieve this?



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

How to create a new record without executing a database query?

I have Question and Answer models. The Question hasMany Answers. Following commands run in the php artisan tinker mode invoke a database query for no apparent reason:

$q = new Question;
$q->answers[] = new Answer; // invokes the below query

// the executed query
select * from `answers` where `answers`.`question_id` is null and `answers`.`question_id` is not null

As you see, there is no need for database call whatsoever. How can I prevent it?



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

Consuming API from website

I'm developing a website and REST API which are a part of the same project folder.

I've developed a REST api as below

URL: example.com/login
Type: POST
Request Body:
{
    "credentials": {
        "username":"dd",
        "password":"dd"
    },
    "appInfo": {
        "version": "1.0"
    }
}

In the login method (UserController) I'm fetching the request body as below

json_decode(Request::instance()->getContent());

Now I'm developing a webpage and want to invoke this API from there. I'm using the below code (HomeController)

public function acceptlogin(Request $request) {
    $data = array(
        'credentials'=> array(
            'username'=>$request->email,
            'password'=>$request->pasword
        ),
        'appInfo'=> array(
            'version'=> "1.0"
        )
    );
    $request1 = Request::create('example.com/login', 'POST', $data);
    $response = Route::dispatch($request1);
}

I'm able to invoke the login api from the above code but the below code in the login method returns empty.

json_decode(Request::instance()->getContent());

Can you please suggest on how to invoke a local API (within the same project) in Laravel



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

Pass value into @include by default

Long story short

How can I define a default value for a record in @include? Here is my code:

// view - questions.create
@include('questions.__answer', ['number' => 0]) // <-- 'answer' => null by DEFAULT

// view - questions.edit
@include('questions.__answer', ['number' => $index, 'answer' => $answer])

Additional details

As you see, I have create / edit views for a Question model, which contains multiple possible answers. The html code for a single answer is located in a separate partial view to eliminate code duplication. I need to reuse this view both for answer creation, where new record should be created from scratch, and editing of an existing answer, whose fields should be populated with existing data.

I certainly could pass 'answer' => null explicitly, but I prefer it to be passed implicitly.



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

Deploy laravel on azure sever

My project is developed with larval, then i am trying to use azure for web server.

I am copying folder laravel in C:\inetpub\wwwroot\laravel5. Next I run php artisan serve.

Then I test on http://localhost:8000/index (test on remote websever). It's ok.

But if I access my url for example http://ift.tt/26FXEsy, it say page not found.



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

Foreign key, or no foreign key? Defining Laravel relationships

What is the difference between defining a foreign key VS just creating an integer column named user_id?

$table->integer('user_id')->unsigned()->index();
// vs
$table->foreign('user_id')->references('id')->on('users');

Can they be used interchangeably? What purpose do each one serve? Which is considered a best practice, first or second definition?



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

How to register middlewares in OctoberCMS plugin?

Registering middlewares in Laravel is easy:

simply list the middleware class in the $middleware property of your app/Http/Kernel.php class

or

If you would like to assign middleware to specific routes, you should first assign the middleware a short-hand key in your app/Http/Kernel.php file

But how can this be done in an OctoberCMS plugin? Is the "Routing and initialization" meant to be used in place of Kernel.php to register middlewares? If not, where can a plugin register its own middlewares?



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