Laravel Nova, custom options belongsTo - php

How I can add custom options in a belongsTo, I found another solution, but I think isn't correct/beauty
public function fields(Request $request)
{
$rooms = array();
$resourceId = ($request->viaResourceId) ? $request->viaResourceId : $request->resourceId;
$event = \App\Event::where('id', $resourceId)->first();
$rooms = array();
foreach ($event->hotels()->get() as $hotel) {
foreach ($hotel->roomTypes()->get() as $room) {
$rooms[$room->id] = $hotel->name.' - '.$room->title;
}
}
return [
(new Tabs('Configuración', [
'Tarifa' => [
BelongsTo::make('Evento', 'event', Event::class)->required(),
Select::make('Habitación', 'room_type_id')->options($rooms)->displayUsingLabels(),
BelongsTo::make('Régimen', 'board', Board::class)->required(),
Money::make('Importe', 'EUR', 'amount')->required(),
],
'Fechas' => [
Date::make('Fecha desde', 'checkin')->required()->creationRules(REQUIRED, 'after_or_equal:today'),
Date::make('Fecha desde', 'checkout')->required()->rules(REQUIRED, 'after_or_equal:checkin'),
],
'Cupo' => [
Number::make('Ocupación', 'quota')->required()->hideFromIndex(),
Number::make('Reservados', 'booked')->readonly()->hideFromIndex(),
],
'Info Adicional' => $this->detailDataPanel(),
]))->withToolbar()
];
}
I want
Select::make('Habitación', 'room_type_id')->options($rooms)->displayUsingLabels(),
convert to
BelongsTo::make('Habitación', 'room_type_id')->options($rooms)->displayUsingLabels(),
I tried with relatables, but Room Types depend of the Hotels related with Event.

Related

How to add object in laravel relationships

How to add extra object in laravel relationship when fatch data.
Here is my code:
$list = new self;
$list = $list->where('uuid', $uuid);
$list = $list->where('user_id', $user->id)->orWhere('to_user_id', $user->id);
$list = $list->with(['touser' => function($q1) {
$q1 = $q1->select('id', 'name', 'email', 'user_image', 'is_active', 'is_profile_completed', 'is_verified');
}]);
$list->with(['user' => function($q1) {
$q1 = $q1->select('id', 'name', 'email', 'user_image', 'is_active', 'is_profile_completed', 'is_verified');
}]);
$list = $list->first();
I want to add extra object in response like:
"Contest": {
"name": NULL,
"type": 0
}
You have multiple ways to add extra objects in response
The first option is
You have to define the eloquent relationship in the list model like this
public function contest()
{
return $this->belongsTo(Contest::class);
}
and then you can eager load that relationship using a method like this
$list->with('contest')->first();
another option is
You can set custom relationships like this
$list->setRelation('contest', collect(['name'=>null,'type':0]));
try this:
$list->prepend([
'Contest' => [
'name' => null,
'type' => 0
]
]);

laravel create wont store first row of data array

