B
B
Billy Milligan2015-11-01 23:39:20
Laravel
Billy Milligan, 2015-11-01 23:39:20

How to use your variables and functions in the view in laravel?

On the page, you need to display words with adequate endings depending on the number. Such words are enough.
How to do it right? I just started learning laravel, it's not very clear where to put the logic.

public static $time_hour = array('час', 'часа', 'часов');
    public static $time_minute = array('минута', 'минуты', 'минут');
    /*
     * остальные слова...
    */

    public static function getNumEnding($number, $ends)
    {
        if($number == 0) {
            return $ends[0];
        }

        if ($number < 1) {
            if ($number < 0.3) {
                return '&#188; '.$ends[1];
            }
            if ($number > 0.6) {
                return '&#190; '.$ends[1];
            }
            return '&#189; '.$ends[1];
        }

        $i = ceil($number) % 1000;

        if ($i >= 11 && $i <= 19) {
            return $number.' '.$ends[2];
        }

        switch ($i) {
            case (1):
                return $number.' '.$ends[0];
            case (2):
            case (3):
            case (4):
                return $number.' '.$ends[1];
            default:
                return $number.' '.$ends[2];
        }
    }

    echo getNumEnding(5, $time_hour); //5 часов
    echo getNumEnding(3, $time_minute);//3 минуты

I tried to describe the function and variables in the controller, but I can’t call it in the view (an error, it’s understandable).

Answer the question

In order to leave comments, you need to log in

5 answer(s)
F
FMars, 2015-11-02
@Billy_Milligan

If Laravel 5.1 then:
laravel.com/docs/5.1/blade#service-injection
How do I do.
1. We create a class with our functionality. For example in a folderapp

// app/Some.php

namespace App;

class Some {
    public function getValue() {
        return "VALUE";
    }
}

2. We register the class in the service provider. app/Providers/AppServiceProvider.php.
Enter in the method register:
public function register()
    {
        $this->app->bind('Some', function($app)
        {
            return new \App\Some;
        });
    }
}

3. In the windows we use the following construction:
@inject('some', 'Some')

<div>
    Value: {{ $some->getValue() }}.
</div>

Y
yogev_ezra, 2013-08-02
@yogev_ezra

I have a similar configuration at work, Windows7 64bit operating system, JavaEE development on Intellij IDEA, a WebLogic server running locally, and it's also excruciatingly painful. Increased the memory to 6GB - did not help. I asked to upgrade the processor - they are greedy. In general, you have to pay for the speed of development and cross-platform with the speed of the work itself. Therefore, my hobby works as fast as possible :-)

M
Maxim Klyushkov, 2013-08-02
@m_klyushkov

Do you have a processor, memory or disk rests? Analyze and understand for yourself what you need: more memory, ssd or change the entire configuration.

V
Vladimir Smirnov, 2013-08-09
@bobzer

I have been developing JEE applications for 10 years, and the configuration has always been weaker. At the moment, I’ve been developing on a laptop for 3 years at work, I don’t remember exactly which processor (I’m writing now from home), before the i3 / 5/7 line it was produced, it seems Core 2. 3 GB of memory. I run the Idea environment, a couple of JBoss server instances, an httpd web server, and I load it all in SOAPUi - tens of thousands of requests through web services, and everything works quite well for itself. While the load test is in progress, I work in Idea without any problems.
Home i3 3000 GHz, 4 GB memory. MySQL is installed, there are about 300 thousand records in the database now (an unpacked dump weighs half a gigabyte). There is no application server. I launch my software from Idea, which works quite intensively with the base, I myself continue to program and launch other developments - there are no hints of brakes at all.
Follow the path of exceptions: stop all running components one by one (server, development environment, etc.) until you notice that the computer “breathed deeply”, then you will understand where the problem is. In the end, the task manager clearly shows who is eating memory or processor. There is also a good chance that JEE has nothing to do with it at all, and the problem is in faulty hardware or a crippled OS.

R
rozhik, 2013-08-04
@rozhik

3.5 GB is not enough. Win for Unix is ​​just top.
Add memory, put 64bit axis. Should help a lot. If it's still not enough, then SSD.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question