I am on a project with Laravel.
I have two database tables that are in a One-To-Many relationship with each other. They are joined by three conditions. How do I model this relationship in Eloquent?
I am not supposed to modify the database schema, since it has to remain backward compatible with other things.
I have tried the following, but it doesn't work.
The owning side:
use Illuminate\Database\Eloquent\Model;
class Route extends Model
{
public function trips()
{
return $this->hasMany('Trip', 'route_name,source_file', 'route_name,source_file')
}
}
The inverse side:
use Illuminate\Database\Eloquent\Model;
class Trip extends Model
{
public function routes()
{
return $this->belongsTo('Route', 'route_name,source_file', 'route_name,source_file');
}
}
I don't want to use "use Awobaz\Compoships\Compoships;"
You need to give hasMany() and belongsTo method the namespace of your models.
Something like 'App\Models\Trip' or Trip::class
class Route extends Model
{
public function trips()
{
return $this->hasMany('App\Models\Trip', 'route_name,source_file', 'route_name,source_file')
}
}
class Trip extends Model
{
public function routes()
{
return $this->belongsTo('App\Models\Route', 'route_name,source_file', 'route_name,source_file');
}
}
Related
I want a relationship where two unrelated models are linked together with a linker model.
My tables are like:
card table
id(pk)
name
User table
id(pk)
username
password
card_id(fk)
Feature table
id(pk)
name
card_id(fk)
How can i define an eloquent relationship to access all the features of user's card from user model like this:
class User extends Model
{
use HasFactory;
public function features(){
return $this->relatonship_statement;
}
}
and when I tried in Card Model:
class Card extends Model
{
use HasFactory;
public function features(){
return $this->hasMany(Feature::class);
}
}
and in User model:
class User extends Model
{
use HasFactory;
public function card(){
return $this->belongsTo(User::class);
}
public function features(){
return $this->card->features;
}
}
I get error:
App\Models\User::features must return a relationship instance.
What you really want is an accessor function, not a relationship. This is how you would do to achieve what you want.
class User extends Model
{
use HasFactory;
protected $appends = ['features']; //to append features in the response json
public function card(){
return $this->belongsTo(Card::class); //editted User to Card
}
public function getFeatures(){ //accessor method
return $this->card->features()->get();
}
}
sample result after returning user in a controller function
return User::query()->with('card')->first();
However, the right way is to access the features through the Card Relationship because these two models have a direct relationship.
I am writing an application that I use laravel eloquent relationship
table structure
clients
id
payroll_id
name
Payroll
id
payroll_id
basic
hmo
Client Model
use Illuminate\Database\Eloquent\Model;
class AdminClient extends Model
{
public function payroll()
{
return $this->hasMany(Payroll::class, "payroll_id", "payroll_id");
}
}
Payroll Model
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Payroll extends Model
{
public function clients()
{
return $this->belongsTo(AdminClient::class, "payroll_id", "payroll_id");
}
}
I am able to do
dd(AdminClient::with("payroll")->get());
but the reverse below is not working
dd(Payroll::with("clients")->get());
I am getting a null value for the clients.
Any Solutions Thank you.
use this in AdminClient
public function payroll()
{
return $this->hasMany(Payroll::class);
}
and use this in Payroll
public function clients()
{
return $this->belongsTo(AdminClient::class);
}
or if it not work then use this in AdminClient
public function payroll()
{
return $this->hasMany(Payroll::class, "forign key of AdminClient");
}
and use this in Payroll
public function clients()
{
return $this->belongsTo(AdminClient::class, "payroll_id");
}
hope its work
In my project there is a Many-to-Many polymorphic relationship (Many instances of a model called Package (morphedByMany) can contain many different content types (each MorphedToMany).
I've created a pivot table containing some additional fields that I'll need to access and query by, and so I've decided that the best thing would be to create a Pivot model by extending the MorphPivot.
Querying is now easy enough, however, I can't access the content through a relation (which I can do if i query the App\Packages::findOrFail(1)->contentType()). I know I should declare that the pivot belongsTo the contentType, but I'm not sure how to go about it seeing as it could belong to any of the morphed contentTypes.
EDIT
Code blocks as requested
Content1
class Song extends Model
{
public function packages()
{
return $this->morphToMany('App\Package', 'packageable')->withPivot('choice')->using('App\PackageContent');
}
Content2
class Video extends Model
{
public function packages()
{
return $this->morphToMany('App\Package', 'packageable')->withPivot('choice')->using('App\PackageContent');
}
Package
class Package extends Model
{
public function songs()
{
return $this->morphedByMany('App\Song', 'packageable')->withPivot('choice')->using('App\PackageContent');
}
public function videos()
{
return $this->morphedByMany('App\Video', 'packageable')->withPivot('choice')->using('App\PackageContent');
}
MorphPivot
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\MorphPivot;
class PackageContent extends MorphPivot
{
protected $table = 'packageables';
Pivot table migration
public function up()
{
Schema::create('packageables', function (Blueprint $table) {
$table->integer('package_id');
$table->integer('packageable_id');
$table->string('packageable_type');
$table->integer('choice');
});
}
I have the following SQL tables for example:
guards
guard_id guard_name guard_type
roles
role_id role_name security_priviledge
guard_roles
gr_id guard_id role_id
How would I define this relationship in Laravel?
You have to define two Models, App/Guard.php and App/Role.php and define their Many to Many relationship. For example:
On App/Guard.php:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Guard extends Model
{
public function roles()
{
return $this->belongsToMany('App\Role');
}
}
On App/Role.php:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Role extends Model
{
public function guard()
{
return $this->belongsToMany('App\Guard');
}
}
I am a bit new to Laravel. I am trying to delete a project from a table along with its images and plans from 2 other tables. How to do this in Laravel Eloquent?
Here is the delete controller of the project:
public function destroy($id)
{
$project = Projects::findOrFail($id);
$project->delete();
return Redirect::to('admin/view-project')->with('message', 'Project deleted successfully');
}
How can I get this to be done from the model? I didn't understand that.
Here is the Projects model:
class Projects extends Eloquent implements UserInterface, RemindableInterface
{
use UserTrait, RemindableTrait;
protected $table = 'project_info_arabic';
public function projectImages()
{
return $this->hasMany('ProjectsImage');
}
public function projectPlans()
{
return $this->hasMany('ProjectsPlans');
}
}
Can kindly anybody help?
The better way when thinking about data consistency is the implementation of foreign keys on the database layer. That does it automatically for you and you don't need to think about it anymore.
See https://laravel.com/docs/5.3/migrations#foreign-key-constraints
You could try model events in laravel.Check this
class Projects extends Eloquent implements UserInterface, RemindableInterface
{
public static function boot()
{
parent::boot();
Projects::deleted(function($project)
{
$project->projectImages()->delete();
$project->projectPlans()->delete();
});
}
}