vendredi 24 avril 2020

Laravel filter array based on element value

I have the following array that I need to filter it and get just the elements which have type = 1

array:5 [
  0 => array:3 [
    "id" => 1
    "name" => "Agua Corriente"
    "type" => 1
  ]
  1 => array:3 [
    "id" => 2
    "name" => "Cloaca"
    "type" => 1
  ]
  2 => array:3 [
    "id" => 3
    "name" => "Gas Natural"
    "type" => 2
  ]
  3 => array:3 [
    "id" => 4
    "name" => "Internet"
    "type" => 3
  ]
  4 => array:3 [
    "id" => 5
    "name" => "Electricidad"
    "type" => 3
  ]
]

This is the expected result:

array:2 [
  0 => array:3 [
    "id" => 1
    "name" => "Agua Corriente"
    "type" => 1
  ]
  1 => array:3 [
    "id" => 2
    "name" => "Cloaca"
    "type" => 1
  ]
]

I'm trying to solve it with Arr::where helper but I'm not getting the expected result. Does anyone can help me?

Regards



via Chebli Mohamed

how i can use like query in aes_encryption in laravel

How i can use like search query in Aes_encryption data in laravel .my data in tables is in AES Encryption form . i try this code but its not work well. please help me. Thanks in Advance.

     public static function search($type, $value, $limit = 12){
        $key = self::encryption_key;
        if($type == "name"){
           $valuess =  DB::raw("AES_ENCRYPT('$value',$key)");
            $values = Crypt::encrypt($valuess);
        }elseif($type == "phone"){
            $valuess =  DB::raw("AES_ENCRYPT('$value',$key)");
            $values = Crypt::encrypt($valuess);
        }elseif($type == "email"){
            $valuess =  DB::raw("AES_ENCRYPT('$value',$key)");
            $values = Crypt::encrypt($valuess);
        }elseif($type == "tag"){
            $values = $value;
        }else{
            $valuess =  DB::raw("AES_ENCRYPT('$value',$key)");
            $values = Crypt::encrypt($valuess);
        }
        $types = Customer::getTranslatedType($type);
        $user_id = Auth::user()->id;
        $data = DB::table("user_customers")->select(DB::raw("customers.status, customers.id, user_customers.tag as tag, SUM(CASE WHEN uploads.views=0 AND folders.isPrivate = 0 THEN 1 ELSE 0 END) as unread, 
        SUM(CASE WHEN uploads.status=1 AND folders.isPrivate = 0 THEN 1 ELSE 0 END) as unprocessed, companies.id as company_id ," .(new static)->columnsToAESDecryptHelper(array("companies.name" => "name",  "companies.email" => "email", "companies.phone" => "phone", "companies.address" => "address"))))
            ->join("customers", "customers.id", "user_customers.customer_id")
            ->join("users", "user_customers.user_id", "users.id")
            ->leftjoin("folders", "users.id", "folders.user_id")
            ->leftjoin("uploads", "folders.id" , DB::raw("uploads.folder_id AND customers.id = uploads.customer_id"))
            ->leftjoin("companies", "user_customers.company_id", "companies.id")
            ->where("user_customers.user_id", $user_id)
            ->where($types, 'LIKE',"%{$values}%")
//            ->groupBy(DB::raw("customers.id , customers.user_id"))
            ->groupBy(DB::raw("companies.id"))
            ->orderBy('customers.id','desc')
            ->paginate($limit);
       // dd($data);
        return $data;


via Chebli Mohamed

Laravel One to one relasionship showing property non object

I am trying basic laravel one to one relationship but there is an error. I have a table called posts(i will attach the image) and a Model Post.php. When I am trying to get the result from Route.php it is showing an error.MySql tableError message User.php

public function post()
{
    return $this->hasOne('App\Post');
}

Route.php

Route::get('/user/post', function () {
 $post = User::find(1)->post;
return $post;

});



via Chebli Mohamed

Remove parameters in the url in the Laravel controller

I am currently working with an existing project that is for maintenance. So per testing, there's a scenario that is having an error

Request-URI Too Large

The requested URL's length exceeds the capacity limit for this server.

This is error happens because our clients uses the GET method, and I don't wanna change their settings since there are other complex logic that might be affected. So I tried the approach to change the URL using javascript but still the same error. Now what I'm thinking, is it possible for us to change the URL in the controller? Like use the url current path:

url()->current();

rather than the fullpath ?

url()->full();

I badly need your help on this one, I'm stuck on this part for days already.



via Chebli Mohamed

Dynamic If else statement, value for condition is from database

is there a way to make this if else condition:

$searchUserTypeName = UserType::findOrFail(1);

if ($searchUserTypeName->name == "Captain" || $searchUserTypeName->name == "Member") {
  echo "This is Captain or Member"
} else if ($searchUserTypeName->name == "Mentor" || $searchUserTypeName->name == "Mentee") {
  echo "This is Mentor or Mentee"
}

to dynamic?

The value Captain, Member, Mentor and Mentee is from a mysql table

hope you can help

thanks



via Chebli Mohamed

Missing required parameters for route issue in laravel

In my laravel based application I have following link in my admin.blade.php

<ul class="nav nav-treeview">
              <li class="nav-item">
                <a href="" class="nav-link">
                  <i class="far fa-circle nav-icon"></i>
                  <p></p>
                </a>
              </li>

In my project I have another blade called, create.blade.php which is in the following path

views/cms/home/create.blade.php

I have a controller called, CmsHomeController.php for that blade

In CmsHomeController I have a method called create

public function create()
    {

        return view('cms.home.create'); 
    }

Once the user clicks on the above mentioned link in the admin.blade.php, user should go to the create.blade.php blade.

And in my web.php I have registered my route as follows,

Route::resource('cms.home','CmsHomeController');

But now the issue is,

When I click on that link in admin blade, I'm getting an error saying

Facade\Ignition\Exceptions\ViewException
Missing required parameters for [Route: cms.home.create] [URI: cms/{cm}/home/create]. (View: C:\xampp\htdocs\mylaravelproject\resources\views\layouts\admin.blade.php) 

In create.blade.php , I'm having just a simple form

Where am I doing wrong and what would be the correct fix?

UPDATE:

I tried running

php artisan route:list

This is what I got

enter image description here

I don't have such a param called, 'cm'..



via Chebli Mohamed

jeudi 23 avril 2020

Expected status code 200 but received 302. Failed asserting that false is true. using phpunit test in laravel 5.4

Hi i have this page which is /dno-personal/cebu-properties. I tried to run in using PHPUNIT test in my laravel. Now i created this test file below

<?php

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Http\Response;


class DnoPersonalTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @test
     */
    public function add_cebu_properties_page()
    {


        $response = $this->get('/dno-personal/cebu-properties');
        $response->assertStatus(200);

    }
}

Now in my route file i i did not create a route for dno-personal/cebu-properties yet which i run test into my phpunit it throws an error of

Expected status code 200 but received 404.
Failed asserting that false is true.

C:\xampp\htdocs\dnogroup\vendor\laravel\framework\src\Illuminate\Foundation\Testing\TestResponse.php:79
C:\xampp\htdocs\dnogroup\tests\Feature\DnoPersonalTest.php:24

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.

which i think is okay cause i have no route yet which throws an error of 404. Now when i add into the route

Route::get('/dno-personal/cebu-properties',
        'DnoPersonalController@cebuProperties')
        ->name('dno-personal.cebuProperties');

without a method into my controller cebuProperties when i run test PHPUNIT it throws

Expected status code 200 but received 302.
Failed asserting that false is true.

C:\xampp\htdocs\dnogroup\vendor\laravel\framework\src\Illuminate\Foundation\Testing\TestResponse.php:79
C:\xampp\htdocs\dnogroup\tests\Feature\DnoPersonalTest.php:24

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.

it throws an error of 302. Now i want that it throws the method is not yet created instead of 302. now when i add $this->withoutExceptionHandling(); this throws me an error

PHP Fatal error:  Call to undefined method Tests\Feature\DnoPersonalTest::withoutExceptionHandling() in C:\xampp\htdocs\dnogroup\tests\Feature\DnoPersonalTest.php on line 22

In DnoPersonalTest.php line 22:

  Call to undefined method Tests\Feature\DnoPersonalTest::withoutExceptionHandling()



Fatal error: Call to undefined method Tests\Feature\DnoPersonalTest::withoutExceptionHandling() in C:\xampp\htdocs\dnogroup\tests\Feature\DnoPersonalTest.php on line 22

it cannot see the $this->withoutExceptionHandling(); can someone help me figured this out? Any help is muchly appreciated. TIA



via Chebli Mohamed