Laravel Eloquent hasManyThrough Relationship, SELECT without - laravel-4

I have 3 entities:
Company, User, Ticket
I need to select ALL companies that have no tickets, how can I manage that using Eloquent?
this are the relationships
class Company extends Eloquent {
protected $table = 'companies';
protected $fillable = array('name', 'priority', 'shortname', 'color');
public function users() {
return $this->hasMany('User', 'companies_id');
}
public function tickets() {
return $this->hasManyThrough('Ticket', 'User', 'companies_id', 'from_users_id');
}
}
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'users';
public function company() {
return $this->belongsTo('Company', 'companies_id');
}
public function tickets() {
return $this->hasMany('Ticket', 'from_users_id');
}
}
class Ticket extends Eloquent {
protected $table = 'tickets';
public function user() {
return $this->belongsTo('User', 'from_users_id');
}
}

It is not documented but Company::doesntHave('tickets') should work.
http://laravel.com/api/4.2/Illuminate/Database/Eloquent/Builder.html#method_doesntHave

Related

One to Many Relationship with Pivot Table and fetch related data using the with method

In Laravel 5.5 application, I have a model called Meeting and a model called Room.
Both models have a many-to-many relationship through a pivot table called MeetingSchedule.
The MeetingSchedule table has a column called schedule_by which is a foreign key to the user_id column in the User table.
I want to get the email address of the user who scheduled the meeting, when I fetch the meeting details with schedules() function of Meeting model.
I have tried the following code but it does not work:
class Meeting extends Model {
protected $table = 'Meetings';
protected $primaryKey = 'meeting_id';
protected $fillable = ['topic', 'description', 'participants_count'];
public function schedules() {
return $this->belongsToMany('App\Room', 'MeetingSchedule', 'meeting_id', 'room_id')
->using('App\MeetingSchedule')
->withPivot('schedule_id', 'start_time', 'end_time', 'scheduled_by')
->whereNull('MeetingSchedule.deleted_at')
->withTimestamps();
}
}
class Room extends Model {
protected $table = 'Rooms';
protected $primaryKey = 'room_id';
protected $fillable = ['name', 'location', 'capacity'];
public function schedules() {
return $this->belongsToMany('App\Meeting', 'MeetingSchedule', 'room_id', 'meeting_id')
->using('App\MeetingSchedule')
->withPivot('schedule_id', 'start_time', 'end_time', 'scheduled_by')
->whereNull('MeetingSchedule.deleted_at')
->withTimestamps();
}
}
class MeetingSchedule extends Pivot {
protected $table = 'MeetingSchedule';
protected $primaryKey = 'schedule_id';
public function scheduledBy() {
return $this->belongsTo('App\User', 'scheduled_by', 'user_id');
}
}
class User extends Authenticatable {
protected $table = 'User';
protected $primaryKey = 'user_id';
protected $fillable = ['username', 'email'];
public function meetingScheduledBy()
{
return $this->hasMany('App\MeetingSchedule', 'scheduled_by', 'user_id');
}
}
function getMeetingWithRelatedData($meetingId) {
return Meeting::where('meeting_id', $meetingId)
->with(['schedules'])
->first();
}

Relation One to One comes Null with Laravel

I am using Eloquent to create the queries but I have a relation One to One and I am trying to get the data and it returns null.
My models are:
//Customer:
class Customer extends Model
{
protected $table = 'customers';
protected $primaryKey = 'rut';
public function user() {
return $this->hasOne(User::class, 'rut');
}
}
//User:
class User extends Authenticatable
{
use Notifiable;
protected $primaryKey = 'rut';
public function customer()
{
return $this->belongsTo(Customer::class, 'rut');
}
}
And I am doing my query like this:
$customers = Customer::with('user')->paginate(10);
It returns data but the user data is null:
activity: "Particular"
address: "AV. GRECIA 690 LOC 5"
commune_id: 12
created_at: null
email: "lavanderiaom#gmail.com"
phone: null
rut: 4180753
updated_at: null
user: null
So I wonder how can I fix it? I mean why does it come null in user? what can the problem be?
Thanks!
You Make the relation one to one in the wrong way:
Customer belongs to a User Not the opposite ..
class User extends Authenticatable
{
use Notifiable;
protected $primaryKey = 'rut';
public function customer()
{
return $this->hasOne(Customer::class, 'customers.rut','users.rut');
}
}
class Customer extends Model
{
protected $table = 'customers';
protected $primaryKey = 'rut';
public function user() {
return $this->belongsTo(User::class, 'customers.rut','users.rut');
}
}

