How to delete relation table's relations on laravel 4.2? For example, I have a table called category which is related to subcategory. So, category id is related to subcategory as foreign key and mapped for cascade delete in my migrations. Now subcategory table has relations with objects table. So subcategory id is related to objects as foreign key and mapped for cascade on delete. Now when I delete category, subcategory is getting deleted which is fine. But since subcategory is getting deleted, even objects too should get deleted right? But it is not. How to solve this?. Below is my code.
Category Model
use Illuminate\Database\Eloquent\SoftDeletingTrait;
class Category extends Eloquent{
use SoftDeletingTrait;
protected $dates = ['deleted_at'];
protected $table = "categories";
public function subcategory(){
return $this->hasMany('Subcategory', 'category_id');
}
public static function boot()
{
parent::boot();
Category::deleting(function($category) {
$category->subcategory()->delete();
});
}
}
SubCategory model
use Illuminate\Database\Eloquent\SoftDeletingTrait;
class Subcategory extends Eloquent{
use SoftDeletingTrait;
protected $dates = ['deleted_at'];
public $timestamps = false;
protected $table = "subcategories";
public function parent(){
return $this->belongsTo('Category', 'category_id');
}
public function objects(){
return $this->hasMany('Object', 'subcategory_id');
}
public static function boot()
{
parent::boot();
Subcategory::deleting(function($subcategory) {
$subcategory->objects()->delete();
});
}
}
You need to call ->delete() on each model directly to trigger associated model events
Category::deleting(function($category) {
foreach($category->subcategory as $subcategory){
$subcategory->delete();
}
});
Related
I have made a separate Model and defined the required relationships between all my Many To Many Models:
class AttributeProduct extends Model
{
use HasFactory;
protected $table = 'attribute_product';
protected $guarded = [];
public function attribute()
{
return $this->belongsTo(Attribute::class, 'attribute_id', 'id');
}
public function product()
{
return $this->belongsTo(Product::class, 'product_id', 'id');
}
public function value()
{
return $this->belongsTo(Value::class, 'value_id', 'id');
}
}
For example, this Model is connected to attribute_products table.
And then I added this method to Product model:
public function attributeproducts()
{
return $this->hasMany(AttributeProduct::class, 'product_id', 'id');
}
So I wonder if it is good to make a separate model and add my own methods to it or I should use Laravel pre-defined way?
I have 3 tables (workflow, user, workflow_user) and I would like to select the view column of the workflow_user table.
class Workflow extends Model
{
public function user()
{
return $this->belongsToMany(User::class,'workflow_user');
}
}
class User extends Model
{
public function works()
{
//return $this->belongsToMany(Role::class);
return $this->belongsToMany(Workflow1::class,'workflow_user');
}
}
workflow_user table
class WorkflowUser extends Model
{
protected $table = 'workflow_user';
protected $fillable = [
'workflow1_id','user_id','view'
];
protected $primaryKey = 'id';
public $timestamps = false;
}
To get the data from the workflow_user table I do this
$workflow = User::find($idconnect)->works()->orderBy('created_at','desc')->paginate(10);
When I make this request it does not give me the data of the workflow_user(workflow1_id,user_id,view) table.
If you have a model for the pivot table, you should have it extend the Pivot class and use it in the relationship's definition.
Also, you need to manually include the fields that are not the foreign ids in the query result.
class Workflow extends Model
{
public function user()
{
return $this->belongsToMany(User::class, 'workflow_user', 'workflow_id', 'user_id')
->using(WorkflowUser::class)
->withPivot(['id', 'view']);
}
}
class User extends Model
{
public function works()
{
return $this->belongsToMany(Workflow::class, 'workflow_user', 'user_id', 'workflow_id')
->using(WorkflowUser::class)
->withPivot(['id', 'view']);
}
}
workflow_user table
class WorkflowUser extends Pivot
{
protected $table = 'workflow_user';
protected $fillable = ['workflow_id', 'user_id', 'view'];
protected $primaryKey = 'id';
public $incrementing = true;
public $timestamps = false;
}
$workflow = User::findOrFail($idconnect)
->works()
->orderBy('created_at', 'desc')
->paginate(10);
I've two tables one is car_category having the fields - id,type.
Another table named vehicle having field - c_id(FK Refers car - id).
Now I want to display the FK(c_id) value which is car-type.
I've below code in models,
class Car extends Model
{
protected $guarded = [];
protected $table = 'car_category';
public function vehicles()
{
return $this->hasMany('Vehicle');
}
}
vehicle model,
class Vehicle extends Model
{
protected $guarded = [];
protected $table = 'vehicles';
public function cars()
{
return $this->belongsTo('Car');
}
}
What 'll be my query for this? I've tried this code, results error.
$vehicles = "SELECT cars.cartype,vehicles.model FROM cars,vehicles
WHERE cars.id = vehicles.c_id";
How can I achieve this? Can anybody help me?
Try this
class Car extends Model
{
protected $guarded = [];
protected $table = 'car_category';
public function vehicles()
{
return $this->hasMany(Vehicle::class, 'c_id');
}
}
The vehicle model
class Vehicle extends Model
{
protected $guarded = [];
protected $table = 'vehicles';
public function cars()
{
return $this->belongsTo(Car::class, 'c_id');
}
}
Eloquent determines the foreign key of the relationship based on the model name. In this case, the Car model is automatically assumed to have a car_id foreign key. If you wish to override this convention, you may pass a second argument to the method
https://laravel.com/docs/5.5/eloquent-relationships#one-to-one
To get the Car along with their Vehicle information you can do a query using Eager Loading
$result = Car::with('vehicles')->get();
To get the Car along with their Vehicle information you can do a query using Eager Loading
$result = Car::with('vehicles')->get();
One more correction you have specified class name as string literals without specifying FQN, relationships in models should be defined using fully qualified name
Car Model
class Car extends Model
{
protected $guarded = [];
protected $table = 'car_category';
public function vehicles()
{
return $this->hasMany(\App\Models\Vehicle::class);
}
}
Vehicle Model
class Vehicle extends Model
{
protected $guarded = [];
protected $table = 'vehicles';
public function cars()
{
return $this->belongsTo(\App\Models\Car::class);
}
}
Change from class car to class Car
After that you can select with Car::first(), the related table data can be found in Car::first()->vehicles
You can also add a where() method on models, if you have more than one record, use a foreach()
In Model,
class Vehicle extends Model
{
protected $guarded = [];
protected $table = 'vehicles';
public function cars()
{
return $this->belongsTo(Car::class, 'c_id');
}
}
In controller,
$vehicles = Vehicle::all();
return view('vehicles.vehicle',['vehicles'=>$vehicles]);
I have a couple of tables like so:
Authors Table
- id
- book_id
- name
Books Table
- id
- ref
- author
- title
The ref on the books table is the same as the book_id on the authors table, but i can't seem to get the authors books by doing this in my view:
{{ $author->books->first()->title }}
My models for authors and books look like so:
class Authors extends \Eloquent {
protected $guarded = ['id'];
public $table = 'authors';
public $timestamps = false;
public function books()
{
return $this->hasMany('Books', "author","name");
}
}
class Books extends \Eloquent {
protected $guarded = ['id'];
public $timestamps = false;
public function author()
{
return $this->hasOne('Authors');
}
public function categories(){
return $this->hasMany('Categories');
}
}
Really need some help on this please.
You can pass the foreign key and local key in hasMany but it looks like you're passing author as the foreign key and name as the local key. It should be ref for the foreign key and book_id for the local key.
class Authors extends \Eloquent {
protected $guarded = ['id'];
public $table = 'authors';
public $timestamps = false;
public function books()
{
return $this->hasMany('Books', "ref", "book_id");
}
}
class Books extends \Eloquent {
protected $guarded = ['id'];
public $timestamps = false;
public function author()
{
return $this->hasOne('Authors');
}
public function categories(){
return $this->hasMany('Categories');
}
}
I have two models, User and Badge. A user can have multiple badges, and a badge can belong to multiple users. (using a pivot table)
Currently I am getting the data I need, but additionally I am getting the pivot table along. How do I exclude this?
Here's the User model:
class User extends Eloquent {
public function badges() {
return $this->belongsToMany('Badge', 'users_badges');
}
}
And the Badge model:
class Badge extends Eloquent {
public function users() {
return $this->belongsToMany('User', 'users_badges');
}
}
Add pivot to your $hidden property's array in your model(s).
class Badge extends Eloquent {
protected $hidden = ['pivot'];
public function users() {
return $this->belongsToMany('User', 'users_badges');
}
}
And same with your User model
class User extends Eloquent {
protected $hidden = ['pivot'];
public function badges() {
return $this->belongsToMany('Badge', 'users_badges');
}
}
Or you can still hide the pivot on demand this way...
$user = User::find(1);
$user->badges->makeHidden('pivot');
$badge = Badge::find(1);
$badge->users->makeHidden('pivot');