mardi 2 juin 2020

Laravel 5.3 Migration: 1215 Cannot add foreign key constraint

I'm using Laravel 5.3 and I'm trying to create FK, however when I migrate my table using artisan I'm getting the following error:

  [Illuminate\Database\QueryException]
  SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `topic_video` add constraint `topic_video_vendor_id_foreign` foreign key (`vendor_id`) references `vendors` (`id`))



  [Doctrine\DBAL\Driver\PDOException]
  SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint



  [PDOException]
  SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint

I have tried multiple solutions on SOF for different laravel versions but none of them work.

This is my topic_video table (InnoDB)

enter image description here

This is an old and big project, as I don't have migrations for it, only for new tables we have migrations. So I have created a vendors (MyISAM)

Schema::create('vendors', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('channel_url');
            $table->timestamps();
        });

Then I have add FK of above migration to topic_video table.

Schema::table('topic_video', function (Blueprint $table) {
            $table->integer('vendor_id')->unsigned()->nullable();
            $table->foreign('vendor_id')->references('id')->on('vendors');
        });

I have tried without unsigned(), without nullable() but still didn't work! Any help would be appreciated!



via Chebli Mohamed

Laravel Join with 2 GroupBy

can you help me solve this problem?

I want to combine the two tables

transaction_ppob transaction_sosmed

In the table there is a column containing the status, I want to count the amount success, wait, and error in table 1 and table 2

Here is the code

$status = DB::table('transaction_ppob as ppob')
    ->join('transaction_sosmed as sosmed','ppob.user_id', '=', 'sosmed.user_id')
    ->select('sosmed.status', 'ppob.status', DB::raw('count(*) as total'))
    ->groupByRaw('sosmed.status, ppob.status')
    ->get();

How do you calculate the second column?

transaction_ppob

  • success
  • success
  • pending

transaction_sosmed

  • pending
  • error


via Chebli Mohamed

Restrict user to update email at the time of reset password in Laravel

I am using laravel 6 for resetting password, but issue is at the time of reset password user isalso given a field to update email, but i don't want to give user option to update email, user can only reset his password. enter image description here



via Chebli Mohamed

Laravel 5.6 : Check the column values in imported csv file using maat/website

In our project, we would like to put import csv file(we're using maat/website). We would like to check if the corresponding values in the csv file are correct so that we can save it in our table.

Controller

public function importCSVVendorList(Request $request)
{
    $data       =   $request->all();
    $validator = Validator::make($data, [
        'file' => 'required|mimes:csv'
    ]);
    // dd($validator->fails());
    //if validator fails
    // if ($validator->fails()) {
    //     return false;
    // }
    $path           = $request->file('file')->getRealPath();
    $row_values     = Excel::load($path)->get();
    if($row_values->count() > 0)
    {
        foreach($row_values->toArray() as $key => $value)
        {
            if($key == 1)
            {
            dd($value);

            }
            // foreach($value as $row)
            // {
            //     dd($row['code']);
            //     $vendor_lists       = array(
            //                              // 'Code' => $row['']
            //                           );
            // }
        }
    }
    dd($path);
}

Value of my dd

enter image description here

Question: How do we check the values in the csv file?



via Chebli Mohamed

Laravel validation messages array indices starting from 0

If in a form request I have rules

[
    'items' => ['required', 'array'],
    'items.*' => ['required', 'numeric'],
]

Then error message returns like this:

"items.0" => array:1 [
  0 => "The selected items.0 is invalid."
]

Is there any way to have the message look like "The selected items #1 is invalid" or something similar, that is actually human readable?

Please note, I need this for the whole system globally, there's already >500 form request classes, although not that many are using array validation, so changing extended class or adding a trait wouldn't be too horrible



via Chebli Mohamed

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