I have a HasOne relationship between 2 models.
In Laravel Nova, is it possible to merge them into a single panel when viewing details, and update both model together when updating details.
This feature is not officially supported in laravel nova. but there is a workaround to achieve it using model accessors and mutators. this answer can help you
Example:
// in the model
public function getProfilePictureAttribute()
{
// Don't forget to check the relation here if you don't want any error on Nova.
return $this->profile ? $this->profile->picture : null;
}
public function setProfilePictureAttribute($value)
{
$profile = $this->profile;
$profile->fill(['picture' => $value]);
$profile->save();
}
// in the resource
Image::make('Profile picture')
This question is quite old... but for Nova 4, this could be easily done by adding ->required()...
HasOne::make('Address')->required(),
MorphOne::make('Address')->required(),
etc... This will display the fields of second resource on the same page, and insert/update accordingly.
Related
I've started using Filament PHP for creating a Laravel based intranet application but stumbled across a question I couldn't answer myself using the official documentation:
What's the easiest way to show relational data inside the view page of a resource?
I have two resources ClientResource and ProjectResource which results in two Laravel relationships:
Client model:
public function projects(): HasMany
{
return $this->hasMany(Project::class);
}
Project model:
public function client(): BelongsTo
{
return $this->belongsTo(Client::class);
}
I have implemented a BelongsToSelect field inside project resource to assign a client:
Components\BelongsToSelect::make('client')
->relationship('client', 'first_name')
->required(),
Everything works fine so far, but (obviously) all I can see on the project's view page is the disabled select field showing the customer's first name. I'd like to have all related fields listed. Have I missed something crucial in the documentation or what's the best way to approach this?
I've had a look into the RelationManager but it seems there is only a belongsToMany relationship (no belongsTo).
This can also be done with a select component by specifying a relation like this:
Select::make('client_id')
->relationship('client', 'first_name');
You also have access to the eloquent query builder instance to play with like so:
Select::make('client_id')
->relationship('client', 'first_name',
fn (Builder $query) => $query->where('status', 'actif'))
);
Document reference: https://filamentphp.com/docs/2.x/forms/fields#populating-automatically-from-a-relationship
php: 7.3.4
Laravel Framework 5.7.28
Hi, everybody. Help add data for all relations.
Now for some reason it turns out that only the last relation is added.
setRelations works similarly.
code
public static function firstOrCreateModel() {
$cart = Cart::firstOrCreate(self::getWhereQuery());
if ($cart->delivery === 1) {
$cart->setRelation('np_area', $cart->with('npArea')->first());
$cart->setRelation('np_city', $cart->with('npCity')->first());
$cart->setRelation('np_warehouse', $cart->with('npWarehouse')->first());
dump($cart->toArray());
}
return $cart;
}
Result. GIF screen recording
setRelation() does not save to the DB setRelation its just for testing you better use sync() if its many-to-many or attach() if its one-to-many
I'm struggling with an Eloquent dummy issue. I have a model User which has a many to many relation with a model Like.
I'm getting my user model by its id with:
$likedUser = User::findorfail($user_id);
At some point in the code, I'm doing:
if ($likedUser->likes->where(...)->count()) {
...
}
Then, I want to return my $likedUser WITHOUT the likes (added in the IF condition above by Laravel). How can I do that? If I do that, it does not work:
return response()->json([
'is_matched' => $isMatched,
'liked_user' => $likedUser,
], 200);
I would love to have a $likedUser->without('likes') to remove the relation added by Laravel. I tried to $outputUser = $likedUser->replicate() but the unique field of my models disappears..
1) What's the elegant way of only returning my User model without its relationship?
2) Why is Laravel adding the relation even if I never set my variable but only use it in a IF?
Thanks a lot for your time guys!
I would like to create a Laravel Authorisation Policy, however rather than checking the user->id I would like to check the related users Business model (like $user->business()->id)
I've tried using the following in my OrderPolicy but it does not work.
OrderPolicy
class OrderPolicy
{
....
public function edit(User $user, Order $order)
{
if ($user->business()->id === $order->business_id) {
return true;
}
}
}
Blade
...
#can('edit', $business->orders())
Edit Link
#endcan
...
Could someone show me how I could do this correctly?
Assuming business() is a relationship method.
$user->business->id would be the id of the Business model that is related to the user.
May want to check that ->business isn't null first.
You can also query directly on the relationship if you don't want to load that relationship. $user->business()->where('id', $order->business_id)->exists()
Laravel 5.4 Docs - Eloquent - Relationships - Relationship Methods vs Dynamic Properties
Here is model structure of my Laravel 5.3 project,
User.php (Model)
it has one invitation method that returns the invitation of a user.
public function invitations()
{
return $this->hasMany( 'App\Invitation', 'invitee_id', 'id' );
}
Invitation.php (Model)
This model has another method that would return the inviter detail of an invitation.
public function inviter()
{
return $this->hasOne( 'App\User', 'id', 'invited_by' );
}
If i want to retrieve all invitations of current user it works,
\Auth::user()->invitations;
But if i try to get the information about the inviter it won't work! (Question: How to do it?)
\Auth::user()->invitations->inviter;
Though i can query the inviter from a invitation eloquent object like this,
\App\Invitation::first()->inviter;
But this is not working when i try to access it from the user model -> invitation -> inviter!
Also can i use eager loading here?
\Auth::user()->invitations->inviter;
Looking at this, it appears that you're attempting to retrieve the inviter property from a collection of invitations. The reason Ken's suggestion to use \App\Invitation::first()->inviter; worked is because you are retrieving the inviter of only one invitation (in this instance, the first). To resolve this, loop through your invites before attempting to retrieve the properties for each one:
$invitations = \Auth::user()->invitations;
foreach ($invitations as $invitation) {
$inviter = $invitation->inviter;
}
There is also an each() method specific to Laravel Collections that will allow you to loop through your object.