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 App\Models\Chambres;
use App\Models\Plaques;
use App\Models\User;
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/" class="badge badge-primary" role="button"> </a></th>
<th scope="col"></th>
<th scope="col"></th>
<th scope="col"></th>
<th scope="col"><a href= "/admin/chambres//edit" class="badge badge-primary" role="button"> Modifier </a></th>
<th scope="col">
<form id="valider" action="/admin/chambres/" 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.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire