Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
i installed guzzle the laravel package, and i display the data from an api. I want to save this data in database. I can't find enough resources to do it
route
controller
response
public function all() {
$response = $client->request('GET', 'http://api'); // Pass the third parameter as an array if you have to set headers
$response = json_decode($response->getBody(), true); // The API response data
// You should put your data in the loop.
$data = collect($response); // So we create an collection to use helper methods
$data->map(function ($item) {
// Now, we can save the data to the database with two methods
// Model:
YourModel::create([
'column' => $item['key'],
]);
// Query builder:
DB::table('your_table')->insert([
'column' => $item['key'],
]);
});
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm using AXIOS with REACTjs
I want a functionality that if my first API(GET) call returns Not Found
status code then I want to send the other call efficiently.
Although I'm getting some data through that .catch method in form of Promise<pending> .Please Help!!!
Why don’t you add a conditional in the catch block to check the status?
For example:
axios.get('/first-call')
.then(resp => {
//handle successful response
})
.catch(error => {
if (error.response && error.response.status === 404) {
// make second API call
}
});
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
https://media2.giphy.com/media/JpYdtQifMv3SAsnf8j/giphy.gif?cid=f25c51ea3ef14717fe448ab031d8a047c77d1438043fcca6&rid=giphy.gif
I want to extract the image from the URL above as an object variable, or BLOB, or whatever would be best.
How do I do this ?
I tried Get with axios and it did not work.
The end goal is to upload the object to an S3 folder later.
You can just use fetch here. Some examples list how to load images via the blob body api
async function loadData(url) {
const resp = await fetch(url)
const data = await resp.blob()
const imgUrl = URL.createObjectURL(data);
return imgUrl
}
where you can set an image.src to be this url
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I am a newbie to AngularJS. Ho do we manage data from page to page?
1) I have a application where a user logs-in. The database returns the user details. I need to use the user details on other pages. Other pages are not a subset of the login page i.e they have their own scope. So should I store the user in rootScope like $rootScope.user=user. Dumping such data in the rootScope does not look like a good practice. Is there a better way?
2) The 2nd issue is related to the first. If I want to add a $watch on page2 which watches a scope variable on page1, how do I do it.
Use a Factory, And store the necessary login details,
app.factory('AuthenticationService', [ '$location','$http', function( $location,$http){
var currentUser;
return {
login: function(username, password){
var endPoint = "url";
var jsonData = {
username: username,
password: password
};
var jsonString = JSON.stringify(jsonData);
$http.post(endPoint, jsonString,{headers: {'Content-Type': 'application/json'}}).success(function (data,status, headers ){
if(data.access == "ok")
{
$location.path("learning");
}
else
{
$location.path("error");
}
});
},
logout: function(){},
isLoggedIn: function(){},
curUser: function(){return currentUser}
};
}
DEMO
I have a couple of ideas for your questions:
1) Use Cookies Instead of rooScope, You could also use LocalStorage(Use carefully, It does not work for all explorers)
2) You are looking for emitters and listeners here is a good explanation https://toddmotto.com/all-about-angulars-emit-broadcast-on-publish-subscribing/
good luck.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am started to work with Angular.js, I want to replace HTTP:// of any URL in a simple string like 'hey', I didn't get any relevant solution.
I'd be grateful for your response.
try this.
http://www.w3schools.com/jsref/jsref_replace.asp
var str = "Visit Microsoft!";
var res = str.replace("Microsoft", "W3Schools");
I got my answer here is the controller code
function ListCtrl($scope, $http,Project) {
$http.get('/project').success(function(data){
for(var i=0;i<data.length;i++){
data[i].site=data[i].site.replace('http://','Hey');
}
$scope.projects=data;
});
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I have an angular service that is responsible for representing a collection of employees (that happens to be stored on the database normally). I hear that it is bad code smell to do a $http.get request inside the service's class's constructor. What I have been told is that I am supposed to inject the dependency that allows you to collect the relevant data but isn't that what I'm doing by using $http, using dependency injection?
So is it bad form to simply do var employees = $http.get('employees') from the server?
Many thanks!
Service is your model, all call to your server should be centralized in services.
If i start from your example you should have an "EmployeeService"
And in it a function like that : (don't forget to inject $q and $http services)
me = this;
me.cacheEmployee = null;
me.getEmployees = function(){
var deferred = $q.defer();
if(me.cacheEmployee != null){
deferred.resolve(me.cacheEmployee);
}else{
$http.get('urlToEmployee').success(function(result){
me.cacheEmployee = result;
deferred.resolve(me.cacheEmployee);
});
}
return deferred.promise;
};
me.forceLoadEmployee = function(){ // after each CreateUpdateDelete operations
var deferred = $q.defer();
$http.get('urlToEmployee').success(function(result){
me.cacheEmployee = result;
deferred.resolve(me.cacheEmployee);
});
return deferred.promise;
};
And in the controller of the page you want to display your employees (don't forget to inject EmployeeService)
EmployeeService.getEmployees().then(function(result){
$scope.employees = result;
});
You use asynchronous call, you can't think in procedural way to resolve your problem.
A basic doc about promise : https://docs.angularjs.org/api/ng/service/$q
I hope my answer will fit you.
Have a nice day !
Edit : now you have a list of employee in cache, the call will be launched to the server only the first time.