i have problem with sending loading data in the view ! (laravel)

37 Views Asked by At

Controller is:

public function show($id)
{
    $asset = Assets::with(['location', 'userUpdatedBy', 'manufacturer.countries', 'tags', 'supplier', 'comments', 'parent', 'workorder', 'attachments'])->where('id', $id)->get()->first();
    $asset->load('attachments');
    dd($asset->attachments);
    return view('admin.assets.show', ['asset' => $asset, 'attachments' => $asset->attachments]);
}

dd($asset->attachments); results in null on app\Http\Controllers\Admin\AssetsController.php:595

But ($asset) results in:

App\Models\Assets {#2311 ▼ // app\Http\Controllers\Admin\AssetsController.php:595
  #connection: "mysql"
  #table: "assets"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  +preventsLazyLoading: false
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #escapeWhenCastingToString: false
  #attributes: array:29 [▶]
  #original: array:29 [▶]
  #changes: []
  #casts: []
  #classCastCache: []
  #attributeCastCache: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: array:9 [▼
    "location" => null
    "userUpdatedBy" => null
    "manufacturer" => null
    "tags" => Illuminate\Database\Eloquent\Collection {#2326 ▶}
    "supplier" => null
    "comments" => Illuminate\Database\Eloquent\Collection {#2325 ▶}
    "parent" => Illuminate\Database\Eloquent\Collection {#2324 ▶}
    "workorder" => Illuminate\Database\Eloquent\Collection {#2315 ▶}
    "attachments" => Illuminate\Database\Eloquent\Collection {#2349 ▼
      #items: array:2 [▼
        0 => App\Models\Attachment {#2351 ▼
          #connection: "mysql"
          #table: "attachments"
          #primaryKey: "id"
          #keyType: "int"
          +incrementing: true
          #with: []
          #withCount: []
          +preventsLazyLoading: false
          #perPage: 15
          +exists: true
          +wasRecentlyCreated: false
          #escapeWhenCastingToString: false
          #attributes: array:12 [▶]
          #original: array:12 [▶]
          #changes: []
          #casts: []
          #classCastCache: []
          #attributeCastCache: []
          #dates: []
          #dateFormat: null
          #appends: []
          #dispatchesEvents: []
          #observables: []
          #relations: []
          #touches: []
          +timestamps: true
          #hidden: []
          #visible: []
          #fillable: []
          #guarded: []
        }
        1 => App\Models\Attachment {#2352 ▶}
      ]
      #escapeWhenCastingToString: false
    }
  ]
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #fillable: []
  #guarded: []
}

i dont know how the dd($asset) clearly returns attachments but when i try to access attachments using dd($asset->attachments); or in the view it returns null,

i know that i can simply send attachments in a separate query , but i try to keep the code as simple and clean as possible as well as to follow laravel standard

please help me or guide me through this issue

0

There are 0 best solutions below