jeudi 1 novembre 2018

Getting data from two separate models in laravel

i wanted to get data which is related to a id and i used find($id) method to get those data, now i wanna get data from two tables which having one to many relationship how can i get data which is related to same id from two table i try to this way but it wasn't worked.

enter code here

public function show($id) {

    $post=Clients::find($id);

    return view('pet.shw',['post'=>$post,'pets'=>$post->pets]);

}



via Chebli Mohamed

Laravel querying to get the sum of a colon for each month

I am working in a query to calculate the sum of a column for each month and the problem is that when I get the data and one month has no data there is no row for that month and then I don't know which is which month, it would be helpful to return 0 if a months amount is 0, this is the query:

$test = DB::table('actuals')
->Where('company_id',$id)
->Where('account_level_l','LIKE','%Income%')
->select(DB::raw('SUM(amount) as total_amount'))
->groupBy(DB::raw('MONTH(date) ASC'))->get();

and this is the objects that get returned, from 12 months 10 has data:

Collection {#1381 ▼
  #items: array:10 [▼
    0 => {#1370 ▼
      +"total_amount": 133409.46
    }
    1 => {#1371 ▼
      +"total_amount": 77421.36
    }
    2 => {#1372 ▼
      +"total_amount": 78193.81
    }
    3 => {#1373 ▼
      +"total_amount": 940477.66
    }
    4 => {#1374 ▼
      +"total_amount": 81713.1
    }
    5 => {#1375 ▼
      +"total_amount": 64792.43
    }
    6 => {#1376 ▼
      +"total_amount": 85099.41
    }
    7 => {#1377 ▼
      +"total_amount": 127753.07
    }
    8 => {#1378 ▼
      +"total_amount": 62953.11
    }
    9 => {#1379 ▼
      +"total_amount": 356.61
    }
  ]
}



via Chebli Mohamed

How in blade template show params in maintenance-mode?

I want to add maintenance-mode support for my Laravel 5.7 app as written here https://laravel.com/docs/5.7/configuration#maintenance-mode

with command in console :

php artisan down --message="Upgrading Database" --retry=60

using template

resources/views/errors/503.blade.php.

it works, but if there is a way in my console to show values of message and retry params entered in console command?

I tried to show variables with these names, but got error that such variables do not exists...

Thanks!



via Chebli Mohamed

Laravel rule 'not' qualifier working but not firing validation message

As the title says, the validation rule is working, but no error message is triggered. I have tried putting an array of messages in the controller and passing it as the 3rd argument to the validator, but this resulted in none of the other validation messages displaying either. The validator is failing when an url is entered, but the user is not given any feedback. I would rather avoid writing a custom validation rule, as laravel already has the functionality for checking urls and negating rules.

I have read through the following answers, and also the laravel website.

Laravel validator::make vs this->validate()

Custom message laravel Validation

Laravel Validation Rule array not working

https://laravel.com/docs/5.7/validation

MyController.php :

protected $rules = [
    'title' => 'filled|max:80|!url',
];

resources/lang/en/validation.php :

'custom' => [
    'attribute-name' => [
        'rule-name' => 'custom-message',
    ],
    'title' => [
        '!url' => 'The :attribute cannot be an URL.',
    ],
],

Any and all help is welcomed and appreciated.



via Chebli Mohamed

How to swap two indexed fields with laravel migrations?

In the Models of a many to many relationship I have accidentally identified the foreign key names in reverse. This is done in both related Models so the relationship works. It's in production.

In Articles:

public function categories()
{
    return $this->belongsToMany(ArticleCategory::class, 'article_category_article', 'article_category_id', 'article_id');
}

and in ArticleCategory:

public function articles()
{
    return $this->belongsToMany(Article::class, 'article_category_article', 'article_id', 'article_category_id');
}

As you can see, both foreign keys are reversed.

It doesn't bother me because it works throughout the project. In the article_category_article table both values are recorded in the 'wrong' column.

But what if I'd like to swap it anyway. The Models are easy, but what about the pivot table? I have tried with a laravel migration:

public function up()
{
    Schema::table('article_category_article', function (Blueprint $table) {
        $table->renameColumn('article_id', 'temporarily');
        $table->renameColumn('article_category_id', 'article_id');
        $table->renameColumn('temporarily', 'article_category_id');
    });
}

without success, it predictably runs into the error There is no column with name 'temporarily' on table 'article_category_article'

Splitting it up in 2 migration files ran into the same error.

I have the tendency to let it be. The question is: can it be done? I presume swapping the columns inside MySQL (without migrations), re-index the tables and adapt the Models is a possibility. Any ideas? I can test it out on a local server.



via Chebli Mohamed

Globally catch ModelNotFoundException on Laravel 5

I am trying to catch the ModelNotFoundException globally from the app\Exceptions\Handler class, so to not have to check for it on every single controller.

But it does not work, although it works fine from inside a controller:

try {

        $asset = Asset::findOrFail($asset_id);

    } catch (Exception $e) {

        if ($e instanceof ModelNotFoundException)
        {
            $model = explode('\\', $e->getModel());
            return $this->respondWithRecordNotFound(end($model) . ' record not found');
        }

        return $this->respondWithGeneralError($e->getMessage());
    }



via Chebli Mohamed

Not working bootstrap navbar with in Laravel 5.6

in my laravel application I have sidebar menu like following,

<ul class="nav navbar-nav">
      <li class="">
      <a href="">Home</a></li>

      <li class="">
      <a href="">Page 1</a></li>
      <li><a href="#">Page 2</a></li>
      <li><a href="#">Page 3</a></li>
    </ul>

this is working with Helper.php file in app/Helper/Helper.php3

<?php

if(! function_exists('active_menu')) {
    function active_menu($currentRouteName, $requestName, $start, $finish){
        if (substr($currentRouteName,$start, $finish) == $requestName){
            return 'active';

        }else{
            return null;
        }
    }
    }

this is working fine, but now I need change above sidemenu bar class as following,

<ul class="nav nav-sidebar">

only this change but now it is not working. how can I fix this problem?



via Chebli Mohamed