get extra data in Many To Many eloquent-relationships in laravel - php

I create a table like this:
posts
id title
categories
id name
category_post
post_id
category_id
custom_value
in Category model:
public function posts()
{
return $this->belongsToMany(Post::class);
}
with this command:
$category = Category::find(1);
$posts = $category->posts()->get();
I can get all of posts.
It is possible to return custom_value in category_post with each related post?

Use withPivot function
class Category extends Model
{
public function posts()
{
return $this->belongsToMany(Post::class)->withPivot('custom_value');
}
}

You can use withPivot() method
return $this->belongsToMany(Post::class)
public function posts()
{
return $this->belongsToMany(Post::class)->withPivot('column1','column2');
}
or
public function categories()
{
return $this->belongsToMany(Category::class)->withPivot('column1','column2');
}

Related

Laravel safest way to delete a many to many relationship data while sync (replace) with another

I have a project with Product, Category relationship which is Many to Many
// Product Model
public function categories()
{
return $this->belongsToMany(Category::class);
}
//Category Model
public function products()
{
return $this->belongsToMany(Product::class);
}
Now, when a some category gets deleted, I want to assign it's products to a default category (ID = 1).
What is the best way to achieve this with Laravel 8
Finally I found the solution thanks to Kevin
public static function boot()
{
parent::boot();
static::deleting(function ($category) {
$products = $category->products;
if ($products->isNotEmpty()) {
$category->products()->detach();
$defaultCategory = static::find(1);
$defaultCategory->products()->sync($products->pluck('id')->toArray());
}
});
}
You might want to try the deleting event:
class Category extends Model
{
public static function booted()
{
static::deleting(function ($category) {
$products = $category->products()->get();
if ($products->isNotEmpty()) {
$category->products()->detach();
$defaultCategory = static::find(1);
$defaultCategory->products()->sync($products->pluck('id')->toArray());
}
})
}
}

How to fetch data along with relationship data in Laravel using with keyword?

How can I fetch data along with blog category and blog tags from Blogs table using with in query.
Below is my model and controller code, I am getting Get Blogs Api Error instead of the blogs data.
Blog Controller
public function getBlogs()
{
try {
$blogs = Blog::where('status', 1)
->with('category')
->with('tag')
->with('user')
->with('comment')
->orderBy('id', 'desc')
->paginate(5);
return response()->json($blogs);
} catch (\Illuminate\Database\QueryException $e) {
$e = "Get Blogs Api Error";
return response()->json($e);
}
}
Blog Model
class Blog extends Model
{
public function user()
{
return $this->belongsTo(User::class);
}
public function category()
{
return $this->hasMany(Category::class);
}
public function tag()
{
return $this->hasMany(Tag::class);
}
public function comment()
{
return $this->hasMany(Comment::class);
}
}
User Model
public function blog_user()
{
return $this->hasMany(Blog::class);
}
Blog Category Model
public function blog_category()
{
return $this->belongsTo(Blog::class);
}
Blog Tag Model
public function blog_tag()
{
return $this->belongsTo(Blog::class);
}
Blog Comment Model
public function blog_comment()
{
return $this->belongsTo(Blog::class);
}
Database table structure
blogs table structure
blog_categories table structure
blog_tags table structure
First of all change names to plural. not singular. as you are using one to many. and use belongsToMany() method. not hasMany().
public function categories(){
return $this->belongsToMany(Category::class);
}
and change the name of pivot table to blog_category not blog_categories. It will work. and your BlogCategory model will look like this.
class BlogCategory extends Model {
protected $table = 'blog_category';
public function blog() {
return $this->belongsTo( Blog::class );
}
}
now you can get blogs like this.
$blogs = Blog::with( 'categories' )->get();
and this is how you will fetch blog for any category.
$category = BlogCategory::where( 'category_id', $category->id )->first();
dd( $category->blog );

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');
}

Retriving two models and order them by date

I'm trying to retrive the 20 most recent elements from two differents models Post and Link and rank them by the field created_at in descending order.
Here is the link class
class Link extends \Illuminate\Database\Eloquent\Model {
public $incrementing = false;
public function author() {
return $this->belongsTo('App\Models\Author');
}
public function category() {
return $this->belongsTo('App\Models\Category');
}
}
And here the post class
class Post extends \Illuminate\Database\Eloquent\Model {
public $incrementing = false;
public function author() {
return $this->belongsTo('App\Models\Author');
}
public function category() {
return $this->belongsTo('App\Models\Category');
}
}
How can I do that with eloquent (I'm using it outside Laravel, with Slim)?
use the below code to get recent records
orderBy('created_at', 'DESC')->get();
The merge method was the answer :
$posts = \App\Models\Post::where('draft', false)->orderBy('created_at', 'desc')->take(20)->get();
$links = \App\Models\Link::orderBy('created_at', 'desc')->take(20)->get();
$result = $posts->merge($links)->sortByDesc('created_at')->take(20);

Picking the right type of relationship using Laravel and Eloquent

I have three tables in my database.
Posts
Authors
Categories
When viewing an Author page I want to be able to view all of the author's Posts and also the category of the post.
When viewing a Category index page I want to be able to view all of the Posts for that category and also include the Author with each Post.
When viewing a Post I want to be able to include the Category and Author
What type of relationship can I use to achieve this?
One to one, One to Many, Many to many, or polymorphic
Thanks in advance.
You can create your relations like this:
class Post extends Eloquent {
public funcion category()
{
return $this->belongsTo('Category');
}
public funcion author()
{
return $this->belongsTo('User');
}
}
class Author extends Eloquent {
public funcion posts()
{
return $this->hasMany('Post');
}
}
class Category extends Eloquent {
public funcion posts()
{
return $this->hasMany('Post');
}
}
And then use them this way:
$author = Author::find(1);
foreach($author->posts as $post)
{
echo $post->title;
echo $post->author->name;
}
$category = Category::find(1);
foreach($category->posts as $post)
{
echo $post->title;
echo $post->author->name;
}
$post = Post::find(1);
echo $post->category->title;
echo $post->author->name;
I have done something like this :
db table =
users : id name
debates : id post user_id
now in model
class User extends SentryUserModel {
public function debates()
{
return $this->hasMany('Debate', 'user_id');
}
}
and
class Debate extends Eloquent {
public function user()
{
return $this->belongsTo('User', 'id');
}
}
now in query
$debate= Debate::find(1);
echo $debates->user->name;
echo $debates->user->id;
it is giving a null result.
Changing this two solve the problem . (Do now know why we cant use foreign key here. If anyone know this please do inform ).
class User extends SentryUserModel {
public function debates()
{
return $this->hasMany('Debate');
}
}
and
class Debate extends Eloquent {
public function user()
{
return $this->belongsTo('User');
}
}

Categories