2

when I try to run the following it gives the Error Exception where it can't read the title on array. Can you let me know how to fix this?

**My post code from app/models and as below **

 <?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);
    
        });
    
    
        
      
       

    }


}

and my routes class code is

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()
       );
    }
});

The code is right as far as i know, I just don't know if there is something I need to add or do something to make the error go away.

2 Answers2

3

I think the error is really here

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()
   );
}

I think you probably need to do

foreach ($files as $file) {
   // you load an array here !!!
   $document = YamlFrontMatter::parseFile($file);
   $posts[]= new Post(
          $document->title,        
          $document->excerpt,
          $document->date,
          $document->body()
       );
}
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
3

error is inside of this foreach loop

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

specifically in here

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

you add new object into an array $document and in here

$document->title,

you try to access the title directly from $document, you need to specify the index of array to access the object with the attribute, something like this

$document[0]->title,

or you could rewrite the first part into

$document = YamlFrontMatter::parseFile($file);
Lukas Grofcik
  • 457
  • 1
  • 3
  • 15