Laravel 9 laravel associate polymorphic relations bookmarks - php

I have this Bookmark model
public function bookmarkable()
{
return $this->morphTo();
}
public function user()
{
return $this->belongsTo(User::class);
}
and in my User model
public function bookmarks()
{
return $this->hasMany(Bookmark::class);
}
public function posts()
{
return $this->hasMany(Post::class);
}
and the Post model
public function bookmarks()
{
return $this->morphMany(Bookmark::class, 'bookmarkable');
}
public function user()
{
return $this->belongsTo(User::class);
}
I want to create a new $bookmark and associate a $post with that bookmark
the bookmarks table has the columns bookmarkable_type, bookmarkable_id and user_id

Cause morphTo extends belongsTo
U can use like this
$post = Post::first()// the post u want add to bookmark
$newBookmark = new Bookmark([]);
$newBookmark->name="lorem ipsum";
$newBookmark->bookmarkable()->associate($post);
$newBookmark->user()->associate($post->user);
$newBookmark->save();
See the class docs:
https://laravel.com/api/9.x/Illuminate/Database/Eloquent/Relations/MorphTo.html

Related

Relationship between laravel models does not work

I have 2 models: one for users and one for clients. A user is a customer
User has a 'codigocli' field and client has a 'codigo' field
The relationships between my models are like this:
//User model
public function cliente()
{
return $this->hasOne(Cliente::class, 'codigo', 'codigocli');
}
//Cliente model
public function user()
{
return $this->belongsTo(User::class, 'codigocli', 'codigo');
}
My database is fine (I think) client has the 'codigo' field and users has the 'codigocli' field. So what am I doing wrong? When I want to query my home.blade.php with dd(auth()->user()-cliente()) I don't get anything, although it shows me the parent object fine.
H
You have an OneToOne relationship here so try this if you don't change id name:
//User model
public function cliente()
{
return $this->hasOne(Cliente::class, 'codigocli');
}
//Cliente model
public function user()
{
return $this->belongsTo(User::class, 'codigocli');
}
if you change id name:
//User model
public function cliente()
{
return $this->hasOne(Cliente::class, 'codigocli','local_id_name' );
}
//Cliente model
public function user()
{
return $this->belongsTo(User::class, 'codigocli', 'local_id_name');
}

How to set relation in laravel with two ids?

I have these two tables:
product
- id
- name
favorites
- id
- product_id
- user_id
So, a user can add product to favorites only once. How can I set up this relation something like the following?
public function favorites() {
return $this->hasOne(Favorite::class, 'user_id', 'product_id')
}
So, I want to use both product_id & user_id such that the query would return proper result as per the following:
Get me the wishlist of user with id 1 and product with id 13!
you can do something like that:
in favourite Model class:
public function product(){
return $this->belongsTo('App\Product');
}
public function user(){
return $this->belongsTo('App\User');
}
In Product Model Class:
public function favorites(){
return $this->hasMany('App\Favorite');
}
In User Model Class:
public function favorites(){
return $this->hasMany('App\Favorite');
}
The user may have many favorites, so in Class User
public function favorites()
{
return $this->hasMany('App\Favorite');
}
Class Favorite
public function product()
{
return $this->belongsTo('App\Product');
}
public function user()
{
return $this->belongsTo('App\User');
}
If you have user you can
$userFavProducts = $user->favorites;
$product2 = $user->favorites()->where('product_id', 2)->get();
You should try this:
Favorites Model
public function product(){
return $this->belongsTo('App\Product','product_id');
}
public function user(){
return $this->belongsTo('App\User','user_id');
}

How to paginate for hasMany relationship in laravel (App\News::user must return a relationship instance)

