Laravel FirstOrCreate function with null value converts to string - laravel

I send a null value for search field, and it converts to string.
using laravel 8. php 7.4
$notification = Notification::firstOrCreate([
'resource' => $notif['resource'],
'topic' => $notif['topic'],
'response' => null
], [some other values]);
returns
#attributes: array:12 [
"resource" => "/item/55359"
"topic" => "item"
"response" => "null" --> like string and not null value
"status_id" => "Pending"
"attempts" => 0
]

thanks
it was a model problem...
the response field saves a json string.. and it was converting the null value to json string.
that was the problem.
i have add this line before
if (is_null($value)) $this->attributes['response'] = $value;
thanks all

Related

Trying to get property 'name' of non-object error in laravel

here is my function.
public function get(Request $request){
$bike1 = $request->input('bike1');
$bike2 = $request->input('bike2');
//dd($bike1);
return $bike2->name;
}
When i use the dd function, the result is:
"{"id":1,"created_at":"2020-09-16T12:33:25.000000Z","updated_at":"2020-09-16T12:33:25.000000Z","name":"302r","brand":"benelli","price":800000,"displacement":300,"segment":"300cc","power":"28KW#10000 rpm","torque":"27Nm#9000rpm","fuel_delivery_system":"Efi","abs":"1","cooling_system":"Liquid","weight":155} ◀"
But when I try to access the name property(any) i get the error.
Trying to get property 'name' of non-object
What I might be missing?
It looks like $bike1 is a JSON string, before you can access it's attributes you need to decode it.
$bike1 = json_decode($request->input('bike1'));
return $bike1->name
If that doesn't work $bike1 may be an array, not an object, in that case, this should work
return $bike1['name']
you missed that you returned a string, not an object
first decode your json then after get you name
$bike2 = json_decode($request->input('bike2'));
return $bike2->name
You need to decode $bike2 because it's in JSON format, so you can do
$decoded_bike2 = json_decode($request->input('bike2'));
This will return an object
{#1236 ▼
+"id": 1
+"created_at": "2020-09-16T12:33:25.000000Z"
+"updated_at": "2020-09-16T12:33:25.000000Z"
+"name": "302r"
}
you can access name property like
return $decoded_bike2->name;
If you do
$decoded_bike2 = json_decode($request->input('bike2'), true);
This will return an array
array:4 [▼
"id" => 1
"created_at" => "2020-09-16T12:33:25.000000Z"
"updated_at" => "2020-09-16T12:33:25.000000Z"
"name" => "302r"
]
You can access key name like
return $decoded_bike2['name'];
For more details about json_decode() Reference

How to access array properties with Laravel Eloquent, if stored in a database?

For example, I have JSON stored in a Row of a database called 'data'
External.php (model) - casting 'data' to an array:
protected $casts = [
'data' => 'array',
];
I can access it using Tinker with this commmand:
$external = App\External::first()->pluck('data');
This returns
Illuminate\Support\Collection {#3384
all: [
[
"id" => 17566616456845,
"name" => "#1008",
"note" => "",
"tags" => "",
"test" => false,
"email" => "katie.jane#example.com",
"phone" => null,
...
..
.
How do I access the "email" or "id" from that collection? How do I modify the tinker eloquent command to get the "id" or "email"?
$external = App\External::all()->pluck('data')->'email'
T_STRING or T_VARIABLE or '{' or '$' on line 1> Exception with message 'Property [email] does not exist on this> PHP Parse error: Syntax error, unexpected T_CONSTANT_ENCAPSED_STRING,
expecting
Getting warmer:
>>> $external = App\External::pluck('data')->get('email')
=> null
pluck() method returns array. So if you want to access 'email' you must use.
$external = App\External::first()->pluck('data');
$email = $external['email']; // katie.jane#example.com
If you are just trying to get the data for one record use the cast attribute as an array:
// one External record
$external = App\External::first();
$email = $external->data['email'];
$id = $external->data['id'];
Try this
$external = collect(App\External::all()->pluck('data'))->pluck('email')->all();
You don't have to use pluck().
if you want to access email from the data of an External.
$email = App\External::first()->data['email'];
UPDATE
to get all the emails.
$emails = App\External::all()
->map(function($external) {
return $external->data['email'];
});

How retrieve element from collection by name?

First, I create collection from array:
$bank_center = collect(array("amount" => null, "mfo" => null, "name" => null));
Then I try to get value by key:
dd($bank_center->name);
Dump is:
Collection {#562 ▼
#items: array:3 [▼
"amount" => null
"mfo" => null
"name" => null
]
}
In your particular case, the following would just work:
$bank_center['name'];
I am not sure why you want to wrap it as an object, but if you still wish to do it, I'd recommend you take a look at Fluent.
$bank_center = new \Illuminate\Support\Fluent(array("amount" => 'test', "mfo" => 'test2', "name" => 'test3'));
dd($bank_center->name); // test3
You should use square brackets to access item from such collection:
$bank_center['name']
To retrieve element by name from collection you can use get method, it returns the item at a given key. If the key does not exist, null is returned:
$collection = collect(['name' => 'bruno', 'framework' => 'laravel']);
$value = $collection->get('name');
// bruno

Validator fails for not required integer field in laravel 5.3 when I use $request->only()

$request->all() in laravel returns only passed params.
Example:
array:1 [
"user" => array:2 [
"phone" => "+7(900)900-10"
"password" => "123"
]
]
But when I use $request->only(["field1", "field2", ...]) it returns
array:1 [
"user" => array:6 [
"phone" => "+7(900)900-10"
"password" => "123"
"first_name" => null
"last_name" => null
"middle_name" => null
"manager_id" => null
]
]
And validation fails for field manager_id which has these rules: "integer", "min:1"
This field is nullable in database and when not passed from browser it must be set to null.
How to fix this problem?
You don't need to do anything, Laravel will create a row and manager_id will be null.
To make validation pass, try to remove the required rule if you're using it or use the sometimes rule.
In some situations, you may wish to run validation checks against a field only if that field is present in the input array. To quickly accomplish this, add the sometimes rule to your rule list:
'email' => 'sometimes|required|email'

Laravel require at least one field

I'm building a form . there is 3 specific text filed that at least one of them should be filled by the user. how may i implement it with Laravel validation rules?
//dd($request)
array:6 [▼
"_token" => "o5td5RMv2EQ5mA7LqXpyMXCKIu7L78BfrRSEU1se"
"skill_id" => "6"
"plan_id" => "1"
//at least one of below fields should be filled
"context" => ""
"link" => ""
"desired_date" => ""
]
You can use:
required_without_all:foo,bar,...
In this the field under validation must be present only when all of the other specified fields are not present.
$rules = array(
'skill_id' => 'required_without_all:plan_id,context,link',
'plan_id' => 'required_without_all:skill_id,context,link',
);
OR
You can use the required_unless rule: https://laravel.com/docs/5.2/validation#rule-required-unless
required_unless:anotherfield,value,...
IN this case, the field under validation must be present unless the anotherfield field is equal to any value.
You can use required_without_all
$rules = array(
'context' => 'required_without_all:link,desired_date',
'link' => 'required_without_all:context,desired_date',
'desired_date' => 'required_without_all:context,link',
);

Resources