Laravel create simple RelationShip and get User data from other table

i'm created simple RelationShip from two table as User and Merchant :
class User extends Model implements AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword;
protected $table = 'users';
protected $fillable = ['name', 'email', 'password'];
protected $hidden = ['password', 'remember_token'];
public function setPasswordAttribute($value)
{
$this->attributes['password'] = Hash::make($value);
}
public function merchant()
{
return $this->hasMany('App\Merchant');
}
}
in App\Merchant is:
class Merchant
{
protected $table = 'merchand_web_service';
protected $fillable = ['agent_unique_id', 'agent_company_name', 'agent_company_logo'];
public function user()
{
return $this->belongsTo('App\User');
}
}
and my Migration file is:
class MerchandWebService extends Migration
{
public function up()
{
Schema::create('merchand_web_service',function(Blueprint $table){
$table->increments('id');
$table->string('customer_key','50');
$table->string('company_name','50');
$table->string('company_logo','100');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('merchand_web_service');
}
}
now i want to access User data on Merchant table on database by this code on Controller :
$all_records = Merchant::with('user')->orderBy('id', 'DESC')->paginate(50);
Unfortunately i get this error:
Call to undefined method app\Merchant::with()
You miss here extending Merchant class from basic Model.
It should be:
class Merchant extends Model
instead of
class Merchant
You should also look use exact same case for namespaces. Looking at error you use app\Merchant instead of App\Merchant

Laravel 5.1 Eloquent Relationship

Is there anyone can suggest from the eloquent relationship that I have based on the screenshot and model setup?
Model setup:
class Leaves extends Model
{
protected $table = 'leaves';
protected $fillable = [
'leave_type',
'user_id'
];
public function user()
{
return $this->belongsTo('App\User');
}
}
class LeaveType extends Model
{
protected $table = 'leave_type';
protected $fillable = ['type_name'];
}
class User extends Model implements AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword;
protected $table = 'users';
protected $fillable = ['name', 'email', 'password'];
protected $hidden = ['password', 'remember_token'];
public function leave()
{
return $this->hasMany('App\Leaves');
}
}
Currently I only able to get the leaves detail but need to retrieve the leave_type's type_name based on
$user = User::oldest('name')->get();
foreach ($users as $user) {
$user->leave()-get();
}
in your Leave model you would have
function type() {
return $this->hasOne(App\LeaveType);
}
in your LeaveType you should reciprocate
function leave() {
return $this->belongsToMany(App\LeaveType);
}
and in your controller:
$user = User::oldest('name')->with('leave', 'leave.type')->get();
dd($user->leave->type);
Leaves model:
class Leaves extends Model {
protected $table = 'leaves';
protected $fillable = [
'leave_type',
'user_id'
];
public function User()
{
return $this->belongsTo('App\User');
}
public function LeaveType()
{
return $this->belongsTo('App\LeaveType');
}
}
LeavesType model:
class LeaveType extends Model {
protected $table = 'leave_type';
protected $fillable = ['type_name'];
public function Leaves()
{
return $this->hasMany('App\Leaves');
}
}
User model:
class User extends Model implements AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword;
protected $table = 'users';
protected $fillable = ['name', 'email', 'password'];
protected $hidden = ['password', 'remember_token'];
public function Leaves()
{
return $this->hasMany('App\Leaves');
}
}

How can I select linked objects from multiple parent objects in Laravel Eloquent

I would like to do something like this.
Location::where('city', '=', 'Chicago')->chef();
With these relationships:
class Location extends Eloquent {
protected $table = 'locations';
public function chef() {
return $this->belongsTo('Chef');
}
}
class Chef extends Eloquent {
protected $table = 'chefs';
public function location() {
return $this->hasMany('Location');
}
}
This should work:
class Location extends Eloquent {
protected $table = 'locations';
public function chefs() {
return $this->belongsTo('Chef');
}
public function getAllChefsByCity($city)
{
$this->with('chefs')->where('city', $city)->get();
}
}
Then in your code:
$array = $location->getAllChefsByCity('Chicago');

Resources