jeudi 13 juillet 2017

How can I retrieve the information I want using MySQL `joins` or Laravel `relationships`?

I am working on a project using the Laravel framework. In this project I have three tables:


1) Master Part Numbers (master_part_numbers)

Columns: id, part_number

Values: 1, MS26778-042


2) Inventory (inventory)

Columns: id, master_part_number, stock_qty

Values: 1, 1, 7


3) Inventory Min Maxes (inventory_min_maxes)

Columns: id, master_part_number, min_qty

Values: 1, 1, 10


I am trying to find the inventory where the stock level is below the min_qty. I have been attempting this using joins, like so:

$test = MasterPartNumber::table('master_part_numbers')
                            ->join('inventory', 'master_part_numbers.id', '=', 'inventory.master_part_number_id')
                            ->join('inventory_min_maxes', 'master_part_numbers.id', '=', 'inventory_min_maxes.master_part_number_id')
                            ->select('master_part_numbers.part_number')
                            ->where('inventory.stock_qty', '<=', 'inventory_min_maxes.min_qty')
                            ->get();

However I am getting an empty collection every time. I have tried removing the where() clause and I get all the part numbers in the inventory, so it feels like I'm on the right track, but missing a critical component.

Also, I don't know if there is an easier or more efficient way to do this using Laravel's Eloquent Relationships, but that option is available.

Note: I added the space after table('master_part_numbers') in my query displayed here on purpose, for readability.



via Chebli Mohamed

laravel upload files in many inputs

I'm trying to upload files in 4 inputs files i get the solution from here but the problem the last file4 input file uploaded in all fields in database

in my blade form

{!! Form::file('file1', null,['class'=>'form-control']) !!}
{!! Form::file('file2', null,['class'=>'form-control']) !!}
{!! Form::file('file3', null,['class'=>'form-control']) !!}
{!! Form::file('file4', null,['class'=>'form-control']) !!}

in my controller

$input = $request->all();
    $files =[];
    if ($request->file('file1')) $files[] = $request->file('file1');
    if ($request->file('file2')) $files[] = $request->file('file2');
    if ($request->file('file3')) $files[] = $request->file('file3');
    if ($request->file('file4')) $files[] = $request->file('file4');
    foreach ($files as $file)
    {
        if(!empty($file)){
            $destinationPath = public_path() . '/uploads';
            $filename = $file->getClientOriginalName();
            $file->move($destinationPath, $filename);
        }

    }
    $model = new Project($input);
    $model -> file1 = $filename;
    $model -> file2 = $filename;
    $model -> file3 = $filename;
    $model -> file4 = $filename;
    $model->save();



via Chebli Mohamed

mercredi 12 juillet 2017

Laravel delete related data?

If have a system that works like this.

There is a theme, Within a theme, are many topics and in those topics are many replies. To delete a topic you have to delete the replies that are related to that topic too. So I made that with the following code

$topic = Topic::find($id);

$topic->replies()->delete();

$topic->delete();

return redirect('/');

But now I'm trying to delete a theme. Which has to delete every topic that is related to that theme and every reply that is related to every topic within that theme. But how do I do this? I figured it may be something like this?

$theme = Theme::find($id);
$theme->topics()->delete();
$theme->topics()->replies()->delete()
$theme->delete();

return redirect('/');

But that doesn't work. How can I make this work? Thanks in advance



via Chebli Mohamed

database connection error while connecting database in live server error 1045

my .env file setup like this on the live server. but while I am

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=onlyaruv_notepad-apps
DB_USERNAME=onlyaruv_notepad
DB_PASSWORD=mypassword

the error was

SQLSTATE[28000] [1045] Access denied for user 'onlyaruv_notepad'@'127.0.0.1' (using password: YES) (SQL: select * from `users` where `email` = ih76632@gmail.com limit 1)

I access my ssh via putty using this host and password http://ift.tt/2tLULbM I am confused .



via Chebli Mohamed

mardi 11 juillet 2017

Proxy url routing in Laravel

