Skip to content
Advertisement

Laravel: Property [name] does not exist on this collection instance while trying to populate field in a Edit view

I’m just starting to learn Laravel and while following Laracast “Laravel from scratch” series I got to the point of creating an edit page in which a have to get the id of a product and populate the inputs with existing data, but following the instructions in the video I keep getting the error

“Property [name] does not exist on this collection instance”.

ProductController.php

public function edit(Product $id)
{
    $product = Product::find($id);

    return view('admin.product.edit', compact('product'));
}

edit.blade.php

<div class="container">
<div class="row justify-content-center">
    <div class="col-md-8">
        <div class="card">
            <div class="card-header">Edit product</div>

            <div class="card-body">
                <form action="/product" method="post">
                    @csrf
                    <input type="text" name="name" id="name" placeholder="Name" value="{{$product->name}}">
                    <br>
                    <textarea name="description" id="descriptopn" cols="30" rows="10" placeholder="Description">{{$product->description}}</textarea>
                    <br>
                    <input type="number" name="price" id="price" min="0.1" step="0.01" placeholder="Price" value="{{$product->price}}">
                    <br>
                    <input type="number" name="stock" id="stock" min="1" step="1" placeholder="Stock" value="{{$product->stock}}">
                    <br>
                    <button type="submit">Save</button>
                </form>
            </div>
        </div>
    </div>
</div>

If it’s any help I’m currently using Laravel v8.22.1 (PHP v8.0.0), and I believe at the time of recording the series, Laracast used Laravel v6.

Edit: Adding table structure.

Migration file:

create_products_table.php

public function up()
{
    Schema::create('products', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->text('description')->nullable();
        $table->float('price');
        $table->integer('stock');
        $table->timestamps();
    });
}

Edit #2: Adding Product Model

Product.php

class Product extends Model
{
    use HasFactory;
}

web.php

Route::get('/', function () {
return view('welcome');
});

Auth::routes();

Route::get('/home', [AppHttpControllersHomeController::class, 'index'])->name('home');

//Admin routes
Route::get('/product', [ProductController::class, 'index']);
Route::post('product', [ProductController::class, 'store']);
Route::get('/product/create', [ProductController::class, 'create']);
//Route::get('/product/{article}', [ProductController::class, 'show']);
Route::get('/product/{article}/edit', [ProductController::class, 'edit']);
Route::put('/product/{product}', [ProductController::class, 'update']);

Thank you everyone for your answers I remember using this same style of logic in Laravel 6 and everything worked, but now I’ve had issues with Laravel 8, if a get rid of the error message with the suggestions of @ByWaleed and @vozaldi then the inputs in the edit.blade.php don’t get populated with the necessary data, Firefox console doesn’t show any errors and when using dd() it shows as follows

dd($id):

AppModelsProduct {#303 ▼
#connection: null
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: false
+wasRecentlyCreated: false
#attributes: []
#original: []
#changes: []
#casts: []
#classCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
#guarded: array:1 [▼
   0 => "*"
  ]
}

dd($product)

IlluminateDatabaseEloquentCollection {#1184 ▼
    #items: []
}

Advertisement

Answer

Thank you everyone for your time

All of your answers where very helpful, but it turns out i just made a typo.

In the edit route Route::get('/product/{article}/edit', [ProductController::class, 'edit']); I wrote {article} instead of {product} (Both the model and controller where named Product) as stated by @ByWaleed. After making that correction everything works correctly.

As a recomendation for future users, if posible use Route::resource() instead of writing each route individually it makes it less likely to make a mistake as te one i did.

Again thank you everyone.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement