old helper with updateOrcreate - php

i'm trying to use old helper with updateorcreate. I have a textarea for an about field. If the user has published something, the old text should be displayed when he wants to update. When I try to use the old helper {{old('about', $profile->about)}}
I get this error: Attempt to read property "about" on null. When I remove the old helper, write something then add the old helper, then it works. I understand that it is trying to retrieve something that isn't in the database. I tried making the about field nullable but it still doesn't work

Seems that $profile is null in your case. Maybe a create form instead of edit?
Try to :
{{old('about', $profile->about ?? '')}}
or
{{old('about', optional($profile->about))}}
So that it uses empty string when old does not exist and profile either.

Related

Laravel 5.8 request value is null instead of default value

I use Laravel 5.8, and I want simply use a default value for description if it's empty, and take summary.
// summary variable request is equal to "test"
$summary = $request->get('summary', null);
$request->get('description', $summary)
But, the field is present, empty, and description give me null instead of summary value. Summary value is "test".
To get information from a request you should use get(), input() or the name directly. There is no documentation for the get method on requests for recent Laravel versions. For the input method on Laravel 5.8 the documentation says
You may pass a default value as the second argument to the input method. This value will be returned if the requested input value is not present on the request
It says it only works if it is not present so I would do it as simple as this
$description = $request->description ? $request->description : $request->summary
It really depends on what you want to achieve after all this and how you want your data.
Possible Solutions
My first impressions were that the data may not be being sent through correctly but upon looking over your code again, I realized you are using the more deprecated function ->get('description').
Try using ->input('description) instead. I personally have never used ->get(), so maybe this could be the problem.
https://laravel.com/docs/5.8/requests

How to access custom field property in Drupal User Object in Drupal 8

I am trying to access the content of custom fields in the user object.
Some have advocated the user of user_load. But according to Drupal, that deprecated.
in Drupal 8.x, will be removed before Drupal 9.0. Use
\Drupal\user\Entity\User::load().
So I've attempted to use Entity::load. It loads the object and in my IDE, I can see the values... but I can't seem to load a local variable with the value. Here is code that isn't working.
public function arguments($arg1) {
$userProfile = \Drupal\user\Entity\User::load($arg1);
$display_username = $userProfile->getAccountName();
$current_user = user_load($user_id);
$lastname = $userProfile->values["field_user_last_name"]["x-default"][0]["value"];
That string came from the path I saw in the object returned. However, as one can see in the screen shot from the IDE, the value comes back NULL.
I'm sure it is something simple that I'm not seeing. (missing the good old days of non-OOP!)
The $userProfile is an object so data should be called via methods.
The list of all methods can be found in Drupal documentation.
Long story short, all Drupal entities use the same methods to return field value.
The get('field_name') method returns the field object and getValue() returns the value of field.
In your case that would be:
$field_value = $userProfile->get('field_user_last_name')->getValue()[0]["value"];
By using so called magic methods the code will look even cleaner and you can simply get the value as:
$field_value = $userProfile->field_user_last_name->value;
Please note that magic methods can not be always used while standard methods can be.

passing variable through controller method attatched to view::composer in Laravel

I am trying to make commenting on my site modular, so I have attempted to use view::composer to run the queries that are required to populate the comment data whenever the comment view is called, then my plan was to include the comment view in any place i needed comments, and then connect it to the appropriate comment table in my database.
The functions I have made for commenting fit each of the comment tables I've created and I've made the view composer work, the only thing is now since the function to get all the comment data is being called by the view composer when the comment view is being set up, that function no longer has access to the id that was being passed to it ( normally it is in the route like: Route::get('/{id}', 'controller#method');).
I have been searching for hours trying to figure out how to pass the variable in, but with no luck.. Maybe I'm missing some really basic detail, Idk, but I can't figure it out and haven't found anything that solves the problem, please help..
It seems like it should be so simple, I've tried doing:
$var = Route::get('/{id}', function($id){ return $id; }
Which i thought might do it, but the result was an error saying whatever the response was couldn't be converted to string.
Option 1: Access the route parameter
View::composer('comment-view', function($view){
$id = Route::current()->getParameter('id');
});
Option 2: Use the view to access the id
In your controller: return View::make('view-name')->with('commentId', $id);
View::composer('comment-view', function($view){
$id = $view->commentId;
});

How save data with special symbols in cakephp 2.4.x?

I want save some special symbols to save in database using cakephp's query() function.
Sanitize functions of cakephp is not working.
$data = Sanitize::escape($this->request->data['Post']['body']);
I want save posted data with any symbols typed in database and show it in my view page.
How make it possible?
//Editted//
For example something like this
'Hello ..',I am new to stackoverflow', etc
When I add like this it will not be saving to database. I used save() method also. But no effect..Is it possible to use mysql_real_escape function in cakephp

How do you restore the values of the form in case if validation failed (in Kohana 3)

There is a sample in the bottom of the official documentation http://kohanaframework.org/3.2/guide/kohana/security/validation
But obviously it wont work at the request as long as $post['username'] in View is used but the $post array is empty on first request.
So how do you restore the values in this case? Any general solution?
PS: yes, I do understand I could do isset($post['username']) ? $post['username'] : ''; but it is just annoying
I use the model to display the data in the form. That way the initial form value is the initial value in the model.
I then update the model data with POST data in the controller, if there are validation errors, the model data will contain the POST data. This means I don't have to put any conditional logic in the view, and I just do: Form::input('name', $model->name)
Here's a more detailed explanation of this approach: Kohana ORM and Validation, having problems
I use Arr::get function:
echo Form::input('name', Arr::get($post, 'name'))
I was just looking at the old documentation on Building and Validating a Form.
You can see from the sample code that first you need to initialize an array with the form field names as the key and set the value to an empty string. And if there's an error, fill in the values of each element. In the views, you can simply call Form::input() normally without any if statement or some sort.
I guess Kohana has already been built this way from the start. And it doesn't seem to change. You'll probably just need to do the same thing.

Categories