Get Eloquent (Laravel) to return object without relationship - laravel-4

Please consider this method of in the User model:
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
use Cartalyst\Sentry\Users\Eloquent\User as SentryUserModel;
class User extends SentryUserModel implements UserInterface, RemindableInterface {
//[...]
public function company()
{
Log::info("SPOOFED", array($this->getSpoofedCompanyId()));
// == 1
if($this->getSpoofedCompanyId() > 0) {
return Company::find($this->getSpoofedCompanyId());
}
else {
return $this->belongsTo('Company');
}
}
This produces the following error:
LogicException
Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation
I guess I'm not allowed to return an object which has no relation? Is there any way to bypass this?
The reason is that I'm letting a superuser impersonate another user.

Related

laravel eloquent relationship for indirectly related model

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.

How to get reverse in laravel one to many 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

one to many relationship laravel with multiple foreign key

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

How to get access to relationship in model User Laravel?

I have default User model:
class User extends Authenticatable implements HasRoleContract
{
use Notifiable, HasRole;
}
With one relationship inside:
public function distributor() {
return $this->hasOne('App\DistributorContacts', 'distributor_id', 'id');
}
So, when user passed authorization I can not see this relation in object:
{{dd(Auth::user())}}
you may use ->with('distributor') on your user Object to get relationships loaded.
e.g.
$user = new User()->with('distributor');
dd($user->distributor);
or
Auth::user()->with('distributor');

How to seed in Laravel?

I'm trying to seed my DB following the instructions on http://laravel.com/docs/migrations#database-seeding:
class DatabaseSeeder extends Seeder {
public function run()
{
$this->call('UserTableSeeder');
$this->command->info('User table seeded!');
}
}
class UserTableSeeder extends Seeder {
public function run()
{
DB::table('users')->delete();
User::create(array('email' => 'foo#bar.com'));
}
}
I'm a bit confused by this. What is User in User::create(array('email' => 'foo#bar.com'));?
The create method inserts a record in the database (Seeding is a way of pre-populating the database).
It basically invokes the Model named User and uses the Create() method in a static manner by passing an array and returns an instance of the model representing the user entity with the passed details.

Resources