Image and name as title - laravel

I am building a Laravel Nova application and want to show a user with image and name.
/**
* The single value that should be used to represent the resource when being displayed.
*
* #var string
*/
public static $title = 'name';
As you can see above, the $title is used to represent the resource, but I want two values, a image (Avatar) and name (Sting).
Is this possible, or is it possible to show an other value like an image, instead of a string?

Use Avatar field
If a resource contains an Avatar field, that field will be displayed next to the resource's title when the resource is displayed in search results
https://nova.laravel.com/docs/1.0/resources/fields.html#field-types

Related

Laravel : Polymorphic Relations + Accessor

I have a Gallery table that uses Polymorphic Relations so I can add Images and Videos to my gallery list.
Within the Gallery table I have a galleryable_type column that is populated with either App\Video or App\Image.
Is there a way for me to use an accessor (docs here) to change the value of galleryable_type to either video or image so I can use that column in JS to decide what gallery item type I'm dealing with?
I tried the following:
/**
* Get and convert the makeable type.
*
* #param string $value
* #return string
*/
public function getMakeableTypeAttribute($value)
{
return str_replace('app\\', '', strtolower($value));
}
But i end up with the following error:
FatalErrorException in Model.php line 838:
Class '' not found
I'm assuming that has to do with the accessor is being processed before the the polymorphic relationship but I'm not sure.
I can simply use the following in my controller:
foreach (Gallery::with('galleryable')->get() as &$gallery) {
$gallery->galleryable_type = str_replace('app\\', '', strtolower($gallery->galleryable_type ));
}
But that seems like a dodgy way of doing things. Could a Laravel guru shed some light on the best way to tackle this problem?
Thanks!
Well I've found an interesting way to solve this issue.
In your models (App\Video and App\Image) you have to add:
protected $morphClass = 'video'; // 'image' for image class
then in your register method in service provider class add:
$aliasLoader = \Illuminate\Foundation\AliasLoader::getInstance();
$aliasLoader->alias('video', \App\Video::class);
$aliasLoader->alias('image', \App\Image::class);
This will cause that you will write image, and video in galleryable_type in the database instead of class names.
So now you can easily get to this values with:
echo $model->galleryable_type;

Model fields not recognized from the Intellisense

So, in the model I've listed the fillable and hidden fields, and then when I access fields on the object of that model, they get highlighted as Field 'some_field' not found in class .... If I add phpDoc to it as follows:
/** #var Trip $trip */
$trip->driver = ...
the field is being highlighted. If I write
/** #var object $trip */
$trip->driver = ...
it's not highlighted, but that's just not right. Everything works fine, but it just looks bad in the IDE, and the highlighting is annoying.
Then I decided to simply declare variables in the model class, for every field, so that they're recognized, but then the fields always hold NULL, when I access them on the object.
Anyone has a solution for that?
If you use PhpStorm you can write it above your model class as a comment.
/**
* App\User
*
* #property string $username
*/
You can also use a composer package called laravel-ide-helper here you have the link
https://packagist.org/packages/barryvdh/laravel-ide-helper
You can use some simple commands to generate helper files and it will provide you with code completion.

PasswordField validation in SilverStripe

I have a member in my project called Customer. I need to validate PasswordField.
I created a function in my page containing the PasswordField as below
function getValidator() {
$data = $this->loadData();
if (#$data['Password']->minLength(7)) {
return $this->validationError('Password', 'Password field should contain minimum 7 charactors', 'bad');
}
}
This does not give any result. How do I get this working?
Remove that ugly # from your code and watch that beautiful error message. Error supression is a real bad practice, you should avoid that
There is already a class PasswordValidator which is used by the Member object and can check the minimum length of a password. (See API Docs for PasswordValdiator)
If you show us more code I can try to help you implementing it right.
Is your Customer object a subclass of Member? Where is the password changed? What FormField is used for Password? There is also a ConfirmedPasswordField showing two masked fields for matching passwords where you can set a minlength.
So, assuming you save your password in a field called Password you could set up the field e.g.
$passwordField = ConfirmedPasswordField('Password', 'Choose a password');
$passwordField->minLength = 7; //there is no setter method for that right now
Then add $passwordField to your FieldList e.g using
$fields->push($passwordField);

How to get position value from a model (catalog/product_image) in Magento?

I only have this Magento model (catalog/product_image). Is there any way for me to get position value or label value of that image. When I var dump this model, I can only get width, height, quality, _keepAspectRatio, etc. The most valuable data I can get from it is image file name like this: /s/e/productABC.png
Im not quite sure if you will get anything more useful from the catalog/product_image model. From your question it sounds like you are wanting to fetch the image label. If you have access to a product object you can use:
$_product->getMediaGalleryImages()->getItemByColumnValue('label', 'LABEL_NAME')->getUrl();
This will return you the label of the image.
If you need any more help please share what objects you have access to and what you are trying to achieve.
in that class there is just one property and one set and one get function for file
Property : protected $_baseFile;
Function
/**
* Set filenames for base file and new file
*
* #param string $file
* #return Mage_Catalog_Model_Product_Image
*/
public function setBaseFile($file)
and
public function getBaseFile()
{
return $this->_baseFile;
}
This implies: You will not get position using this object.
Just check getBaseFile that will return a string.
I always suggest that for any given object if extended from varien_object use $object->debug(); as this provides lot of information on what all it stores.

How to access Magento Model data from a different scope

I am facing a requirement to access values for an attribute that are specified in another scope on a multi-website/multi-store site. In particular, we need to display the Admin (default) label for an attribute in the frontend when the label for the Store has been set.
So the code should render the Hex value from the Admin column in one part of the page, and the textual Description from the English (US) on another part of the page. How do I do that?
Conversely, I have seen instances where values have been set on a Store View but are null for Default, and the code returns null even when the Store has been set. Can someone please explain how that works?
Here is how to do it using Magento classes:
// Get the model of the attribute in question
/* #var $attribute Mage_Catalog_Model_Resource_Eav_Attribute */
$attribute = Mage::getSingleton('eav/config')->getAttribute('catalog_product', 'color');
// Load the option collection for that attribute adding the storeFilter()
/* #var $collection Mage_Eav_Model_Resource_Entity_Attribute_Option_Collection */
$collection = Mage::getResourceModel('eav/entity_attribute_option_collection')
->setPositionOrder('asc')
->setAttributeFilter($attribute->getId())
->setStoreFilter();
// Load the product so we can get the correct option from the collection
$product = Mage::getModel('catalog/product')->load(39);
$productOption = $collection->getItemById($product->getColor());
printf("Default: %s, Store: %s\n", $productOption->getDefaultValue(), $productOption->getValue());
Adjust as needed.

Resources