I have been recently handed a project which has a very weird proxy routing setup in it. Basically the setup is that they have the app setup and hosted on an AWS instance which is pointed to by a domain say project.example.com. They have set this project.example.com to as their APP_URL in the env file and this all works like its supposed to. Then there comes the proxy url into play, which is basically a domain like http://ift.tt/2ub4x9y, this subfolder routing actually points to the same domain and codebase that I mentioned earlier, project.example.com. I don't actually know much about the setup of the http://ift.tt/2ub4x9y domain that is pointing to the actual codebase domain but whenever we hit it we are routed correctly.

On the laravel side what the previous devs have done is basically defined a new variable say PROXY_URL in the env file and set this as http://ift.tt/2ub4x9y and on the top of the routes.php file they do a forceRootUrl to basically set the base/app url to what has been defined in the PROXY_URL. Now I and the app are having problems due to this weird kinda routing as devs were reporting me issues previously due to urls not being made properly. The current problem I'm having is that a package for laravel to handle grid listings which is being used here, Nayjest, makes the grid properly but inside the table listing it messes up all the urls it has made, basically the urls are correct but the base urls are set in it as the old APP_URL, like http://ift.tt/2v9r9V5 when it should've been http://ift.tt/2ub4B9i. The reason I found for this was that the package was using the base url from the Request object of laravel, which on the runtime checks the servers hostname which is set to of course project.example.com, so basically not using the Laravel's Url() method to fetch the base/app url (which has now been over ridden due to forceRootUrl). Now the solution could be to ask the package's dev to change it to use Url() method, but I have seen the use of this Request object's base url in core laravel code as well (there must be a reason they are doing it which I would like to know as well).

My questions are many here, firstly this approach to handle the proxy domain, is this correct or is there a better one ? Secondly if one is using this approach how can we ask or tell the package to use the Url() method instead of the Request object's getSchemeAndHttpHost without changing the source of the package(a hack basically in our code). Thirdly are we looking straight at the problem coz I kept thinking that instead of correcting the base url in the grid listing we can keep it as it is and handle the routing of it along with query parameters correctly so we reach say from this http://ift.tt/2v9r9V5 to this http://ift.tt/2ub4B9i correctly.

Any help would be greatly appreciated, thanks.



via Chebli Mohamed

Larave Dotenv on beanstalkd Queue

I have three dot env, default .env for example.com, .env.sub1 for sub1.example.com and .env.queue2 for sub2.example.com

I am able to load different .env when I bootstrap using the following code in bootstrap/app.php. This works really well when I access the websites.

$env = $app->detectEnvironment(function () {
    $environmentPath = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR;
    $environmentFile = null;
    switch (getenv('HTTP_HOST')) {
        case 'sub1.example.com':
            $environmentFile = '.env.sub1';
            break;
        case 'sub2.example.com':
            $environmentFile = '.env.sub2';
            break;
    }
    if (!empty($environmentFile) && file_exists($environmentPath . $environmentFile)) {
        Dotenv::load($environmentPath, $environmentFile);
    } else {
            // .env will be in affect, do nothing
    }
});

This also works really well when your QUEUE_DRIVER is set to sync.

But the problem when my QUEUE_DRIVER is set to beanstalkd it take the environment variables always from .env.

Where is beanstalkd queue loading the .env while executing a queued job.

Thanks,

K



via Chebli Mohamed

Angularjs laravel 5 posting form data using post method 404 page not found error. But get method works

Using Angularjs laravel 5.1 posting form data using post method 404 error comes but get method works fine

controller

`(function (ng, app) { app.controller('app.promocodeCtrl', function($scope,$http) { // create a message to display in our view

    $scope.newPromocode = function(){


        $scope.onSubmit = function(){
            console.log($scope.form);
            // $http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
            //$http.defaults.headers.post["Content-Type"] = "text/plain";
            var request = $http({
                method: "POST",
                url: "backend/statistics/promo",
                data: {
                    promocode : $scope.form.codetxt,
                    discount : $scope.form.discount,
                    minvalue : $scope.form.minvalue
                },
                headers: {'Content-Type': 'application/x-www-form-urlencoded'}
            }).success(function (data) {
                alert(data);
            });
        }

    };

});

})(angular, angular.module('app'));`

route

Route::post('statistics/promo', ['uses' => 'Api\Backend\Statistics@promoCode']);



via Chebli Mohamed