skip to Main Content

I am facing this error ‘Attempt to read property "nom" on null’ in Laravel 10. though the User eloquent model work fine beside for Plaques;

Here is my Code:

Plaques Model:

class Plaques extends Model
{
    use HasFactory;

    protected $table = 'Plaques';

    protected $fillable = ['nom', 'zone', 'el_plaque', 'tranche', 'gc_mecanise', 'gc_trad', 'gpon', 'ville', 'status_plaque', 'closed_by' , 'created_by'];

    public function chambres()
    {
        return $this->hasMany(Chambres::class);
    }

    public function immeuble()
    {
        return $this->hasMany(Immeuble::class);
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }
}

Chambres Model:

class Chambres extends Model
{
    use HasFactory;

    protected $table = 'Chambres';

    protected $fillable = ['ref_chambre', 'type_chambre', 'chambre_terminer', 'loc_x_chambre', 'loc_y_chambre', 'plaque_id', 'user_id', 'status_chambre', 'created_by', 'closed_by', 'affected_by'];

    public function photos()
    {
        return $this->hasMany(Photos::class);
    }

    public function immeuble()
    {
        return $this->hasMany(Immeuble::class);
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function plaques()
    {
        return $this->belongsTo(Plaques::class);
    }

}

ChambresController:

use AppModelsChambres;
use AppModelsPlaques;
use AppModelsUser;

class ChambresController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        return view('admin.chambres.index')
        ->with('chambres', Chambres::get());
    }

View:

<tbody>
            @forelse ($chambres as $chambre)
            <tr>
                <th scope="col"><a href= "/admin/chambres/{{ $chambre->id }}" class="badge badge-primary" role="button">{{ $chambre ->ref_chambre }} </a></th>
                <th scope="col">{{ $chambre->type}}</th>
                <th scope="col">{{ $chambre->plaques->nom}}</th>
                <th scope="col">{{ $chambre->user->name}}</th>
                <th scope="col"><a href= "/admin/chambres/{{ $chambre->id }}/edit" class="badge badge-primary" role="button"> Modifier </a></th>
                <th scope="col">
                    <form id="valider" action="/admin/chambres/{{ $chambre->id }}" method="post" class="hidden">
                    @csrf
                    @method('delete')
                    <button class="btn" role="button">Supprimer</button>
                </form>
                </th>
            </tr>

Chambres Migration:

        Schema::create('chambres', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('user_id')->nullable();
            $table->unsignedBigInteger('plaque_id')->nullable();
            $table->string('ref_chambre')->unique();
            $table->string('type_chambre');
            $table->date('chambre_terminer')->nullable()->change();
            $table->string('loc_x_chambre');
            $table->string('loc_y_chambre');
            $table->boolean('status_chambre')->nullable();
            $table->string('created_by')->nullable();
            $table->string('closed_by')->nullable();
            $table->string('affected_by')->nullable();
            $table->foreign('plaque_id')->references('id')->on('plaques')->onDelete('cascade');
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->timestamps();
        });

i want to get ‘nom’ column value from ‘Plaques’ table which has foreign key on ‘Chambres’ table.

2

Answers


  1. Because $chambre->plaques is null, you can not get the nom property. You can consider to fix it like below:

    <th scope="col">{{ $chambre->plaques ? $chambre->plaques->nom : ''}}</th>
    <th scope="col">{{ $chambre->user ? $chambre->user->name : ''}}</th>
    

    Try pass ‘plaque_id’ as the second parameter

     public function plaques()
     {
       return $this->belongsTo(Plaques::class, 'plaque_id');
     }
    

    Or change your model Plaques to Plaque

    Login or Signup to reply.
  2. You can querying plaques like below in your controller to fix the empty data load issue:

    $chambres = Chambres::with('plaques')->get();
    

    Change view code like below to display empty string if plaques return null value.

    <th scope="col">{{ $chambre->plaques ? $chambre->plaques->nom : '' }}</th>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search