phplaravellaravel-bladecrud

Empty attributes in Laravel


I'm trying to edit my database output but when I use dd($customerlist); I get empty attributes.

web.php (routes)

Route::get('/customer/{customer}/edit', 'App\Http\Controllers\CustomerListController@edit')->name('customer.edit');

CustomerListController.php (Controller)

    public function edit(Customerlist $customerlist)
    {
        dd($customerlist);
    }
        

customerlist.blade.php

        <tbody class="divide-y divide-gray-200">
            @foreach ($customers as $customer)
                <tr>
                    <td class="px-6 py-4 whitespace-nowrap">{{ $customer->Naam }}</td> 
                    <td class="px-6 py-4 whitespace-nowrap">{{ $customer['Bank_Account_Number'] }}</td> 
                    <td class="px-6 py-4 whitespace-nowrap">{{ $customer['Social_Security_Number'] }}</td> 
                    <td class="px-6 py-4 whitespace-nowrap">
                        <a href="{{ route('customer.edit', ['customer' => $customer]) }}" class="text-blue-500 hover:underline">Edit</a>
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap">
                        <form action="{{ route('customer.destroy', $customer['id']) }}" method="POST">
                            @csrf
                            @method('DELETE')
                            <button type="submit" class="text-red-500 hover:text-red-700">Delete</button>
                        </form>
                    </td>
                </tr>
            @endforeach
        </tbody>
    </table>

CustomerList.php (model)

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class CustomerList extends Model
{
    use HasFactory;

    protected $table = 'customerlists';
    protected $fillable = ['Naam', 'Bank_Account_Number', 'Social_Security_Number'];
}

There is more code in my web.php, CustomerListController and Blade file, i'm just showing what is in my eyes necessary, there are other crud actions in my controller that are 100% working. These are Create and Delete. My database also outputs on my site.

I hope someone can help me out, if you need more code to help just let me know!


Solution

  • The variable in your action must match the variable set in the route:

    public function edit(Customerlist $customer)
    

    The mechanism is called "implicit binding", here the full documentation.
    In short, the route variable name and the (type hinted) action variable name must match to allow retrieving the model from the DB using the key provided by calling the route, otherwise the system will only create one instance of a new model without filling it