mardi 2 juin 2020

dropdown is populating empty list in laravel 6

Laravel Version 6. PHP 7.4.

I simply wants to populate my dropdown values form database table. Initially, It was returning error "variable undefined" but when I enclosed my code into if condition, my error was gone but drop-down list is empty.

Please suggest where I' m stuck.

Route.php

Route::get('products/qrcodes/basic','niceActionController@getMake');

Controller

<?php

namespace App\Http\Controllers;
use \Illuminate\Http\Request;
use App\NiceAction;
use App\NiceActionLogged;

public function getMake()
{
$records = DB::table('users')->get();
return view('products.qrcodes.basic', ['records' => $records]);
}

View

<form>
   <select  required>
     @if ((empty($records)))
        Whoops! Something went wrong
     @else
     @foreach ($records as $item)
     <option value=""></option>
     @endforeach
     @endif
   </select>
</form>

I also tried "dd($records);" but nothing happens.



via Chebli Mohamed

get the value based on two fields form single table

I have a table user with below fields .

 id user_name   team       onsite_offshore 
 1   xxx        US East     offshore
 2   YYY        US East     onsite
 3   zzz        US East     onsite
 4   aaa        Us West     onsite
 5   bbb        US West     offshore

I have two dependency dropdown geo and location with geo values 'US East' and 'US West' and location value 'US East and premisis' for'US East'

'US West and premisis' for 'US West dropdown'.

geo Location
US East - US East,offshore(display as 2 options one by one) US West - US West and offshore(display as 2 options one by one)

  on choosing US East in localtion  with geo 'US East' dropdow I want to load the value 'yyy,zzz' which is not inluded in offshore(which is in same table with different column.)
       on choosing offshore in localtion dropdown for geo 'US EASt'I want to load the value 'XXX' which is should inluded in offshore(which is in same table with different column.)

  on choosing US West in localtion  with geo 'US West' dropdown I want to load the value 'aaa' which is not inluded in offshore(which is in same table with different column.)
       on choosing offshore in localtion dropdown for geo 'US West' I want to load the value 'bbb' which is should inluded in offshore(which is in same table with different column.)

$filter_team_details=DB::table('user_master')->leftjoin('user_teams','user_teams.team','=','user_master.geo')->whereIn('user_teams.team',$teams)->groupBy('user_teams.team')->pluck('user_teams.team')->toArray(); (In this query I want to check for onsite_offshore condition in same query )

Is it possible to check both condition single table as



via Chebli Mohamed

Error in laravel multiple union query in foreach loop

I am facing issue in laravel union query in foreach loop.

  $query1 = DB::table('tbl_document_types');
  $query1->where('tbl_document_types.is_app',0);
  $query1->orderBy('tbl_document_types.document_type_order', 'ASC');
  $document_types = $query1->get();

  $length=10;
  $loop=0;

  foreach($document_types as $k => $v)
  {    
    $tbl_documents = 'tbl_documents';
    $document_coloumn = 'tbl_document_column_'. $v->document_type_id;
    $document_type_name = $v->document_type_name; 
    if(Schema::hasTable($document_coloumn))
    {

        $query = DB::table($tbl_documents);
        $query->join($document_coloumn, $document_coloumn . '.' . 'document_id', '=', $tbl_documents . '.' . 'document_id');
        $query->select("$tbl_documents.document_id","$tbl_documents.document_type_id");
        if($loop < 1)
        {
            $union_query = $query;

        }
        else
        {
            //$union_query->select("$tbl_documents.document_id","$tbl_documents.document_type_id");
            $union_query->union($query);
        }
        $loop++;
    }
  }
  $result = $union_query->paginate($length);

I am getting following mysql error

SQLSTATE[21000]: Cardinality violation: 1222 The used SELECT statements have a different number of columns (SQL: 
(select count(*) as aggregate from `tbl_documents` inner join `tbl_document_column_107` on `tbl_document_column_107`.`document_id` = `tbl_documents`.`document_id`) 
union (select `tbl_documents`.`document_id`, `tbl_documents`.`document_type_id` from `tbl_documents` inner join `tbl_document_column_111` on `tbl_document_column_111`.`document_id` = `tbl_documents`.`document_id`) 
union (select `tbl_documents`.`document_id`, `tbl_documents`.`document_type_id` from `tbl_documents` inner join `tbl_document_column_134` on `tbl_document_column_134`.`document_id` = `tbl_documents`.`document_id`) 
union (select `tbl_documents`.`document_id`, `tbl_documents`.`document_type_id` from `tbl_documents` inner join `tbl_document_column_140` on `tbl_document_column_140`.`document_id` = `tbl_documents`.`document_id`) 
union (select `tbl_documents`.`document_id`, `tbl_documents`.`document_type_id` from `tbl_documents` inner join `tbl_document_column_141` on `tbl_document_column_141`.`document_id` = `tbl_documents`.`document_id`))

One "tbl_documents" and multiple "tbl_document_column" for each document type. How to write laravel union query in for each loop?



via Chebli Mohamed

Illuminate \ Database \ QueryException (42S22)

Illuminate \ Database \ QueryException (42S22) SQLSTATE[42S22]: Column not found: 1054 Unknown column 'address' in 'field list' (SQL: insert into users (username, name, last_name, email, contact_number, password, package_id, address, updated_at, created_at) values (A, aki, sidd, a@gmail.com, 84240265241, $2y$10$kouZn3sBowwtMB/uKxuAvejyz.aQNibry714OoWLYQXuneeJwLgpW, 7, bal street, 2020-06-02 08:02:42, 2020-06-02 08:02:42))



via Chebli Mohamed

Laravel Not Writing Session To Memcached

I have 2 Memcached Servers e.g serv1 and serv2 Both Work Fine

But when i use it with Laravel Cache,

It works fine when i use serv1.

But when i change the server to serv2. It does not write the Session

I use 1 server at a time. I change the servers IPs.

serv2 works fine when i store data manually and retrieve it, it only gives me issue when i am using it with Laravel.

Both Servers have Memcached Service Installed.

Laravel's code cannot be wrong as it works on serv1.

I cant seem to figure out the problem, been a week since this issue.

Laravel version is 5.6. OS is CentOs THis is my Laravel's Cache.php

the servers are in my env file

'memcached' => [

        'driver' => 'memcached',
        'persistent_id' => env('MEMCACHED_PERSISTENT_ID'),
        'sasl' => [
            env('MEMCACHED_USERNAME'),
            env('MEMCACHED_PASSWORD'),
        ],
        'options' => [
            // Memcached::OPT_CONNECT_TIMEOUT  => 2000,
        ],
        'servers' => [
            [
                'host' => env('MEMCACHED_HOST', '127.0.0.1'),
                'port' => env('MEMCACHED_PORT', 11211),
                'weight' => 100,
            ],

I have used serv2 to store sessions from vanilla PHP and it is working fine there.

Laravel is storing Values to Cache using Cache:put but not using Session to serv2



via Chebli Mohamed

lundi 1 juin 2020

Use whereHas and query pivot table at the same time is possible in laravel?

I have 2 entities: Beneficiaries and Initiatives, those related in a pivot table beneficiary_initiative. I want all the beneficiaries related to a specific "region". But i have following error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'beneficiaries.region' in 'where clause' (SQL: select count(*) as aggregate from `initiatives` where `state` = 1 and exists (select * from `beneficiaries` inner join `beneficiary_initiative` on `beneficiaries`.`id` = `beneficiary_initiative`.`beneficiary_id` where `initiatives`.`id` = `beneficiary_initiative`.`initiative_id` and `beneficiaries`.`region` = 120000))

Laravel version: 5.4

My database structure:

beneficiaries

  • id
  • name

initiatives

  • id
  • name

beneficiary_initiative

  • beneficiary_id
  • initiative_id
  • region
  • province

My models:

// Beneficiary.php
public function initiatives()
{
  return $this->belongsToMany(Initiative::class)->withPivot('region', 'province');
}
// Initiative.php

public function beneficiaries()
{
  return $this->belongsToMany(Beneficiary::class)->withPivot('region', 'province');
}

public static function search ($region)
{
  $query = Initiative::select(
    'initiatives.id',
    'initiatives.name'
  )->with('program')
   ->with('beneficiaries')
   ->with('contacts');

  $query->when($region !== null, function ($q) use ($region) {
      return $q->whereHas('beneficiaries', function ($q1) use ($region) {
        $q1->where('beneficiary_initiative.region', '=', $region);
      });
  });

  $initiatives = $query->get();
  return $initiatives;
}


via Chebli Mohamed

How to run Artisan command without a console

Someone know how to start command without console

Let me explain After the file is downloaded successfully, you need to run the command php artisan db:seed I tried this option but I got an error as a result Maybe someone knows another solution to this problem I will be very grateful I tried this option but I got an error as a result

$file = file($request->file->getRealPath());
$fileName = resource_path('upload/csv/accounts/' . date('y-m-d-H-i-s') . '.csv');
$path = file_put_contents($fileName, $file);
return redirect()->route('admin.accounts');

I tried this option

$file = file($request->file->getRealPath());
$fileName = resource_path('upload/csv/accounts/' . date('y-m-d-H-i-s') . '.csv');
$path = file_put_contents($fileName, $file);
return redirect()->route('admin.accounts');
Artisan::call('db:seed');

And this

$file = file($request->file->getRealPath());
$fileName = resource_path('upload/csv/accounts/' . date('y-m-d-H-i-s') . '.csv');
$path = file_put_contents($fileName, $file);
return redirect()->route('admin.accounts')->with(Artisan::call('db:seed'));


via Chebli Mohamed