I have select options in my form where it has first row when page loads and users can add more rows by AJAX and store all rows data at once.
The problem is that my first row (which is visible when page loads) does not save the data while all other added rows will be saved.
Here is a screenshot of my select options when page loads:
And here it is when user adds new rows:
Here is my sample data that those rows have sent to controller (screenshot #2):
array:2 [▼
0 => array:3 [▼
"name" => "ali"
"username" => "alireza"
"action" => "delete"
]
1 => array:3 [▼
"name" => "eraty"
"username" => "aery"
"action" => "optional"
]
]
As I explained in my screenshots, object 0 data will not be store in the database, not sure why.
Here is my controller:
public function update(Request $request, $id)
{
$this->validate($request, [
'note' => 'required|string',
]);
$post = Post::where('id', $id)
->where('user_id', Auth::id())
->first();
$post->user_id = Auth::id();
$post->note = $request->input('note');
if ($post->save()) {
// social media (add new)
$socialHeir = $request->input('social_media_heir');
$socialAction = $request->input('social_media_action');
$socialNames = $request->input('social_media_name');
$socialUsernames = $request->input('social_media_username');
if (!empty($socialNames)) {
$result_array = [];
foreach ($socialNames as $key => $val) {
$result_array[$key] = [
'name' => $socialNames[$key],
'username' => $socialUsernames[$key],
'action' => $socialAction[$key]
];
}
// dd($result_array); <-- result of this line I shared above (after screenshot 2)
foreach ($result_array as $key => $merge) {
if (!empty($key) && !empty($merge)) {
SocialMedia::create([
'owner_id' => Auth::id(),
'heir_id' => $socialHeir,
'post_id' => $post->id,
'name' => $merge['name'],
'username' => $merge['username'],
'what_to_do' => $merge['action'],
]);
}
}
}
}
return redirect()->route('posts.index');
}
Update
SocialMedia model:
protected $fillable = [
'owner_id',
'heir_id',
'post_id',
'name',
'username',
'what_to_do',
];
public function post()
{
return $this->belongsTo(Post::class);
}
Post model
protected $fillable = [
'user_id',
'note',
];
public function socialMedias()
{
return $this->hasMany(SocialMedia::class);
}
Solved
The issue was in $key in my foreach after removing it, it saves all rows now.
foreach($result_array as $merge) {
if(!empty($merge)) {
SocialMedia::create([
'owner_id' => Auth::id(),
'heir_id' => $socialHeir,
'post_id' => $will->id,
'name' => $merge['name'],
'username' => $merge['username'],
'what_to_do' => $merge['action'],
]);
}
}

Laravel 5.8 | FullCalendar display events on calendar

I am trying to display events on calendar from this package:
composer require maddhatter/laravel-fullcalendar
I did model:
class Event extends Model
{
protected $fillable = [
'title',
'start_date',
'end_date',
];
}
Event migrations with same columns. In controller I have function:
public function all()
{
$events = [];
$data = Event::all();
if($data->count()) {
foreach ($data as $key => $value) {
$events[] = Calendar::event(
$value->title,
true,
new \DateTime($value->start_date),
new \DateTime($value->end_date.' +1 day'),
null,
[
'color' => '#f05050',
]
);
}
}
$calendar = Calendar::addEvents($events);
return view('pages.block.event.all', compact('calendar'));
}
and in my all.blade.php:
{!! $calendar->calendar() !!}
the calendar is displayed correctly, but no events are applied to it. Could someone help me? Thank you

Create new record using 2amigos SelectizeDropDownList in Yii2

I am trying to implement the 2amigos SelectizeDropDownList widget in a form to add new values to a table directly within the dropdown.
I am using the model Book and the Model Author so basically want to be able to add a new author in the book form.
This is the book controller at the update function:
public function actionUpdate($id) {
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['index']);
} else {
return $this->render('update', [
'model' => $model,
'categories' => BookCategory::find()->active()->all(),
'publishers' => Publisher::find()->all(),
'copirights' => Copiright::find()->all(),
'authors' => Author::find()->all(),
]);
}
}
This is the form:
<?=
$form->field($model, 'author_id')->widget(SelectizeDropDownList::className(), [
// calls an action that returns a JSON object with matched
// tags
'loadUrl' => ['author/list'],
'value' => $authors,
'items' => \yii\helpers\ArrayHelper::map(\common\models\author::find()->orderBy('name')->asArray()->all(), 'id', 'name'),
'options' => [
'class' => 'form-control',
'id' => 'id'
],
'clientOptions' => [
'valueField' => 'id',
'labelField' => 'name',
'searchField' => ['name'],
'autosearch' => ['on'],
'create' => true,
'maxItems' => 1,
],
])
?>
And this is the function author controller:
public function actionList($query) {
$models = Author::findAllByName($query);
$items = [];
foreach ($models as $model) {
$items[] = ['id' => $model->id, 'name' => $model->name];
}
Yii::$app->response->format = \Yii::$app->response->format = 'json';
return $items;
}
The form works fine to load, filter, search and add new items.
But it is not inserting the new typed attribute in the author table.
Do I need to add something in the book controller?
How can I check if it is a new value or a change of an existing author?
Thanks a lot
I made it work with the following code, not sure the most elegant because i am checking the if the author_id is a number or a string.
In my case the author won't be a number anyway.
public function actionUpdate($id) {
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post())) {
$x = Yii::$app->request->post('Book');
$new_author = $x['author_id'];
if (!is_numeric($new_author)) {
$author = new Author();
$author->name = $new_author;
$author->save();
$model->author_id = $author->id;
}
if ($model->save()) {
return $this->redirect(['index']);
}
} else {
return $this->render('update', [
'model' => $model,
'categories' => BookCategory::find()->active()->all(),
'publishers' => Publisher::find()->all(),
'copirights' => Copiright::find()->all(),
'authors' => Author::find()->all(),
]);
}
}

