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');
}
}
Related
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();
}
public function index()
{
$query = Activity::orderBy('id','DESC')->with('provinces');
if(!Auth::user()->hasRole('Administer')){
$query=$query->where('province_id', Auth::user()->id);
}
$activities = $query->latest()->get();
return view('activity.index',compact('activities'));
}
How to display owner of a post in laravel admin list display?
My User Model
class User extends Authenticatable implements HasMedia
{
public function activities()
{
return $this->hasMany(Activity::class);
}
}
My Activity Model:
class Activity extends Model
{
protected $guarded = [];
public function user()
{
return $this->belongsTo(User::class);
}
public function provinces()
{
return $this->belongsToMany(Province::class);
}
}
My province Model
class Province extends Model
{
protected $fillable = [ 'title', 'post_id'];
public function activities()
{
return $this->belongsToMany(Activity::class);
}
public function user()
{
return $this->belongsToMany(User::class);
}
}
I want to show the content to the user by province.
And also the admin can see all the content.
please help me i am new in laravel
Based on your comment, you should change your province conditions to:
if (!Auth::user()->hasRole('Administer')) {
$query = $query->where('province_id', Auth::user()->province_id);
}
You have defined that each activity belongs to an user. So can access owner of each activity like this:
$activity->user->name;
And if your activity belongs to a province, you should define your relation in this way:
class Activity extends Model
{
protected $guarded = [];
public function user()
{
return $this->belongsTo(User::class);
}
public function province()
{
return $this->belongsTo(Province::class);
}
}
Also for your Province model:
class Province extends Model
{
protected $fillable = [ 'title', 'post_id'];
public function activities()
{
return $this->hasMany(Activity::class);
}
public function users()
{
return $this->hasMany(User::class);
}
}
And this is best practice to use eager loading for fetching related records. It will look like this one:
$query = Activity::with(['user', 'province'])->orderBy('id','DESC');
Also another hint: try to use better variable names. You can use $activities instead of $query.
I'm facing a problem, as to why the relation is not getting connected.
vendor.php
class Vendor extends Model
{
public function internets()
{
return $this->hasMany('App\Internet');
}
}
internets.php
class Internet extends Model
{
protected $with=['vendor','coba'];
public function vendor()
{
return $this->belongTo('App\Vendor');
}
}
any one can help me?
it will be belongsTo
return $this->belongsTo('App\Vendor');
Try something like this
In your vendor
class Vendor extends Model
{
protected $table = 'Vendor Table Name';
protected $primarykey = 'Your Vendor Table primary Key';
public function internets()
{
return $this->hasMany(App\Internet::class, 'relational key');
}
}
And In your internet
class Internet extends Model
{
protected $with=['vendor','coba'];
protected $table = 'Internet Table Name';
protected $primarykey = 'Internet table primary key';
public function vendor()
{
return $this->belongsTo(App\Vendor::class, 'relation key');
}
}
I hope this will solve your problem let me know if this do the trick
I have 3 tables and these table are not according laravel
job_post Table
job_post_id
name
job_functional_area Table (has many with job_post table and belongsTo with functional_area)
job_functional_area_id
job_post_id
functional_area_id
functional_area
functional_area_id
name
now I want to fetch job_post functional area
Relation like this job_post > job_functional_area > functional_area
In cakephp we use contain in laravel i don't have an idea.
I have try Has Many Through but it will not work in this condition. Please help me
Models
class JobPost extends Model
{
protected $table = 'job_post';
protected $primaryKey = 'job_post_id';
}
class JobFunctionalArea extends Model {
protected $table = 'job_functional_area';
protected $primaryKey = 'job_functional_area_id';
}
class FunctionalArea extends Model {
protected $table = 'functional_area';
protected $primaryKey = 'functional_area_id';
}
Your models should look something like this.
class JobPost extends Model {
protected $table = 'job_post';
protected $primaryKey = 'job_post_id';
public function jobFunctionalAreas() {
return $this->belongsToMany('App\JobFunctionalArea');
}
}
class JobFunctionalArea extends Model {
protected $table = 'job_functional_area';
protected $primaryKey = 'job_functional_area_id';
public function jobPosts() {
return $this->hasMany('App\JobPost');
}
public function functionalArea() {
return $this->belongsTo('App\FunctionalArea');
}
}
class FunctionalArea extends Model {
protected $table = 'functional_area';
protected $primaryKey = 'functional_area_id';
public function jobFunctionalAreas() {
return $this->hasMany('App\JobFunctionalArea');
}
}
Also in the relations you can add your own custom $primaryKey.
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