Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

310
Views
Intento de leer la propiedad "título" en la matriz (Excepción de error) Laravel Php

cuando trato de ejecutar lo siguiente, aparece la excepción de error donde no puede leer el título en la matriz. ¿Puedes decirme cómo arreglar esto?

** Mi código postal de la aplicación/modelos y como se muestra a continuación **

 <?php namespace App\Models; use Illuminate\Database\Eloquent\ModelNotFoundException; // use Illuminate\Support\Facades\File; class Post { public $title; public $excerpt; public $date; public $body; public function __construct($title, $excerpt, $date, $body) { $this-> title = $title; //this is where the error is occurring $this-> excerpt = $excerpt; $this-> date = $date; $this-> body = $body; } public static function all() { $files = File::files(resource_path("postss/")); return array_map(function ($file){ return $file-> getContents(); }, $files); } public static function find($slug) { base_path(); if (!file_exists($path = resource_path("postss/{$slug}.html"))) { throw new ModelNotFoundException(); } return cache()-> remember("posts.{$slug}", 7 , function () use ($path){ return file_get_contents($path); }); } }

y mi código de clase de rutas es

 Route::get('/', function () { $files = File::files(resource_path("postss")); $posts =[]; foreach ($files as $file) { $document[] = YamlFrontMatter::parseFile($file); $posts[]= new Post( $document->title, $document->excerpt, $document->date, $document->body() ); } });

El código es correcto hasta donde yo sé, simplemente no sé si hay algo que deba agregar o hacer algo para que desaparezca el error.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Creo que el error está realmente aquí.

 foreach ($files as $file) { // you load an array here !!! $document[] = YamlFrontMatter::parseFile($file); $posts[]= new Post( $document->title, // then you use $document as a scalar? $document->excerpt, $document->date, $document->body() ); }

Creo que probablemente necesites hacer

 foreach ($files as $file) { // you load an array here !!! $document = YamlFrontMatter::parseFile($file); $posts[]= new Post( $document->title, $document->excerpt, $document->date, $document->body() ); }
over 4 years ago · Santiago Trujillo Report

0

el error está dentro de este bucle foreach

 foreach ($files as $file) { $document[] = YamlFrontMatter::parseFile($file); $posts[]= new Post( $document->title, $document->excerpt, $document->date, $document->body() ); }

específicamente aquí

 $document[] = YamlFrontMatter::parseFile($file);

agrega un nuevo objeto en una matriz $ documento y aquí

 $document->title,

intenta acceder al título directamente desde $document, debe especificar el índice de la matriz para acceder al objeto con el atributo, algo como esto

 $document[0]->title,

o podría reescribir la primera parte en

 $document = YamlFrontMatter::parseFile($file);
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!