skip to Main Content

I’ve got 2 arrays that I’ve created from a 2 SQL queries.

I’m wanting to turn them into a named array,and then complete an array merge the reason for this is that they have different information and are named differently you’ll see there are 2, array variables $AddonDomainSelect and $PackageSelect.

When doing a DD on the either $cpanelAcounts or $AddonDomains I’m only getting 1 of the results back instead of the many that should be in the array.

This has been attempted to be merged into the same array. but it errors in my view when passed in there.

Controller

    <?php

namespace AppHttpControllersManage;

use AppHttpControllersController;
use IlluminateHttpRequest;
use Gufy;
use AppPackage;
use AppAddonDomain;
use AppUser;

class WebsiteManage extends Controller
{
    public function index(){
      $userid = auth()->user()->client_id;
      
      $AddonDomainSelect = AddonDomain::select('username', 'domain', 'server', 'addon_domain', 'doc_root')
                           ->where('client_id', '=', $userid)
                           ->get();
       $AddonDomains = [];
            foreach ($AddonDomainSelect as $AddonDomain) {
  
        $AddonDomains = [
          'username' => $AddonDomain['username'], 
          'server' => $AddonDomain['server'],
          'domain' => $AddonDomain['addon_domain'],
          'doc_root' => $AddonDomain['doc_root']
        ];
          
    }     
      
      
      
      
      $PackageSelect = Package::select('username', 'domain', 'serverhostname')
                           ->where('client_id', '=', $userid)
                           ->get();
       
      
      $cpanelAcounts = [];
      foreach ($PackageSelect as $cpanelAccount) {
      
        $cpanelAcounts = [ 'username' => $cpanelAccount['username'], 
          'server' => $cpanelAccount['serverhostname'],
          'domain' => $cpanelAccount['domain'],
          'doc_root' => '/'];
          
    }     
             
   $AllWebsites = array_merge($AddonDomains, $cpanelAcounts);
     
                          
      return view('manageweb.AllWebsites',[
        'websites' => $AllWebsites,
                ]);
    }
}

View

 `@extends('layouts.standardHeader')
    
    @section('content')
    
    <section id="auto-layout-columns">
    <div class="app-content content">
      <div class="content-wrapper">
          <div class="content-header row" >
     
       
      
      <div class="content-header-left col-12 mb-2 mt-1">
                <div class="row breadcrumbs-top">
                  <div class="col-12">
                    
                    <div class="breadcrumb-wrapper col-12">
                      <ol class="breadcrumb p-0 mb-0">
                        <li class="breadcrumb-item"><a href="index.html"><i class="bx bx-home-alt"></i></a>
                        </li>
                        <li class="breadcrumb-item"><a href="#">Dashboard</a>
                        </li>
                        <li class="breadcrumb-item active">Manage Websites
                         </li>
                      </ol>
                    </div>
                    
                  </div>
                </div>
              </div>
      </div>
    
    <div class="row" id="basic-table">
      
           
       <div class="col-12">
          <div class="card">
             <div class="card-header">
                <h4 class="card-title">Manage Websites</h4>
             </div>
            
             <div class="card-content">
               
                <div class="card-body">
                   <p class="card-text"> 
                   </p>
            <div class="col-md-6 ">
                            
                                <div class="input-group">
                                  <input type="text" class="form-control" onkeyup="myFunction()" id="myInput" placeholder="Search Domain Name" aria-describedby="button-addon2">
                                  <div class="input-group-append" id="button-addon2">
                                    
                                  </div>
                                </div>
                             <td class="text-center py-1">
                       
                          </div>
                  <br>
                  
                   <!-- Table with outer spacing -->
                   <divn id="myTable"class="table-responsive">
                      <table class="table">
                         <thead>
                            <tr>
                               <th>Domain</th>
                               <th>cPanel user</th>
                               <th>Server Hostname</th>
                              <th>Document Root  </th>
                               
                               <th></th>
                            </tr>
                         </thead>
                         <tbody>
                          @foreach ($websites as $website)
                            <tr>
                               <td class="text-bold-500">{{$website->domain}}</td>
                               <td>{{$website->username}}</td>
                               <td class="text-bold-500">{{$website->serverhostname}}</td>
                               <td>/</td>
                               
                               <td> <a href="#" class="btn btn-info shadow mr-1 mb-1">Manage</a></td></td>
                            </tr>
                           @endforeach
                         </tbody>
                      </table>
                   </div>
                </div>
               
             </div>
          </div>
       </div>
    </div>
        <script>
    function myFunction() {
      var input, filter, table, tr, td, i, txtValue;
      input = document.getElementById("myInput");
      filter = input.value.toUpperCase();
      table = document.getElementById("myTable");
      tr = table.getElementsByTagName("tr");
      for (i = 0; i < tr.length; i++) {
        td = tr[i].getElementsByTagName("td")[0];
        if (td) {
          txtValue = td.textContent || td.innerText;
          if (txtValue.toUpperCase().indexOf(filter) > -1) {
            tr[i].style.display = "";
          } else {
            tr[i].style.display = "none";
          }
        }       
      }
    }
    </script>
        
        
        
        
    </div>
      </div>
      </section>
    @endsection`

I get the following error.

Trying to get property ‘domain’ of non-object (View: /var/www/cp/resources/views/manageweb/AllWebsites.blade.php)

Highlighing "{{$website->domain}}" in the error logs.

Thanks for looking hope you can spot what I’m doing wrong.

2

Answers


  1. Change this in your blade file

    <td class="text-bold-500"> {{ $website['domain'] }}</td>
    <td> {{ $website['username'] }}}</td>
    <td class="text-bold-500"> {{ $website['serverhostname'] }}</td>
                     
    
    Login or Signup to reply.
  2. The problem you are having, alongside the already mentioned way of using the array values, instead of object properties, is in your two arrays, that you are merging

    foreach ($PackageSelect as $cpanelAccount) {
    
            $cpanelAcounts = [ 'username' => $cpanelAccount['username'], 
              'server' => $cpanelAccount['serverhostname'],
              'domain' => $cpanelAccount['domain'],
              'doc_root' => '/'];
    
        }
    

    this will produce in the end, that $cpanelAcounts is just a flat array with keys: ‘username’, ‘server’, ‘domain’ and ‘doc_root’, so when you go deeper in it with the "foreach", you are iterating the values of these keys – neither of them has a subarray with key ‘domain’ and that is the error
    I think you want it to be an array of arrays
    so it must be

    $cpanelAcounts[] = [ 'username' => $cpanelAccount['username'], 
              'server' => $cpanelAccount['serverhostname'],
              'domain' => $cpanelAccount['domain'],
              'doc_root' => '/'];
    

    or use array_push()

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search