Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have following query: $scholen = Scholen::with('media', 'translations', 'seo')->online()
when i dump it:
How can i sort the schools on 'title' from the relations -> translations -> fields
Thanks in advance.
Olivier
You could access fields with $scholen[$index]->translations[$index]->fields, however you couldn't access the title property since the whole value of fields is a string - you would have to strip the string to get the bit you wanted.
You would have to get() the results for the query, and then sort the results by including this logic inside a sortBy() callback, e.g.
$scholen->sortBy(function ($value, $key) {
// Logic here to get the part of the `fields` string you want
// and then sort it
});
Hope this helps, this looks like a bit of a tricky one
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I am trying to push new array item into existing array variable that has items from database. What I want to do is add a new item named 'Others' at the end of this array and display it as select drop down in view which consists of all the items from database and at the end of this select the 'Others' item that I manually added in my controller.
Here is what I tried to do:
When you query from database in Laravel, the result is a collection object. You will first need to convert this collection object to array to be able to use array functions. The following should work in your case:
$admin = DB::table('tbl_account_admin')->where('id', $id)->get()->toArray();
You can use json_decode(json_encode($admin), true);
And then use array_push function
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I was trying to build a script that could get me definitions of words from the wikipedia API in php.
I tried several obtaining the definitions in an array and displaying the first definition but it didn't work. Can anyone please help me out. Any help is appreciated
Which parameters did you use in your request?
You can take this as an example:
https://en.wikipedia.org/w/api.php?action=opensearch&search=PHP&limit=1&format=json
You said there that you want only the first definition, so you can put limit=1. The response is in json.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have a field in one of my database tables, a sample of this field looks like so ; ["1","4","7"]. Those are keys to a reference in another table. What i want to do is fetch the field and ideally loop through the values and get the data from the referenced table. As you will notice, ["1","4","7"] is PHP declaration for an array but for the life of me, I am unable to parse it as an array. How do i parse it as an array? What am I missing?
You should be able to decode and convert it into an array with PHP's json_decode function.
Example:
<?php
echo '<pre>';
print_r(json_decode('["1","4","7"]'));
echo '</pre>';
?>
If I understood your problem is that your database column store a string PHP array representation.
So you should use the function json_decode
By the way it's a strange way to store relation in a database
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
$query=$this->db
->select(a.date,group_concat(s.name)
->from("tbl_attendance a,tbl_students s")
->WHERE ("FIND_IN_SET(s.student_id, a.attendance)")->group_by("a.date")
->get();
I wanted to know whether I have used the FIND_IN_SET and group_by functions correctly. Thanks in advance
FIND_IN_SET() returns the position of a string if it is present (as a
substring) within a list of strings
so you should search if a value is != 0
eg:
->where("FIND_IN_SET(s.student_id, a.attendance) !=", 0)
->group_by("a.date")
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm trying to use PHP to find a value in a string and replace it with another value, the value may be (for example) £x on one line and £x.xx on the next, but will always be replaced by a value of £x.xx. Hope that makes sense? TIA.
Use this regex to match the prices: £[0-9]+\.?[0-9]{0,2}
You can perform the replace with this by using this function:
preg_replace("/£[0-9]+\.?[0-9]{0,2}/", " ** REPLACEMENT ** ", $input_lines);
The nice thing about preg_replace is that if you feed it a single line you'll get a single value back, but if you feed it an array of strings with the values you want changed it will return an array with those values replaces. Nice and tidy if you need to make a lot of replacements.