Zend Framework 2 filter / validate array of contents

How do I apply a filter to a field element with the contents of an array?
For example:
$this->add(
"name" => "tags",
"type" => "text",
"filter" => array(
array("name" => "StripTags"),
array("name" => "StringTrim")
)
);
$tags[0] = "PHP";
$tags[1] = "CSS";
If I attempt to filter I receive an error saying a scalar object is excepted, array given.
This isn't really possible at this time. Your best bet is to use a Callback filter and filter each Item individually. Something like this
$this->add(
"name" => "tags",
"type" => "text",
"filter" => array(
array("name" => "Callback", "options" => array(
"callback" => function($tags) {
$strip = new \Zend\Filter\StripTags();
$trim = new \Zend\Filter\StringTrim();
foreach($tags as $key => $tag) {
$tag = $strip->filter($tag);
$tag = $trim->filter($tag);
$tags[$key] = $tag;
}
return $tags;
}))
)
);
I realize this is old but you can specify the input type as ArrayInput and InputFilter will handle it as expected:
"name" => "tags",
"type" => "Zend\\InputFilter\\ArrayInput", // Treat this field as an array of inputs
"filter" => array(
array("name" => "StripTags"),
array("name" => "StringTrim")
)
I've made a CollectionValidator that applies an existing validator to all items in an array.
I'm using it with Apigility as such:
'input_filter_specs' => [
'Api\\Contact\\Validator' => [
[
'name' => 'addresses',
'required' => false,
'filters' => [],
'validators' => [
[
'name' => 'Application\\Validator\\CollectionValidator',
'options' => ['validator' => 'Api\\Address\\Validator']
]
],
'description'=> 'List of addresses for contact'
],
[
'name' => 'birthdate',
# ...
]
],
]
I'm not sure if this is how you would use a validator inside a controller, but probably something like this:
new Collection(array('validator' => 'Zend\Validator\CreditCard'))
It returns validation_messages per index. Let's say it was REST POST request to create a contact, it indicates that the second address contains an error in the zipcode field.
{
"detail": "Failed Validation",
"status": 422,
"title": "Unprocessable Entity",
"type": "http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html",
"validation_messages": {
"addresses": {
"1": {
"zipcode": {
"notAlnum": "The input contains characters which are non alphabetic and no digits"
}
}
},
"birthdate": {
"dateInvalidDate": "The input does not appear to be a valid date"
}
}
}
The Collection validator:
<?php
namespace Application\Validator;
class Collection extends \Zend\Validator\AbstractValidator implements \Zend\ServiceManager\ServiceLocatorAwareInterface {
protected $serviceLocator;
protected $em;
protected $messages;
protected $options = array(
'validator' => null
);
public function setServiceLocator(\Zend\ServiceManager\ServiceLocatorInterface $serviceLocator) {
$this->serviceLocator = $serviceLocator->getServiceLocator();
}
public function getServiceLocator() {
return $this->serviceLocator;
}
public function isValid($array) {
$inputFilterManager = $this->getServiceLocator()->get('inputfiltermanager');
$validatorName = $this->getOption('validator');
$this->messages = [];
$isvalid = true;
foreach($array as $index => $item) {
$inputFilter = $inputFilterManager->get($validatorName);
$inputFilter->setData($item);
$isvalid = $isvalid && $inputFilter->isValid($item);
foreach($inputFilter->getMessages() as $field => $errors) {
foreach($errors as $key => $string) {
$this->messages[$index][$field][$key] = $string;
}
}
}
return $isvalid;
}
public function getMessages() {
return $this->messages;
}
}
Current limitations:
No support for translation
Only the errors for the first erroneous array item are returned.
I had a very simular issue and I was able to solve it with Zend\Form\Element\Collection.
With the Collection Element I was able to validate inputs that looks like
$post = [
[
'idUser' => 1,
'address' => 'foo street',
],
[
'idUser' => 2,
'address' => 'bar street',
],
];
For a more detailed explanation check out the Zend Documentation and this working example

Categories