0
votes

I am facing this error in laravel 5.

Here is the controller function in which I am facing the error (in the line where I do ($vendor->client[0]->id) :

public function show($username) {
Log::info('Vendors Controller : show function with '.$username);
$vendor = VendorProfile::where('username', $username)->first();
$output = print_r($vendor,1);
Log::info($output);
if($vendor) {
  Log::info('client '. $vendor->client);
  $client = Client::find($vendor->client[0]->id);
  $title = $client->profile->company_name;

$output is printed as:

    [2015-06-15 21:34:43] local.INFO: App\models\VendorProfile Object
(
    [table:protected] => vendor_profile
    [guarded:protected] => Array
        (
            [0] => id
        )

    [morphClass:protected] => MorphVendorProfile
    [connection:protected] =>
    [primaryKey:protected] => id
    [perPage:protected] => 15
    [incrementing] => 1
    [timestamps] => 1
    [attributes:protected] => Array
        (
            [id] => 16
            [first_name] => some name
            [last_name] =>
            [company_name] => some name
            [contact_number] => 1234567890
            [username] => username
            [profile_photo] =>
            [photo_mime_type] =>
            [cover_photo] =>
            [cover_photo_mime_type] =>
            [address] =>
            [city_id] => 1
            [zip_code] =>
            [story] =>
            [establishment_date] =>
            [pricing] =>
            [education] =>
            [services_offered] =>
            [assignments_undertook] =>
            [advanced_fees] =>
            [equipments] =>
            [about_service] =>
            [coins] => 500
            [created_at] => 2015-06-15 20:21:45
            [updated_at] => 2015-06-15 20:21:45
        )

    [original:protected] => Array
        (
            [id] => 16
            [first_name] => some name
            [last_name] =>
            [company_name] => some name
            [contact_number] => 1234567890
            [username] => username
            [profile_photo] =>
            [photo_mime_type] =>
            [cover_photo] =>
            [cover_photo_mime_type] =>
            [address] =>
            [city_id] => 1
            [zip_code] =>
            [story] =>
            [establishment_date] =>
            [pricing] =>
            [education] =>
            [services_offered] =>
            [assignments_undertook] =>
            [advanced_fees] =>
            [equipments] =>
            [about_service] =>
            [coins] => 500
            [created_at] => 2015-06-15 20:21:45
            [updated_at] => 2015-06-15 20:21:45
        )

    [relations:protected] => Array
        (
        )

    [hidden:protected] => Array
        (
        )

    [visible:protected] => Array
        (
        )

    [appends:protected] => Array
        (
        )

    [fillable:protected] => Array
        (
        )

    [dates:protected] => Array
        (
        )

    [casts:protected] => Array
        (
        )

    [touches:protected] => Array
        (
        )

    [observables:protected] => Array
        (
        )

    [with:protected] => Array
        (
        )

    [exists] => 1
)

The models VendorProfile and Client are connected as:

in VendorProfile model:

    protected $morphClass = 'MorphVendorProfile';

  // Defining 'Polymorphic' Relationship with Client Model
  public function client() {
    return $this->morphMany('App\models\Client', 'profile');
  }

and I have an alias in my config/app.php:

'MorphVendorProfile'=> 'App\models\VendorProfile'

in Client model :

public function profile() {
        return $this->morphTo();
    }

Update:

This error has occurred while migrating the code from laravel 4.2 to laravel 5. So right now, when I run the previous code that was based on 4.2 version with the SAME database, and it didn't throw me any error, so I think problem is with the code, not database. I am certain that there is a problem with 'morph' relationships, I had to modify a bit in the process of migrating to make it work on other pages.

here is the morphTo function in my Eloquent/Model.php:

/**
 * Define a polymorphic, inverse one-to-one or many relationship.
 *
 * @param  string  $name
 * @param  string  $type
 * @param  string  $id
 * @return \Illuminate\Database\Eloquent\Relations\MorphTo
 */
public function morphTo($name = null, $type = null, $id = null)
{
    // If no name is provided, we will use the backtrace to get the function name
    // since that is most likely the name of the polymorphic interface. We can
    // use that to get both the class and foreign key that will be utilized.
    if (is_null($name))
    {
        list(, $caller) = debug_backtrace(false, 2);

        $name = snake_case($caller['function']);
    }

    list($type, $id) = $this->getMorphs($name, $type, $id);

    // If the type value is null it is probably safe to assume we're eager loading
    // the relationship. When that is the case we will pass in a dummy query as
    // there are multiple types in the morph and we can't use single queries.
    if (is_null($class = $this->$type))
    {
        Log::info('eagerly loading');
        return new MorphTo(
            $this->newQuery(), $this, $id, null, $type, $name
        );
    }

    // If we are not eager loading the relationship we will essentially treat this
    // as a belongs-to style relationship since morph-to extends that class and
    // we will pass in the appropriate values so that it behaves as expected.
    else
    {
        Log::info('not eagerly loading');
        Log::info($class);
        $instance = \App::make('\App\models\\'.$class);
        $output = print_r($instance,1);
        Log::info('*'.$output.'*');
        return new MorphTo(
            $instance->newQuery(), $this, $id, $instance->getKeyName(), $type, $name
        );
    }
}
1
It appears there are no clients associated with that vendor. Can you double check that?Kryten
Can you please tell me how to do that through code? In my database, I have a row in the clients table which has profile -> VendorProfile id = 16 username = (Same username above)Yash
It's been a while since I configured a polymorphic relationship. I'd suggest reviewing the documentation on these relationships and making sure a) your database columns are set up correctly, and b) you actually have entries in both tables corresponding to the data you expect to see.Kryten
This error has occurred while migrating the code from laravel 4.2 to laravel 5. So, right now, I run the previous code that was based on 4.2 version with the SAME database, and it didn't through me any error, so I think prob is with the code, not database. I am certain that there is a problem with 'morph' relationships, I had to modify a bit in the process of migrating to make it work on other pages.Yash
I have code I've migrated from 4.2 to 5 without issue. The error is telling you that the Client collection which you access via the relationship is empty. The question to ask is "why is it empty?" The answer is probably not "something is wrong with the framework" - I suspect it's probably a change to the namespace of your model. If the value in your database doesn't match the namespace + name of your model, the code will not be able to find it.Kryten

1 Answers

0
votes

Check your namespaces. Make sure that the polymorphic relationship that you have setup references the full namespace in both the Vendor and Client models