Laravel 5.1 Eloquent ORM randomly returning incorrect relationship - *major update* Laravel 5.1 Eloquent ORM randomly returning incorrect relationship - *major update* laravel laravel

Laravel 5.1 Eloquent ORM randomly returning incorrect relationship - *major update*


I can't help you get to the root of whats causing your issue, but I can offer a possible work around based on the logic you provided in Update 1 of your question.

Original Logic

$customer = Customer::find($order->customer_id);$user = User::find($customer->user_id);if(!is_null($user)) {    // send email and log actions etc}

Revised Logic

Because a customers user_id can be null, it may be more effective to limit the returned customers to those who have a user_id. This can be achieved by using the whereNotNull() method. We can then go on to check if a customer was returned, and if so send the email, etc.

$customer = Customer::whereNotNull('user_id')->find($order->customer_id); if (!$customer->isEmpty()) {     // send email and log actions etc }

By not giving the application a chance to return a customer with a null user_id this should hopefully solve your issue, but unfortunately it doesn't shine any light on why it is happening in the first place.


Shouldn't your migrations have

->unsigned()

for example:

$table->integer('user_id')->unsinged()->index();

As mentioned in the Laravel Doc?

Laravel also provides support for creating foreign key constraints, which are used to force referential integrity at the database level. For example, let's define a user_id column on the posts table that references the id column on a users table. http://laravel.com/docs/5.1/migrations#foreign-key-constraints


I am not sure if your migrations are defined correctly specially when compared to your Models. If you are using belongsTo and hasOne relationship, you should use foreign key references in migrations.

Schema::create('customers', function(Blueprint $table)    {        $table->increments('id');        $table->integer('user_id')->nullable();        $table->foreign('user_id')->references('id')->on('users');        $table->string('first_name');        $table->string('last_name');        $table->string('telephone')->nullable();        $table->string('mobile')->nullable();        $table->timestamps();        $table->softDeletes();    });Schema::create('orders', function(Blueprint $table){   $table->increments('id');   $table->integer('payment_id')->nullable()->index();   $table->integer('customer_id')->nullable();   $table->foreign('customer_id')->references('id')->on('customers');   $table->integer('staff_id')->nullable()->index();   $table->decimal('total', 10, 2);   $table->timestamps();   $table->softDeletes();    });

Now you will need to set this column whenever an actual user exists when the customer record is being created. But you don't actually have to set this column manually. You can do this below as you are using relationships:

Step 1: Save the customer first.

$customer->save();

Step 2: Now we will set the user_id on the customer if exists. To do this, you can get the user object in $user and then just call

$customer->user->save($user);

The code above will automatically set the user_id on customers table

Then I will check if user record exists in this way below:

$user_exists = $order->customer()->user();if($user_exists){    //email whatever}