samedi 6 novembre 2021

MPDF in Laravel can't output (inline) pdf

I am doing below code in Laravel 5.5 with mpdf 8.0

$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML('Hello World');
$mpdf->Output("test","I");

It outputs gibberish/garbage values, seemingly showing pdf file in raw form.

Some findings

  • If I use $mpdf->Output($reportPath, 'F'); (saving it to file) and the opening that. It opens the file as expected.
  • If I place die(); after $mpdf->Output("test","I"); it shows the document.
  • My suspicion is, it has something something to do with Content-type:application/pdf not being set by default but I have also tried using header("Content-type:application/pdf"); before Output but of no use. it is still showing Content-Type: text/html; charset=UTF-8 in response header in Network tab of chrome (also tried Firefox).

Some back-story

  • It used to work on php7.3 just fine, but I have to update it to php7.4 due to some library and multiple application on a single server scenario.
  • Also start using a sub-domain for my application instead of placing the directories after the domain.

I'm looking for

  • A solution that doesn't require me to place die; at the end of output.
  • Or some clue in on why this has started happening or/and perhaps why I need to place die; after Output.
  • Any other solution.

The goal is to provide some ref. for people encountering same issues in future, since I have spent hours and haven't anything that specifically address such issue.



via Chebli Mohamed

jeudi 4 novembre 2021

I have a this error en laravel : Syntax error or access violation: 1075 Incorrect table definition.How do I solve this problem?

** SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key (SQL: create table loans (id_loan int unsigned not null auto_increment primary key, book_id bigint unsigned not null auto_increment primary key, user_id bigint unsigned not null auto_increment primary key, date_vto date not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci').How do I solve this problem?

**

Migrations

public function up()
{
    Schema::create('books', function (Blueprint $table) {
        $table->increments('id_book');
        $table->string('title');
        $table->integer('amount');
    });
}
public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('username');
        $table->string('password');
        $table->string('role');

    });
}
 public function up()
{
    Schema::create('loans', function (Blueprint $table) {
        $table->increments('id_loan');
        $table->bigIncrements('book_id')->unsigned();
        $table->bigIncrements('user_id')->unsigned();
        $table->date('date_vto');

        $table->foreign('book_id')->references('id_book')->on('books')->onDelete('cascade');
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
    });
}

Models books

public function loans(){
    return $this->hasMany(Loans::class);
}

Models Loans

public function books()
{
    return $this->belongsTo(Books::class, 'book_id', 'id_book');
}
public function users()
{
    return $this->belongsTo(Users::class, 'user_id', 'id');
}

Models Users

public function loans(){
        return $this->hasMany(Loans::class);
    }


via Chebli Mohamed

query in eloquent model

I´m traying to do this query in eloquent.

SELECT llamada_estado.desc, count(id_estado) 
                                      FROM estadisticas INNER JOIN llamada_estado ON estadisticas.id_estado = llamada_estado.id 
                                      WHERE updated_at BETWEEN '2021-11-01' AND '2021-11-30' 
                                      GROUP BY estadisticas.id_estado

i´m traying this:

$query2=Estadisticas::query()->whereDate('updated_at', '<=', $toDate)
                                     ->whereDate('updated_at', '>=', $fromDate)
                                     ->join('llamada_estado', 'estadisticas.id_estado', '=', 'llamada_estado.id')
                                     ->groupBy('estadisticas.id_estado')
                                     ->get();

but always return that x column it´s not included in group by, but in phpMyAdmin i´m doing this query:

SELECT llamada_estado.desc, count(id_estado) 
FROM estadisticas 
INNER JOIN llamada_estado ON estadisticas.id_estado = llamada_estado.id 
WHERE updated_at BETWEEN '2021-11-01' AND '2021-11-30' 
GROUP BY estadisticas.id_estado

and this it´s my result:

desc, count(id_estado)
NUEVA 399
PENDIENTE 104
ANULADA 117
CONFIRMADA 50
PASADA A COMERCIALES 175
MAYOR 196
ERRONEO NO EXISTE 649
AUSENTE 681

and i need this result in eloquent.

my tables it´s:

my table call status

CREATE TABLE `llamada_estado` (
  `id` int(10) UNSIGNED NOT NULL,
  `nombre` varchar(30) COLLATE utf8mb4_unicode_ci NOT NULL,
  `desc` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
  `clase_span` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `clase` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `hex` varchar(7) COLLATE utf8mb4_unicode_ci DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

--
-- Volcado de datos para la tabla `llamada_estado`
--

INSERT INTO `llamada_estado` (`id`, `nombre`, `desc`, `clase_span`, `clase`, `hex`) VALUES
(1, 'nueva', 'NUEVA', 'badge bg-primary text-white', 'primary', '#5c80d1'),
(2, 'pendiente', 'PENDIENTE', 'badge bg-warning text-white', 'warning', '#f3b760'),
(3, 'anulada', 'ANULADA', 'badge bg-danger text-white', 'danger', '#d26a5c'),
(4, 'confirmada', 'CONFIRMADA', 'badge bg-success text-white', 'success', '#46c37b'),
(5, 'comercializada', 'PASADA A COMERCIALES', 'badge bg-info text-white', 'info', '#70b9eb'),
(6, 'confirmada-ausente', 'CONFIRMADA/AUSENTE', 'badge bg-secondary text-white', 'secondary', '#6c757d'),
(7, 'confirmada-anulada', 'CONFIRMADA/ANULADA', 'badge bg-dark text-white', 'dark', '#343a40'),
(8, 'MAYOR', 'MAYOR', 'badge bg-danger text-white', 'danger', '#d26a5c'),
(9, 'Erroneo-no existe', 'ERRONEO NO EXISTE', 'badge bg-danger text-white', 'danger', '#d26a5c'),
(10, 'Robinson', 'ROBINSON', 'badge bg-danger text-white', 'danger', '#d26a5c'),
(11, 'ausente', 'AUSENTE', 'badge bg-warning text-white', 'warning', '#f3b760');

my table statistics

--
-- Estructura de tabla para la tabla `estadisticas`
--

CREATE TABLE `estadisticas` (
  `id` int(10) UNSIGNED NOT NULL,
  `id_empleado` int(10) UNSIGNED NOT NULL,
  `id_llamada` int(10) UNSIGNED NOT NULL,
  `id_estado` int(10) UNSIGNED NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

--
-- Volcado de datos para la tabla `estadisticas`
--

INSERT INTO `estadisticas` (`id`, `id_empleado`, `id_llamada`, `id_estado`, `created_at`, `updated_at`) VALUES
(72, 9, 89008, 1, '2021-08-26 06:47:38', '2021-08-26 06:47:38'),
(75, 9, 56617, 1, '2021-08-26 06:47:38', '2021-08-26 06:47:38'),
(77, 9, 78359, 1, '2021-08-26 06:47:38', '2021-08-26 06:47:38'),
(80, 9, 70361, 1, '2021-08-26 06:47:39', '2021-08-26 06:47:39'),
(87, 9, 89023, 1, '2021-08-26 06:47:39', '2021-08-26 06:47:39'),
(89, 9, 84052, 1, '2021-08-26 06:47:39', '2021-08-26 06:47:39'),
(90, 9, 89026, 1, '2021-08-26 06:47:39', '2021-08-26 06:47:39'),
(92, 9, 89028, 1, '2021-08-26 06:47:39', '2021-08-26 06:47:39'),
(108, 23, 89044, 1, '2021-08-26 06:50:12', '2021-08-26 06:50:12'),
(109, 23, 89045, 1, '2021-08-26 06:50:12', '2021-08-26 06:50:12'),
(112, 23, 89048, 1, '2021-08-26 06:50:12', '2021-08-26 06:50:12'),
(124, 23, 56496, 1, '2021-08-26 06:50:12', '2021-08-26 06:50:12'),
(126, 23, 89062, 1, '2021-08-26 06:50:12', '2021-08-26 06:50:12'),
(129, 23, 89065, 1, '2021-08-26 06:50:13', '2021-08-26 06:50:13'),
(130, 23, 89066, 1, '2021-08-26 06:50:13', '2021-08-26 06:50:13'),
(142, 24, 89078, 1, '2021-08-26 06:50:36', '2021-08-26 06:50:36'),
(144, 24, 55175, 1, '2021-08-26 06:50:36', '2021-08-26 06:50:36'),
(146, 24, 58093, 1, '2021-08-26 06:50:36', '2021-08-26 06:50:36'),
(149, 24, 72171, 1, '2021-08-26 06:50:36', '2021-08-26 06:50:36'),
(152, 24, 89088, 1, '2021-08-26 06:50:36', '2021-08-26 06:50:36'),
(155, 24, 89091, 1, '2021-08-26 06:50:36', '2021-08-26 06:50:36'),
(156, 24, 89092, 1, '2021-08-26 06:50:36', '2021-08-26 06:50:36'),
(161, 24, 73468, 1, '2021-08-26 06:50:36', '2021-08-26 06:50:36'),
(164, 24, 89100, 1, '2021-08-26 06:50:36', '2021-08-26 06:50:36'),
(165, 24, 89101, 1, '2021-08-26 06:50:36', '2021-08-26 06:50:36'),
(193, 26, 56101, 1, '2021-08-26 06:51:36', '2021-08-26 06:51:36'),
(195, 26, 89131, 1, '2021-08-26 06:51:36', '2021-08-26 06:51:36'),
(196, 26, 83856, 1, '2021-08-26 06:51:36', '2021-08-26 06:51:36'),
(197, 26, 78782, 1, '2021-08-26 06:51:36', '2021-08-26 06:51:36'),
(225, 11, 89161, 1, '2021-08-26 06:52:01', '2021-08-26 06:52:01'),
(227, 11, 89163, 1, '2021-08-26 06:52:01', '2021-08-26 06:52:01'),
(229, 11, 89165, 1, '2021-08-26 06:52:01', '2021-08-26 06:52:01'),
(231, 11, 85394, 1, '2021-08-26 06:52:01', '2021-08-26 06:52:01'),
(233, 11, 89169, 1, '2021-08-26 06:52:01', '2021-08-26 06:52:01'),
(236, 11, 60645, 1, '2021-08-26 06:52:01', '2021-08-26 06:52:01'),
(242, 11, 84919, 1, '2021-08-26 06:52:01', '2021-08-26 06:52:01'),
(246, 11, 59005, 1, '2021-08-26 06:52:01', '2021-08-26 06:52:01'),
(263, 11, 68628, 1, '2021-08-26 06:52:37', '2021-08-26 06:52:37'),
(264, 11, 89200, 1, '2021-08-26 06:52:37', '2021-08-26 06:52:37'),
(265, 11, 89201, 1, '2021-08-26 06:52:37', '2021-08-26 06:52:37'),
(268, 11, 89204, 1, '2021-08-26 06:52:37', '2021-08-26 06:52:37'),
(269, 11, 89205, 1, '2021-08-26 06:52:37', '2021-08-26 06:52:37'),

My question is, how i can to do this query in eloquent?

Thanks for help and read



via Chebli Mohamed

mercredi 3 novembre 2021

Request get method route handling

I am using laravel 5 . I want to set route like

http://localhost:8000/website/product-search-data?term=k

I also tried with

Route::get('/website/product-search-data/*', 'websiteController@searchProductList');
public function searchProductList($term)
{
    dd($term);
 }

But its providing me Sorry, the page you are looking for could not be found. How can I handle and get parameer value ?



via Chebli Mohamed

mardi 2 novembre 2021

How to remove any role by using removeRole() in laravel?

I have three models user,roles,model_has_roles,when i assign any role to the user from the roles table it's creating an instance of the model in model_has_roles table.I am using removeRole() laravel method for removing roles ,i am giving some details what are the roles present inside my database like(super-market,notification,all...).it's deleteing all roles except notification,all.

$roleName = implode(' ,',$roleName); // "super-market" or "all"

        foreach ($roles->pluck('name')->toArray() as $roleName) {
            $user->removeRole($roleName); 
        }

Now what i need is irrespective of the role(any role) i want to delete that role ,some of the roles it's deleteing and remove the instance of the model from the model_has_roles and some of the roles are not deleted (for example all,notification),please help me to fix this issue



via Chebli Mohamed

dimanche 31 octobre 2021

paypal transaction id from express checkout

I am unable to get the transactions id from paypal ExpressCheckout method.

 $provider = new ExpressCheckout;
 $response = $provider->getExpressCheckoutDetails($request->token);
 echo "<pre>";print_r($response);
Array
(
  [TOKEN] => EC-79454119W8713794B
  [BILLINGAGREEMENTACCEPTEDSTATUS] => 1
  [CHECKOUTSTATUS] => PaymentActionNotInitiated
  [TIMESTAMP] => 2021-11-01T04:14:23Z
  [CORRELATIONID] => f00ab9c2819b4
  [ACK] => Success
  [VERSION] => 123
  [BUILD] => 55938276
  [EMAIL] => john@abc.com
  [PAYERID] => 238MG2US77AQY
  [PAYERSTATUS] => unverified
  [FIRSTNAME] => john
  [LASTNAME] => doe
  [COUNTRYCODE] => US
  [ADDRESSSTATUS] => Confirmed
  [CURRENCYCODE] => USD
  [AMT] => 24.99
  [ITEMAMT] => 24.99
  [SHIPPINGAMT] => 0.00
  [HANDLINGAMT] => 0.00
  [TAXAMT] => 0.00
  [DESC] => Order #61 Invoice
  [INVNUM] => 61
  [NOTIFYURL] => https://example.com/ipn/notify
  [INSURANCEAMT] => 0.00
  [SHIPDISCAMT] => 0.00
  [INSURANCEOPTIONOFFERED] => false
  [L_NAME0] => Monthly Unlimited
  [L_QTY0] => 1
  [L_TAXAMT0] => 0.00
  [L_AMT0] => 24.99
  [PAYMENTREQUEST_0_CURRENCYCODE] => USD
  [PAYMENTREQUEST_0_AMT] => 24.99
  [PAYMENTREQUEST_0_ITEMAMT] => 24.99
  [PAYMENTREQUEST_0_SHIPPINGAMT] => 0.00
  [PAYMENTREQUEST_0_HANDLINGAMT] => 0.00
  [PAYMENTREQUEST_0_TAXAMT] => 0.00
  [PAYMENTREQUEST_0_DESC] => Order #61 Invoice
  [PAYMENTREQUEST_0_INVNUM] => 61
  [PAYMENTREQUEST_0_NOTIFYURL] => https://example.com/ipn/notify
  [PAYMENTREQUEST_0_INSURANCEAMT] => 0.00
  [PAYMENTREQUEST_0_SHIPDISCAMT] => 0.00
  [PAYMENTREQUEST_0_SELLERPAYPALACCOUNTID] => abc@gmail.com
  [PAYMENTREQUEST_0_INSURANCEOPTIONOFFERED] => false
  [L_PAYMENTREQUEST_0_NAME0] => Monthly Unlimited
  [L_PAYMENTREQUEST_0_QTY0] => 1
  [L_PAYMENTREQUEST_0_TAXAMT0] => 0.00
  [L_PAYMENTREQUEST_0_AMT0] => 24.99
  [PAYMENTREQUESTINFO_0_ERRORCODE] => 0
)

I need transaction id for making refund. that's why I need transaction id. This is the function I am trying for refund

$response = $provider->refundTransaction($transactionid);


via Chebli Mohamed

vendredi 29 octobre 2021

Laravel public_path is showing entire site URL

I finally figured out I can just use my laravel public folder for storing these PDF files I need to display on a web page, but the way I'm getting them fails to load on the front-end.

I just realized it's because public_path shows the whole URL like C:\user\user1\websites\public\test1.pdf where I'm expecting it to just show /test1.pdf

Why is this code resulting in the full URL like that

    $file1 = 'test1.pdf';
    $file2 = 'test2.pdf';

    if(file_exists(public_path($file1))){
        $files[1] = public_path($file1);
    }

    if(file_exists(public_path($file2))){
        $files[2] = public_path($file2);
    }

    $PDFfiles = json_encode($files);

    dd($PDFfiles);


via Chebli Mohamed