0
votes

I have 4 tables:

partners users phones addresses

Once a user has logged in I want to be sure that only records associated with his (id) are returned in the query.

So, when I query a partner, I want to return not only the data for the partner matching (id) but also insure that the users (id) matches the fk (user_id) in partners.

Now, for one more level of complexity, I would like to know if I can query the partner (matching id & user_id) as well as get their primary phone and primary address from the related phones and addresses tables. So, is this possible using eloquent and the table structure I have laid out below?

partners controller (PartnersController.php)

public function show($id)
{

    $data = Partners::find($id)->where('user_id', Auth::user()->id)->first();
    $phone = Partners::find(1)->phone;
    //var_dump($phone);
    return View::make('partners.showone')
            ->with('data', $data)
            ->with('phone', $phone)
            ->with('title', "$data->firstName $data->lastName")
            ->with('breadcrumb', "$data->firstName $data->lastName");
}

ideally, I would like to just pull the one record in the phones table that matches that primaryPhone value from the $data array. Is this possible while also insuring that the user_id matches user()->id?

view (showone.blade.php)

How would I present that data in the view? If phones is an array (like it would be above) I assuem the following would work

                @foreach($phone as $ph)
                <option value="{{ $ph->id }}" 
        @if($ph->id == $data->primaryPhone) {{ "selected" }} 
        @endif
        > {{ $ph->number }} - {{ $ph->description }}</option>
                @endforeach

Partners model (Partners.php)

public function phone()
{
   return $this->hasMany('Phone', 'partner_id')->where('user_id', Auth::user()->id);
}

public function primaryPhone($id)
{
   return $this->has('Phone', 'id')->where('user_id', Auth::user()->id);
}

TABLE STRUCTURES

        CREATE TABLE `partners` (
        `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
        `user_id` int(11) NOT NULL,
        `firstName` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
        `lastName` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
        `spouseName` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
        `primaryPhone` int(11) NOT NULL,
        `primaryAddress` int(11) NOT NULL,
        `primaryEmail` int(11) NOT NULL,
        `greeting` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
        `birthDate` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
        `spouseBirthDate` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
        `anniversary` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
        `notes` text COLLATE utf8_unicode_ci NOT NULL,
        `profession` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
        `spouseProfession` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
        `pNewsletter` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
        `eNewsletter` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
        `partnerStatus` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
        `photoUrl` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
        `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
        `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
        PRIMARY KEY (`id`),
        UNIQUE KEY `partners_id_unique` (`id`)
      ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

      CREATE TABLE `phones` (
        `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
        `partner_id` int(11) NOT NULL,
        `user_id` int(11) NOT NULL,
        `number` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
        `description` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
        `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
        `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
        PRIMARY KEY (`id`),
        UNIQUE KEY `phones_id_unique` (`id`)
      ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

      CREATE TABLE `addresses` (
        `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
        `partner_id` int(11) NOT NULL,
        `user_id` int(11) NOT NULL,
        `city` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
        `state` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
        `country` int(11) NOT NULL,
        `postalcode` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
        `address_1` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
        `address_2` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
        `description` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
        `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
        `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
        PRIMARY KEY (`id`),
        UNIQUE KEY `addresses_id_unique` (`id`)
      ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

      CREATE TABLE `users` (
        `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
        `email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
        `firstName` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
        `lastName` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
        `phoneNumber` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
        `password` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
        `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
        `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
        PRIMARY KEY (`id`),
        UNIQUE KEY `users_id_unique` (`id`),
        UNIQUE KEY `users_email_unique` (`email`)
      ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
1

1 Answers

0
votes

First of all, please, stop making SELECT tags like that if you're using blade for your templates. Use the "lists" method to get an array with key and values. This will save you a lot of time and will simplify your code a lot. This answer

About if it's possible to load only the phone row that matches the one in the main table: YES... but have you set relationships in models? Paste your model here