lundi 31 juillet 2017

Laravel Serialization of 'UploadFile' class is not allowed

Running tests written by other programmers and encountered the following error:

ErrorException: Serialization of closure failed: Serialization of 'Symfony\Component\HttpFoundation\File\UploadedFile' is not allowed

/home/vagrant/Code/punto/vendor/jeremeamia/SuperClosure/src/SerializableClosure.php:116
/home/vagrant/Code/punto/vendor/jeremeamia/SuperClosure/src/Serializer.php:69
/home/vagrant/Code/punto/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php:272
/home/vagrant/Code/punto/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php:193
/home/vagrant/Code/punto/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:219
/home/vagrant/Code/punto/app/Services/NotificationService.php:126
/home/vagrant/Code/punto/app/Exceptions/Handler.php:42
/home/vagrant/Code/punto/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:269
/home/vagrant/Code/punto/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:89
/home/vagrant/Code/punto/vendor/laravel/framework/src/Illuminate/Foundation/Testing/CrawlerTrait.php:394

I am not familiar with the system yet. This error seems like I have to implement Serialization to some class.



via Chebli Mohamed

vendredi 28 juillet 2017

how to solve this exception in laravel ? Symfony\Component\Debug\Exception\FatalThrowableError

I run this command to create model.

php artisan make:model BaseModel

but issue this exception

[Symfony\Component\Debug\Exception\FatalThrowableError]                   
 Parse error: syntax error, unexpected '<<' (T_SL), expecting end of file 

may i know how to handle this issue.



via Chebli Mohamed

Connection could not be established with host smtp.gmail.com Network is unreachable #101 error in Laravel email

I am trying to send email from my laravel 5.1 system. I can send emails from my localhost and unable to send from the server. See my email configuration settings in .env file,

MAIL_DRIVER=smtp
MAIL_HOST= smtp.gmail.com
MAIL_PORT= 587
MAIL_USERNAME= username***@gmail.com
MAIL_PASSWORD= *********
MAIL_ENCRYPTION=tls

This configuration only work on my localhost. On server I am getting this error,

Swift_TransportException in StreamBuffer.php line 268:
Connection could not be established with host smtp.gmail.com [Network is unreachable #101]

I also try with changing MAIL_PORT from 587 to 465 and MAIL_ENCRYPTION from tls to ssl. But I am geeitng the same error. How can I fix this issue?



via Chebli Mohamed

jeudi 27 juillet 2017

Only mailables may be queued

i have to update from 5.1 to 5.4

this was code for mail with 5.1

  Mail::queue('emails.welcome_client', compact('user', 'userPassword'), function ($message) use ($user, $adminEmails) {
        $message->subject('Welcome to Enterprise Solutions!');

        $message->from(env('MAIL_FROM_ADDRESS'), env('MAIL_FROM_NAME'));
        $message->to($user->email);
        foreach($adminEmails as $adminEmail) { 
                       $message->bcc($adminEmail);
}  
});

I have to change from laravel 5.1 to 5.4 so i create object mail

here it is

<?php

 namespace App\Mail;

 use Illuminate\Bus\Queueable;
 use Illuminate\Mail\Mailable;
 use Illuminate\Queue\SerializesModels;
 use Illuminate\Contracts\Queue\ShouldQueue;

 class ClientMail extends Mailable
 {
use Queueable, SerializesModels;

/**
 * Create a new message instance.
 *
 * @return void
 */
public $user;
// protected $content;

public function __construct($user)
{

    $this->content = $user;
}

/**
 * Build the message.
 *
 * @return $this
 */
public function build()
{
    return $this->from(('MAIL_FROM_ADDRESS'), env('MAIL_FROM_NAME'))
                ->subject('Welcome to Enterprise Solutions!')
                ->view('emails.welcome_client');
    }
}

and in controller i do this

 Mail::to($user->email)
    ->bcc($adminEmail)
    ->queue(new ClientMail($adminEmails)); 
        }

when I try to run I get this error Undefined $adminEmail How I can fix this problem ?



via Chebli Mohamed

mercredi 26 juillet 2017

Define a relationship as a union

In several parts of my code I have for example these lines:

$user->movements()->get();
...
$user->movements()->where(...)->get();
...
$user->movements()->where(...)->select(... sum, count, avg ...)->get();
...

But now I'm facing an important change on the movements table structure. Now I need two tables with a similar structure (the data HAVE to be in separate tables), movements and ticket_movements.

These tables are consulted by two system, one of then needs the data to be separated and the other needs the data to be as in one table.

So, in one of the systems, I would like to define the relationship movements() as an union of movements and ticket_movements tables.

So, having the relationship movements defined as:

