vendredi 18 juin 2021

Trying to get property of non-object - name

i am trying to print the username, but it gives me error

Trying to get property of non-object

Models

User:

<?php

namespace App\Models;
use App\Models\Registro;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
        'email',
        'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
    public function registros(){
      
       
         return $this->belongsTo('\App\Models\Registro', 'user_id');
    }
}

Registro:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use \App\Models\User;

class Registro extends Model
{
    use HasFactory;

    public function user(){
        return $this->hasMany('\App\Models\User');

    }
   
}

database:

enter image description here

in blade.php:

    

maybe I'm doing something wrong with relationships, but I can't figure it out on my own

<tbody class="list">
            @foreach($registros as $registro)
              <tr>
                <th scope="row">
                  <div class="media align-items-center">
                    <a href="#" class="avatar rounded-circle mr-3">
                      <img alt="Image placeholder" src="../../assets/img/theme/bootstrap.jpg">
                    </a>
                    <div class="media-body">
                      <span class="name mb-0 text-sm"></span>
                    </div>
                  </div>
                </th>
            
              </tr>
             @endforeach
            </tbody>

try changed relationships but it doesn't work does not allow me to print the name of user_id, help pls



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire