Laravel BelongsTo works fine but in Nova not displaying data - laravel

I have an application with many question responses. I am not using the traditional foreign key naming conventions but when I query either object, the relationships pull up correctly. Using the current Laravel Nova v4.
class Application extends Model
{
public function questionResponses(): HasMany
{
return $this->hasMany(QuestionResponse::class, 'application_ec_id', 'ec_id');
}
}
class QuestionResponse extends Model
{
public function application(): BelongsTo
{
return $this->belongsTo(Application::class, 'application_ec_id', 'ec_id');
}
}
Within Laravel Nova, when I open an Application, I see the references to multiple question responses. However when I open an individual QuestionResponse, the application relationship just shows a hyphen like it's missing; it does not display the Application reference.
This is from the Application Nova Resource
HasMany::make('Question Responses'), // WORKS
But this does not work in the QuestionResponse Resource
BelongsTo::make('Application'), // does not work
BelongsTo::make('Application', 'application', Application::class), // doesn't work either
Text::make('application ec id'), // just to make sure the correct ID is in place
Even when I check the browser console, I see this request pulling the correct data. I am not sure what else to check.
// this URL returns a fields array and the correct record ID for application is present
domain.test/nova-api/question-responses/58649?relationshipType=

Related

Laravel nova and belongsToThrough?

Is there a way to display in Laravel Nova Resource a relation like this ?
$report->meter->user
where
- a report belongs to a meter
- a meter belongs to a user
I am in report ressource and I want to display related user
I struggled with this lately. What we seek is a BelongsToThrough relationship field. That type of relationship isn't natively supported by Laravel, which means there's no field for it in Nova either. However, you can easily implement it in Laravel by installing this package: https://github.com/staudenmeir/belongs-to-through
You can then add your report relationship to the User model.
// App\Models\Report
use \Znck\Eloquent\Traits\BelongsToThrough;
public function report()
{
return $this->belongsToThrough('App\Models\User','App\Models\Meter');
}
And you can use a regular BelongsTo relationship within Nova.
// App\Nova\Report
public function fields(Request $request)
{
return [
BelongsTo::make('User')
]
}
Note: This would work for the Report index/details views, but you'll want to exclude it from the edit form. (Consider a computed Text field, if you want to display the user on the edit form.)

Laravel eager loading a BelongsTo relation doesn't work

This seems extremely simple and yet it doesn't work:
// Reservations
class Reservation extends Model
{
public function service()
{
return $this->belongsTo('App\Landing');
}
}
// Service
class Landing extends Model
{
public function reservation()
{
return $this->hasMany('App\Reservation');
}
}
And then in my controller I have:
$x = Reservation::with('service')->get();
$y = Reservation::all()->load('service');
None of these work. I've tried several ways of loading it and none of them work. It always returns an empty result for the service.
Any other result works fine with eager loading (even with nesting) except te BelongsTo - this one.
The eager loading works.
The problem is your relationship
When you define a BelongsTo relationship you need to specify a foreign key if the name of your property does not correspond to the entity being referenced
For example: if you call the relationship "landing", you will be fine, because under-the-hood, Laravel passes the foreign key landing_id based on the name of the property
class Reservation extends Model
{ //landing correspond to the lowercase (singular) name of the Landing class
//in that case Laravel knows how to set the relationship by assuming
//that you want to match landing_id to the id of the landings table
public function landing()
{
return $this->belongsTo(Landing::class);
}
}
If you chose to name the relationship differently, such as "service", then you need to specify the foreign key ex: landing_id since service and landing are two different words, and landing correspond to the lowercase version of the actual class Landing. Otherwise Laravel would think your foreign key is "service_id" instead of landing_id
class Reservation extends Model
{
//service is a custom name to refer to landing
//in that case Laravel needs you to specify the foreign key
public function service()
{
return $this->belongsTo(Landing::class, 'landing_id');
}
}
Read more here: https://laravel.com/docs/5.8/eloquent-relationships#updating-belongs-to-relationships

Laravel model create relationships twice between two tables

I am trying to create relation between two tables, users and messages in Laravel models, as the user can send a message to another user so that I have two foreign-keys (fromUser_id and toUser_id) as shown in the image below.
For the first relation it is straightforward that I will create a function with the name messages
public function messages(){
return $this->hasMany('App\Models\Message', 'fromUser_id');
}
However I do not know how to name the second relation as far as I know it should be messages too, according to the standard naming of Laravel, which will obviously issue an error as we have the first function with the same name.
public function messages(){
return $this->hasMany('App\Models\Message', 'toUser_id');
}
Would you please let me know what should I name it and how this will affect the models.
Well, you should not use simple messages as relationship but rather use receivedMessages and sentMessages like this:
public function sentMessages()
{
return $this->hasMany('App\Models\Message', 'fromUser_id');
}
public function receivedMessages()
{
return $this->hasMany('App\Models\Message', 'toUser_id');
}

how to display the current logged on users picture from a relationship laravel

I want to retrieve the user's photo and display it in a thumbnail form which i have stored in public/assets/uploads/thumbnail/. I tried auth()->user()->user_detail->file_name but I can't get it to work. How do you do it ?
you have to first define a relationship if they are stored in different table
like i did in model
public function imagedata() {
return $this->hasMany(Images::class, 'listID', 'id');
}
and after that when you get the user just call this method like this
$listingimg = Listings::findOrfail($id);
and for calling the relationship
foreach (listingimg as $singleIlisting) {
$singleIlisting->imagedata;
}
modify the code according your needs as if needed and by the way relatio is one to many

Laravel get related data from 3 tables

I have three tables: users, emails,attachments. User table is connected with emails by user_id. Emails table is connected with attachments by email_id.
My question is: How should I make it look eloquent in laravel to get all users their emails and their attachments? (I know how get all user and they emails but I don't know how to add attachments.)
Depending on your database relationship,you may declare a relationship method in your Email model, for example:
// One to One (If Email has only one attachment)
public function attachment()
{
return $this->hasOne(Attachment::class);
}
Otherwise:
// One to Many (If Email contains more than one attachment)
public function attachments()
{
return $this->hasMany(Attachment::class);
}
To retrieve the related attachment(s) from Email model when reading a user using id, you may try something like this:
$user = User::with('email.attachment')->find(1); // For One-to-One
$user = User::with('email.attachments')->find(1); // For One-to-Many
Hope you've already declared the relationship method in the User model for Email model using the method name email.
Note: make sure, you have used right namespace for models. It may work if you've done everything right and followed the Laravel convention, otherwise do some research. Also check the documentation.

Resources