I have data like this
result from
dd($data)
in blade
array:18 [▼
"id" => "5dbb3b9adbc24572692f50e1"
"external_id" => "T Shirt Cyber Jawara"
"user_id" => "5785e6334d7b410667d355c4"
"status" => "PENDING"
"merchant_name" => "Xendit Testing"
"merchant_profile_picture_url" => "https://www.xendit.co/images/logo.png"
"amount" => 300000
"payer_email" => "hendro#gmail.com"
"description" => "BayarT Shirt Cyber Jawara"
"expiry_date" => "2019-11-01T19:52:58.423Z"
]
data is from controller
return view('confirmorder',['data' => $responseObject]);
How to show in blade laravel?
I don't think I fully understood what you need, but to pass data to your blade view is simple. Just do something like so:
public function returnView()
{
$responseObject = [
"id" => "5dbb3b9adbc24572692f50e1",
"external_id" => "T Shirt Cyber Jawara",
"user_id" => "5785e6334d7b410667d355c4",
"status" => "PENDING",
"merchant_name" => "Xendit Testing",
"merchant_profile_picture_url" => "https://www.xendit.co/images/logo.png",
"amount" => 300000,
"payer_email" => "hendro#gmail.com",
"description" => "BayarT Shirt Cyber Jawara",
"expiry_date" => "2019-11-01T19:52:58.423Z"
];
return view('confirmorder', compact('responseObject'));
}
Then, on your blade view, you will be able to call the array however you want, like so:
{{ $responseObject['status'] }}
Let me know if this is what you want.
Just use {{ $data }} in the view
for a specific key,
{{ $data['user_id'] }}
Related
I have an array like this, after I use json_decode ($json = json_decode($items, true);), the result looks like this
array:1 [
61 => array:53 [
"id" => 2790
"name" => "ABC"
"created_at" => "2020-12-04 09:43:57.317"
"updated_at" => "2021-02-16 16:47:16.167"
"deleted_at" => null
"remark" => null
]
]
And Iwant to get the value from field name, how to do it in Laravel??
I try like this but, get error Undefined offset name
dd($array['name']);
The correct way to access the element you're trying to get to is $array[61]["name"];
$array[61] will give you:
array:53 [
"id" => 2790
"name" => "ABC"
"created_at" => "2020-12-04 09:43:57.317"
"updated_at" => "2021-02-16 16:47:16.167"
"deleted_at" => null
"remark" => null
]
Then you can use ["name"] to access the value you're after.
Arr::add doesn't work with dot notation. Arr::set will create a new element only if it does not exist, otherwise, it will overwrite the existing one. There is data_set, it works similar to Arr::set, but it accepts the overwrite flag which does not do what I need (if it's set to false instead of adding a new item, it will just skip setting new value).
My code:
$array = [['name' => 'Test', 'link' => 'test_link'], ['name' => 'Test1', 'link' => 'test1_link']];
$result = [];
$position = 1;
foreach($array as $element) {
Arr::set($result, 'itemListElement.type', 'ListItem');
Arr::set($result, 'itemListElement.position', $position);
Arr::set($result, 'itemListElement.item.name', $element['name']);
Arr::set($result, 'itemListElement.item.link', $element['link']);
$position++;
}
And I would like to have multiple list elements within the itemListElement parent, instead of having one list element which is being overwritten all the time.
Here is what it should look like:
[
"itemListElement" => [
[
"type" => "ListItem",
"position" => 1,
"item" => [
"name" => "Test",
"url" => "test_url",
]
],
[
"type" => "ListItem",
"position" => 1,
"item" => [
"name" => "Test1",
"url" => "test1_url",
]
],
]
]
Can you try this:
$array = [['name' => 'Test', 'link' => 'test_link'], ['name' => 'Test1', 'link' => 'test1_link']];
$result = [];
foreach($array as $key=>$element) {
Arr::set($result["itemListElement"][$key], 'type', 'ListItem');
Arr::set($result["itemListElement"][$key], 'position', $key + 1);
Arr::set($result["itemListElement"][$key], 'item.name', $element['name']);
Arr::set($result["itemListElement"][$key], 'item.link', $element['link']);
}
$result;
I met some difficulties when I was trying to get value from an array in my php blade.
It has rather clear structure (printed with dd function)
{{dd($attr)}}
array:4 [▼
"id" => "215"
"type" => "select"
"name" => "Status"
"value" => array:2 [▼
"pred" => array:3 [▼
0 => "Employed"
1 => "On vacation"
2 => "Dismissed"
]
"sel_val" => "0"
]
]
And when I want to get a value by key 'sel_val' or 'pred'
print_r($attr['value']['pred']);
it gives me Illegal string offset 'pred'
And it works nice in Controller. What should i do?
It gives that error because pred is also an array. you'll have to do $attr['value']['pred'][0] to get Employed, $attr['value']['pred'][1] to get On vacation, $attr['value']['pred'][2] to get Dismissed and $attr['value']['sel_val'] to get the value of sel_val which is 0 in this case. Hope this helps.
working fine when we send array in compact function in the controller
$record = array('id' => '215', 'type' => 'select', 'value' => array('pred' => array('0'=> 'Employed', '1' => 'On vacation', '2' => 'Dismissed'),'sel_val' => '0'));
return view('home', compact('record'));
I have two tables, news and news_links. The news table looks like this (id, title, body, date_published) and the news_links look like this (id, news_id, url).
I'm trying to build a query where I can insert a news array:
array(
array(
"id" => "123456",
"title" => "First Title",
"body" => "First body text",
"date_published" => "01-01-16"
)
array(
"id" => "22222",
"title" => "Second Title",
"body" => "Second body text",
"date_published" => "01-01-16"
)
)
This array will contain multiple array, this is just an example.
Links related to these news will also be inserted.
array(
array(
"0" => "1",
"1" => "123456",
"2" => "img_url"
)
array(
"0" => "2",
"1" => "123456",
"2" => "img_url"
)
array(
"0" => "2",
"1" => "22222",
"2" => "img_url"
)
)
Will I have to build a foreach loop and run multiple queries or is there a way to run a single query and insert everything into both news and news_links?
No, you can't use one query to insert data in two different tables. However, you can combine several inserts for one table in one query.
It will look like
INSERT INTO news_links VALUES(1,123456,'url'),(2,123456,'url'),(2,22222,'url');
I think this is a simple solution NewsLink::insert(array);
I have an AngularJS form that uses ng-repeat and ng-form to populate a list of users that can be marked as here or not.
I am now trying to use AJAX to submit the updated field to Laravel.
My form request is trying to POST a collection and update one column for multiple records. Nothing seems to be happening, no errors.
I suspect there is something wrong with my method.
public function setRoster(Request $request, $id) {
$players[] = $request;
foreach ($players as $player) {
Player::where('playerId', $player->playerId)
->update(array('gamesPlayed' => $id));
}
}
Any ideas? Thanks!
Here is the JSON from the POST
Request {#39
#json: ParameterBag {#31
#parameters: array:1 [
"players" => array:22 [
0 => array:15 [
"playerId" => 1
"teamId" => "6"
"number" => "2"
"position" => "unknown"
"gamesPlayed" => ""
"tempNumbers" => ""
"firstName" => "Tom"
"lastName" => "Travis"
"dob" => "1968-10-01"
"email" => "tom#hotmail.com"
"mobile" => ""
"photo" => "headshot.jpg"
"created_at" => "-0001-11-30 00:00:00"
"updated_at" => "-0001-11-30 00:00:00"
"onIce" => true
]
and so on...
So based on feedback, I have tried this;
foreach ($request as $player) {
Player::where('playerId', $player['playerId'])->update('gamesPlayed', '=', $id);
}
Which is now returning this error;
Cannot use object of type Symfony\Component\HttpFoundation\ParameterBag as array