I am beginner in Laravel. I use Laravel 5.8 in my project.
I have this model:
User
class User extends Authenticatable implements MustVerifyEmail
{
use Notifiable;
use cms\Presenters\UserPresenter;
public static $roles = [];
public $dates = ['last_activity'];
protected $fillable = ['name', 'surname', 'email', 'email_verified_at', 'password', 'counter', 'url_address', 'isCompany', 'isMailing', 'content'];
protected $hidden = [
'password', 'remember_token',
];
public function roles()
{
return $this->belongsToMany('App\Role');
}
public function mainRole()
{
return $this->hasOne('App\Role');
}
public function favourites()
{
return $this->hasMany('App\Likeable');
}
}
Likeable
class Likeable extends Model
{
protected $quarded = [];
protected $fillable = ['likeable_id', 'user_id'];
public $timestamps = false;
}
My migration:
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name', 120)->nullable();
$table->string('surname', 120)->nullable();
$table->string('email', 120)->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');]
$table->char('isMailing', 1)->default(0);
$table->text('content')->nullable();
.....
$table->rememberToken();
$table->timestamps();
$table->engine = "InnoDB";
$table->charset = 'utf8mb4';
$table->collation = 'utf8mb4_unicode_ci';
});
}
Schema::create('likeables', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('likeable_id');
$table->bigInteger('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->engine = "InnoDB";
$table->charset = 'utf8mb4';
$table->collation = 'utf8mb4_unicode_ci';
});
I have a problem with displaying the list of users who have been added to the user's favorite list.
When I make this:
dd($request->user()->favourites);
I get this result:
Collection {#1031 ▼
#items: array:1 [▼
0 => Likeable {#1032 ▶}
]
}
And I need to get a user (likeables. Likeable_id = users.id)
How to do it? How do I fix my code to receive such a result?
from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/30VFaFU
via IFTTT
Aucun commentaire:
Enregistrer un commentaire