Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Shivamyadav's avatar

Showing error ..

Hello sir @jeffreyway , @Sinnbeck sir ..help me or anybody can able to help me! :) This error is coming from this episode https://laracasts.com/series/laravel-8-from-scratch/episodes/12?page=5only Showing error, when I dd($posts) evrything is fine except the date is showing null also my yamlFrontmatter code

---
title: My First Post
excerpt: Lorem Ipsum is simply dummy text of the printing and typesetting industry.
date: 2022-10-29
---


<p>
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
Too few arguments to function App\Models\Post::__construct(), 0 passed in C:\laragon\www\blog\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\HasAttributes.php on line

my model code

 public $title;
    public $date;
    public $excerpt;
    public $body;

    public function __construct($title, $date, $excerpt, $body)
    {
        $this->title = $title;
        $this->$date = $date;
        $this->excerpt = $excerpt;
        $this->body = $body;
    }

my routes code

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

        );
    }
    
    // dd($posts[3]->body);
    return view('posts', compact('posts'));
});
0 likes
18 replies
tykus's avatar

Seems like your Post model might incorrectly extending Illuminate\Database\Eloquent\Model? But you don't show all of the relevant Post model class file.

Shivamyadav's avatar

@tykus my model Post file

<?php

namespace App\Models;

use Illuminate\Support\Facades\File;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\ModelNotFoundException;


class Post extends Model
{
    use HasFactory;
    public $title;
    public $date;
    public $excerpt;
    public $body;

    public function __construct($title, $date, $excerpt, $body)
    {
        $this->title = $title;
        $this->$date = $date;
        $this->excerpt = $excerpt;
        $this->body = $body;
    }

    public static function allPost()
    {
        $files = File::files(resource_path("posts"));
        return array_map(fn($file) => $file->getContents(), $files );
         
    }
    
    public static function find($slug)
    {
        $path = resource_path("posts/{$slug}.html");
        if(!file_exists($path))
        {
          throw new ModelNotFoundException();
        }
       return cache()->remember("posts.{slug}", 1200, fn() => file_get_contents($path));
    // return file_get_contents($path);
    }
}
tykus's avatar

@Shivamyadav yeah, like I said above - do not extend Model

<?php

namespace App\Models;

use Illuminate\Support\Facades\File;

class Post
{
    public $title;
    // ... etc
Shivamyadav's avatar

@tykus now getting this error

Object of class App\Models\Post could not be converted to string
Shivamyadav's avatar

@tykus in blade file

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="{{ url('css/app.css') }}">
</head>
<body>
   @foreach ($posts as $post)
        <article>
            <h1> {{ $post->title }}</h1>
        </article>
   @endforeach
   
</body>
</html>
tykus's avatar

@Shivamyadav there is nothing in that Blade file to cause the error message you are showing. What does the full stack trace look like?

tykus's avatar

@Shivamyadav if this error message does not occur during the tutorial then you have (again) done something different; so that is why I ask you to share your code and your stacktrace.

Shivamyadav's avatar

@tykus it showing in the line no 12

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <meta http-equiv="X-UA-Compatible" content="ie=edge">

    <title>Document</title>

    <link rel="stylesheet" href="<?php echo e(url('css/app.css')); ?>">

</head>

<body>



    <?php $__currentLoopData = $posts; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $post): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>

        <?php echo $post; ?> //This is a error in the stacktrace



    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>



</body>

</html><?php /**PATH C:\laragon\www\blog\resources\views/posts.blade.php ENDPATH**/ ?>
REQU
tykus's avatar

@Shivamyadav well, there you go... you are trying to echo an Object that does not have a __toString method. The reason is because you loop does not look like this:

   @foreach ($posts as $post)
        <article>
            <h1> {{ $post->title }}</h1>
        </article>
   @endforeach

I see no <article> tag, no <h1> tag, and you are not echoing the $post->title in the compiled view code.

Shivamyadav's avatar

@tykus finded out sir, sorry my bad i have 2 blade files having same approximately that's why i am changing in another file and trying to clear cace routes all shit things..

Shivamyadav's avatar

@tykus yeah ! sir, getting another error Undefined variable $slug

my post model code

<?php

namespace App\Models;

use Illuminate\Support\Facades\File;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\ModelNotFoundException;


class Post
{
    use HasFactory;
    public $title;
    public $date;
    public $excerpt;
    public $body;
    public $slug;

    public function __construct($title, $date, $excerpt, $body, $slug)
    {
        $this->title = $title;
        $this->$date = $date;
        $this->excerpt = $excerpt;
        $this->body = $body;
        $this->$slug = $slug;
    }

    public static function allPost()
    {
        $files = File::files(resource_path("posts"));
        return array_map(fn($file) => $file->getContents(), $files );
         
    }
    
    public static function find($slug)
    {
        $path = resource_path("posts/{$slug}.html");
        if(!file_exists($path))
        {
          throw new ModelNotFoundException();
        }
       return cache()->remember("posts.{slug}", 1200, fn() => file_get_contents($path));
    // return file_get_contents($path);
    }
}

route code


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

shoing this error stacktrace



Route::get('/', function () {

    $files = File::files(resource_path("posts"));

    $posts = [];

    foreach($files as $file)

    {

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

        $posts[] = new Post(

            $document->title,

            $document->date,

            $document->excerpt,

            $document->$slug, //this line slug error

            $document->body(),



       );

    }



    // dd($posts[3]->body);

    return view('posts', compact('posts'));

});
tykus's avatar
tykus
Best Answer
Level 104

@Shivamyadav oh my... you're not doing well:

public function __construct($title, $date, $excerpt, $body, $slug)
{
    $this->title = $title;
    $this->date = $date;
    $this->excerpt = $excerpt;
    $this->body = $body;
    $this->slug = $slug;

and

$posts[] = new Post(
    $document->title,
    $document->date,
    $document->excerpt,
    $document->slug, // not $slug
    $document->body()
)
1 like

Please or to participate in this conversation.