Laravel insert array of arrays with foreign keys - arrays

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);

Related

laravel- get value from json decode array

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.

How to view data decode json in blade

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'] }}

How do I update multiple records from one post that returns a collection?

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

grocery crud multiselect field

No problem to use this function 'multiselect field' which show on this website:
http://www.grocerycrud.com/documentation/options_functions/field_type
$crud->field_type('fruits','multiselect',
array( "1" => "banana", "2" => "orange", "3" => "apple"));
Next step, i try to extract data from database to replace the 'array' in the formulae above but failed, pls advise.
$this->db->select('employeeNumber');
$a = $this->db->get('employees')->result();
$crud->field_type('firstName', 'multiselect', $a);
I'm getting result like
Array ( [0] => stdClass Object ( [employeeNumber] => 1002 )
[1] => stdClass Object ( [employeeNumber] => 1056 )
Hmm... how to make it into this format, any advise?:
array( "1" => "banana", "2" => "orange", "3" => "apple")
You need to actually do a foreach here. In our case you need to do something like this:
$this->db->select('employeeNumber');
$results = $this->db->get('employees')->result();
$employees_multiselect = array();
foreach ($results as $result) {
$employees_multiselect[$result->employeeNumber] = $result->employeeNumber;
}
$crud->field_type('firstName', 'multiselect', $employees_multiselect);
or it is even better if you have the name of the employer to do something like this:
$this->db->select('employeeNumber, employeeName');
$results = $this->db->get('employees')->result();
$employees_multiselect = array();
foreach ($results as $result) {
$employees_multiselect[$result->employeeNumber] = $result->employeeName;
}
$crud->field_type('firstName', 'multiselect', $employees_multiselect);

Zend Form Element Arrays

I'm building a simple message system using Zend Form where users will be able to select multiple recipients. Each recipient may be a different user type and so I need to have both the user type and the ID to send to the servicelayer. The form will start off with one recipient selected (as an array element in the form), and I plan to use jQuery to append each subsequent recipient to said array. I wish to end up with this:
array(4) {
["Subject"] => string(4) "My message subject"
["Body"] => string(6) "My body of text"
["Recipients"] => array(1) {
[0]
["profile"] => string(2) "476"
}
[1]
["otherusertype"] => string(1) "54"
}
}
}
This would enable me to loop through each recipient nice and easily, gaining the user type and the respective ID.
Now, I am currently doing this in Zend Form:
$form->addElement(
'hidden',
$type,
array(
'value' => $id,
'belongsTo' => 'Recipients'
)
);
But this leaves me with
array(4) {
["Subject"] => string(4) "hfgh"
["Body"] => string(6) "fghfgh"
["Recipients"] => array(1) {
["profile"] => string(1) "1"
}
}
As you can see, if I add another recipient of usertype "profile" to the array, it will just be overwritten.
How am I able to get an extra dimension within this array?
Thanks in advance!
You should set your belongsTo to the array:
$this->addElement('hidden', '1', array(
'value' => 1,
'belongsTo' => 'Recipients[profile]'
));
$this->addElement('hidden', '2', array(
'value' => 2,
'belongsTo' => 'Recipients[profile]'
));

Resources