I am not able to paginate in laravel in this situation
return $this->hasMany('App\News','category_id')->orderBy('id','desc')->paginate(20);
but says error. in controller I have also tried
$byCategories=Category::findOrFail($id)->paginate(20);`
it also says error.
help me.
My Model is
class Category extends Model
{
public function news()
{
return $this->hasMany('App\News','category_id')->orderBy('id','desc');
}
public function newsMany()
{
return $this->belongsToMany('App\News')->paginate(20);
}
public function user()
{
return $this->belongsTo('App\User');
}
public function getRouteKeyName()
{
return 'slug';
}
}
another model one is
class News extends Model
{
public function user()
{
return $this->belongsTo('App\User');
}
public function category()
{
return $this->belongsTo('App\Category');
}
}
my controller code is
public function byCategory($id)
{
$byCategories=Category::findOrFail($id);
return view('back-end/news/byCategory', compact('byCategories'));
}
Thank you so much.
Pagination is not done in the Model
it is to be done in the controller, For example remove (->paginate(20);) from here
public function newsMany()
{
return $this->belongsToMany('App\News')->paginate(20);
}
keep it only as
public function newsMany()
{
return $this->belongsToMany('App\News');
}
and call the pagination in controller when returning the view
$news=Category::findOrFail($id)->newsMany()->paginate(20);
return view('view name', compact('news'));

Laravel- 5.5 App\Comment::user must return a relationship instance.

I want to display products comments. But When I do that, it gives me above error. How can I fix that ?
I'm using One To Many relationship beetwen product-comments and user-comments
Product model;
public function comments(){
return $this->hasMany('App\Comment','product_id','id');
}
User Model;
public function comments() {
return $this->hasMany('App\Comment','user_id','id');
}
Comment Model;
public function user(){
$this->belongsTo('App\User');
}
public function product(){
$this->belongsTo('App\Product');
}
Blade file
<figcaption class="text-center">{{$comment->user->username}}</figcaption>
You need to return relationship. So add return to the user() relationship definition method:
public function user()
{
return $this->belongsTo('App\User');
}
The same is with the product() relationship:
public function product()
{
return $this->belongsTo('App\Product');
}

Eloquent Relationships (Laravel 5.4)

I'm making a Laravel 5.4 application, but I'm having a hard time figuring out how I should structure my data with eloquent relationships.
This is my models and how I want them to be related:
School → Has classes, users and events
User → Can belong to a school. Can have classes and sessions (with cases)
Class → Belongs to a school. Has users and subjects. Can have homework
Subject → Belongs to a class
Session → Belongs to a user. Can have cases
Case → Belongs to a session
Event → Belongs to a school
Homework → Belongs to a class
How should I structure this with eloquent relation functions (belongsTo, hasMany and so on) in my Laravel 5.4 project?
Assuming Class, User and Event models has a property school_id and the primary key you ant to use is id of the respective model, your Class, User, Event and School models should look like as follow.
School
class School extends Model
{
public function users(){
return $this->hasMany('App\User');
}
public function classes(){
return $this->hasMany('App\Class');
}
public function sessions(){
return $this->hasMany('App\Session');
}
}
User
class User extends Model
{
public function school(){
return $this->belongsTo('App\School');
}
public function classes(){
return $this->hasMany('App\Class');
}
public function events(){
return $this->hasMany('App\Event');
}
}
Class
class Class extends Model
{
public function school(){
return $this->belongsTo('App\School');
}
public function users(){
return $this->hasMany('App\User');
}
public function subjects(){
return $this->hasMany('App\Subject');
}
public function homeworks(){
return $this->hasMany('App\Homework');
}
}
Event
class Class extends Model
{
public function school(){
return $this->belongsTo('App\School');
}
}
You can use these relationships to define queries with chaining capability. e.g. if you want to get all the events associated with a School that has a id property equals to $id you can write,
$events = App\School::find($id)->events;
Laravel Documentation explains it well
The correct way to do this is
SCHOOL
public function classes()
{
return $this->hasMany('App\Class');
}
public function users()
{
return $this->hasMany('App\User');
}
public function events()
{
return $this->hasMany('App\Event');
}
CLASS
public function school()
{
return $this->belongsTo('App\School');
}
public function subjects()
{
return $this->hasMany('App\Subject');
}
public function homeworks()
{
return $this->hasMany('App\Homework');
}
public function users()
{
return $this->belongsToMany('App\User','class_users','class_id','user_id');
// this should be many to many because users can also have many classes
}
USER
public function school()
{
return $this->belongsTo('App\School');
}
public function classes()
{
return $this->belongsToMany('App\Class','class_users','user_id','class_id');
// this should be many to many as explained to class
}
public function sessions()
{
return $this->belongsToMany('App\Session','session_users','user_id','session_id');
// like classes do, this should be many to many relationship because sessions can also have many users
}
SUBJECT
public function class()
{
return $this->belongsTo('App\Class');
}
SESSION
public function users()
{
return $this->belongsToMany('App\User','session_users','session_id','user_id');
// should be many to many as well
}
public function cases()
{
return $this->hasMany('App\Case');
}
CASE
public function session()
{
return $this->belongsTo('App\Session');
}
EVENT
public function school()
{
return $this->belongsTo('App\School');
}
HOMEWORK
public function class()
{
return $this->belongsTo('App\Class');
}
With the School model and underlying table created, it’s time to create the relation. Open the School model and create a public method named classes, users and events; inside it referencing the hasMany method:
School :
class School extends Model {
public function classes()
{
return $this->hasMany('App\Class');
}
public function users()
{
return $this->hasMany('App\User');
}
public function events()
{
return $this->hasMany('App\Event');
}
}
User :
class User extends Model {
public function school(){
return $this->belongsTo('App\School');
}
public function classes(){
return $this->hasMany('App\Class');
}
public function sessions(){
return $this->hasMany('App\Session');
}
}
Class :
class Class extends Model {
public function school(){
return $this->belongsTo('App\School');
}
public function users(){
return $this->hasMany('App\User');
}
public function subjects(){
return $this->hasMany('App\Subject');
}
public function homeworks(){
return $this->hasMany('App\Homework');
}
}
Subject :
class Subject extends Model {
public function class(){
return $this->belongsTo('App\Class');
}
}
Session:
class Session extends Model {
public function user(){
return $this->belongsTo('App\User');
}
public function cases(){
return $this->hasMany('App\Case');
}
}
Case :
class Case extends Model {
public function session(){
return $this->belongsTo('App\Session');
}
}
Event :
class Event extends Model {
public function school(){
return $this->belongsTo('App\School');
}
}
Homework:
class Homework extends Model {
public function class(){
return $this->belongsTo('App\Class');
}
}
For more details of hasMany relationship, Please check the link here : EasyLaravelBook

Categories