public function movements()
{
        $movements = $this->hasMany('App\Model\Movement')
            ->select(\DB::raw("
                id,
                user_id,
                movement_type_id,
                amount,
                description
            "));

        $tickets_movements = $this->hasMany('App\Model\TicketMovement')
            ->select(\DB::raw("
                id,
                user_id,
                movement_type_id,
                amount,
                description
            "));

        return $movements->union($tickets_movements->getQuery());
    }

If I do this:

$user->movements()
    ->whereIn('movement_type_id', [1, 2])
    ->select(\DB::raw('
        SUM(CASE WHEN movement_type_id = 1 THEN 1 ELSE 0 END) as credit,
        SUM(CASE WHEN movement_type_id = 2 THEN 1 ELSE 0 END) as debit
    '))
    ->first();

The query I get is:

SELECT 
SUM(CASE WHEN movement_type_id = 1 THEN 1 ELSE 0 END) as credit, 
SUM(CASE WHEN movement_type_id = 2 THEN 1 ELSE 0 END) as debit 
FROM "movements" 
WHERE "movements"."user_id" = 2 
AND "movements"."user_id" is not null 
AND "movement_type_id" in (1, 2)

UNION

SELECT id, user_id, movement_type_id, amount, description
FROM "ticket_movements" 
WHERE "ticket_movements"."user_id" = 2 
AND "ticket_movements"."user_id" is not null limit 1

Besides it's not the query I need, it give me an error because of the columns:

Syntax error: 7 ERROR: 
each UNION query must have the same number of columns

The query I need is something like this:

SELECT 
SUM(CASE WHEN movement_type_id = 1 THEN 1 ELSE 0 END) as credit, 
SUM(CASE WHEN movement_type_id = 2 THEN 1 ELSE 0 END) as debit 

FROM (

SELECT id, user_id, movement_type_id, amount, description
FROM "movements" 

UNION

SELECT id, user_id, movement_type_id, amount, description
FROM "ticket_movements" ) as movements

WHERE "movements"."user_id" = 2 
AND "movements"."movement_type_id" in (1, 2)

Without modifying each line where I do $user->movements()...

I don't know is that is possible...



via Chebli Mohamed

Trying to get property of non-object laravel 5.4

We have a project upgraded from laravel v 5.1 to v 5.4 and many issues and bugs appear after upgrade anyway have this one

Trying to get property of non-object for index.blade.php

and this is the code

<tbody>
 @foreach($routeFEmails as $routeFEmail)

 <tr>
 <td></td>
 <td> ()</td>
 <td>:&nbsp; </td>
 <td></td>
 <td></td>
  </tr>
  @endforeach
 </tbody>

i check model and no value from what mention is null no NULL

i did also

php artisan cache:clear 
php artisan route:clear 
php artisan view:clear 

HOW I CAN FIX THIS ? :(



via Chebli Mohamed

mardi 25 juillet 2017

Attaching pivots using Laravel model on a different connection

I am trying to copy some data from one database to another (both share the same schema). Both databases share the same host.

In config/database.php I have the following:

/*
|--------------------------------------------------------------------------
| Default Database Connection Name
|--------------------------------------------------------------------------
|
| Here you may specify which of the database connections below you wish
| to use as your default connection for all database work. Of course
| you may use many connections at once using the Database library.
|
*/

'default' => 'connection1',

/*
|--------------------------------------------------------------------------
| Database Connections
|--------------------------------------------------------------------------
|
| Here are each of the database connections setup for your application.
| Of course, examples of configuring each database platform that is
| supported by Laravel is shown below to make development simple.
|
|
| All database work in Laravel is done through the PHP PDO facilities
| so make sure you have the driver for your particular database of
| choice installed on your machine before you begin development.
|
*/

'connections' => [
    'connection1' => [
        'driver'    => 'mysql',
        'host'      => 'host.link.here'
        'database'  => 'db1',
        'username'  => env('DB_USERNAME'),
        'password'  => env('DB_PASSWORD')
    ],

    'connection2' => [
        'driver'    => 'mysql',
        'host'      => 'host.link.here',
        'database'  => 'db2',
        'username'  => env('DB_USERNAME_2'),
        'password'  => env('DB_PASSWORD_2')
    ],
],

I wrote a command to duplicate an experiment from db1 to db2. The relevant part of the code is here:

$experiment = App\Experiment::where('key', 1234)->first();
$connection = 'connection2';

DB::connection($connection)->transaction(function() use ($experiment, $connection) {
        $clone_experiment = $experiment->replicate();
        $clone_experiment->setConnection($connection);
        $clone_experiment->save();

        $this->info('Created experiment on destination DB with ID ' . $clone_experiment->id);

        // Copy objectives
        foreach ($experiment->objectives as $objective) {
            $clone_objective = $objective->replicate();
            $clone_objective->setConnection($connection);
            $clone_objective->save();

            // Attach objective to experiment with pivot data
            $pivot_data = [
                'field_1' => $objective->pivot->field_1,
                'field_2' => $objective->pivot->field_2,
            ];

            $clone_experiment->objectives()->attach([$clone_objective->id => $pivot_data]);
        }
        $this->info('Duplicated objectives');
});

There seems to be an issue with attaching the pivot to the model on db2. I get this output when running the command:

Created experiment on destination DB with ID 3626
Exception: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`db1`.`experiment_objective`, CONSTRAINT `experiment_objective_experiment_id_foreign` FOREIGN KEY (`experiment_id`) REFERENCES `experiments` (`id`) ON DELETE CASCADE) (SQL: insert into `experiment_objective` (`created_at`, `experiment_id`, `objective_id`, `field_1`, `field_2`, `updated_at`) values (2017-07-25 23:54:25, 3626, 4973, 10, exploration, 2017-07-25 23:54:25))

The error indicates that the constraint for db1 is failing, but I do not know why it is looking at that since both $clone_experiment and $clone_objective are using connection2, which connects to db2. Is there any way around this?



via Chebli Mohamed