I am using Laravel.
And I have a controller action, which get the orders' information by date. and sum all the amount of orders, and it will return datas.
And I want to call it in Console\Command script so I don't need to repeat the same code, just get those datas from action.
Please help me how to do it?
Do something like this:
Without parameters:
\App::call('App\Http\Controllers\MyController#actionName')
If you have Request parameters:
$request = new \Illuminate\Http\Request($datas);
$controller = app()->make(MyController::class);
$Response = $controller->callAction('ActionName',[$request]);
I wanted to call the CreateNewUser action from the FortifyServiceProvider. It took me some time to find out, but it is quite simple:
use App\Actions\Fortify\CreateNewUser;
// Use fortify to create a new user.
$new_user_action = new CreateNewUser();
$user = $new_user_action->create($input);
Related
I have Laravel project and in this project I have to create Request object to use it like this:
$request = new Request();
I tried to add from and to dates like this:
$request->request->add([from'=>$from_temp,'to'=>$to_temp]);
when I dd the request it gives me that from and to are exist(image)
image for request Object
but when I try to access them using
request('from')
it gives me null
I don't know what's going on
Thank you
request('from') helper will pull values form the global request object, not from the $request variable that you created. What you want to to is use $request->get('from')
So, I have the following code:
$homepage = Homepage::first();
if (!$homepage) {
$homepage = new Homepage;
}
$homepage->first_presta_title = $request->first_presta_title;
$homepage->first_presta_content = $request->first_presta_content;
$homepage->second_presta_title = $request->second_presta_title;
$homepage->second_presta_content = $request->second_presta_content;
$homepage->third_presta_title = $request->third_presta_title;
$homepage->third_presta_content = $request->third_presta_content;
$homepage->shiatsu_text = $request->shiatsu_text;
$homepage->shiatsu_image = $request->shiatsu_image;
$homepage->doin_text = $request->doin_text;
$homepage->doin_image = $request->doin_image;
$homepage->save();
Everything works, but I wanted to see if there weren't any better way to save datas without asigning every single element to its column, then I found out someone answering to a question by using the following code:
$homepage->save($request->all());
So I tried it by myself, but nothing happened: no error, but also nothing saved in my database.
So, is there any fastest way to save datas ? Is it possible to use a loop to save everything?
Thank you in advance
When you use save(), you are actually using Mass assignment. So, either you explicitly define all the fields in your model to be mass assignable or you could use create() instead.
However, in your particular case, the whole method could be cleaned up to just one line:
return Homepage::updateOrCreate($request->all());
If you want the model to autofill based on a given array you need to create a new model entity Like this
$homepage = HomePage::create($request->all());
$homepage->save()
If you give an array to save() it expects the options for saving not for values to assign
Source:
laravel api docs for model/save()
laravel api docs for model::create()
I am stuck with it and i couldn't find any appropriate solution for this. what i want to achieve is in my admin panel based upon check box value i want to change active status of specific control and update database value using ajax. I want to make a common ajax function in controller to avoid repeatedly writing same ajax function for other controls like menu manager content manager, documents manager etc.So i want to send Model name to the ajax controller so that same function can be used. Ajax call is working perfectly but couldn't make appropriate models. for example:
$m = new App\Model.$request->model OR $m = 'App\Model\'.$request->model (adding last \ gives an error) or answer provided in Dynamically use model in laravel is not helping either. Is there any better ways if yes please suggest. I can do this below but it is hardcoded so i want to make model dynamic models
if($request->model ==='Menu')
$model = new \App\Http\Models\Menu;
else if($request->model === 'News')
$this->model = new \App\Http\Models\News;
else if($request->model === 'Document')
$this->model = new \App\Http\Models\Document;
Thankyou !!!
You can just use:
$modelName = '\\App\\Http\\Models\\'.$request->model;
$this->model = new $modelName;
However you should add validation to make sure only some allowed models would be used or at least do something like this:
if (! in_array($request->model, ['Menu', 'News', 'Document']))
{
throw new \Exception('Invalid model');
}
$modelName = '\\App\\Http\\Models\\'.$request->model;
$this->model = new $modelName;
This is because you don't want to expose probably all the models data for security reasons.
Try the below code
if($request->model){
$m = '\App'. '\' .$request->model;
//other code
}
I want to pass data from one application to another in Laravel..
Suppose I want to take user data from one application and I want to send this data to another application form.
I wanted to send it this way..But I'm getting Error..
So if anyone could suggest any solution for this..would be appreciated ..
public function store(Request $request)
{
$payment = new Payment();
$payment->username = $request->Input(['username']);
$payment->price = $request->Input(['price']);
$payment->purchase_id = $request->Input(['purchase_id']);
$payment->save();
$store_id =\Hash::make($payment->id);
$price = $payment->price;
return \Redirect::to('http://localhost/blog/public/getPayment');
}
And I get the following Error:
InvalidArgumentException in Response.php line 462:
The HTTP status code "0" is not valid.
If you just want your user to be redirected to another app so they can fill whatever forms you can use
return Redirect::away('http://localhost/blog/public/getPayment');
See the method here.
BUT
If you want to send data to another app without redirecting the user, the best solution I can give is to make an API on this other app so you can make requests (with Guzzle or something else) to this API, allowing you to pass some data.
I have a CakePHP website and navigaton links are stored in database. What i want is for some navigation entries to call custom function which will return some additional, dynamic data about the link: I want to add a count of articles for link "Vacancies". I could call a function on a model that would return total count. This link is to be rendered on every page.
So i need to get appropriate models instance, but not for the current request, but the request where url points to.
So basically i have url "/en/vacancies". I can get controller name by:
$urlInfo = Router::parse("/en/vacancies");
$controllerName = $urlInfo['controller'];
What would be the reliable way to do that?
Any other solutions for the problem are welcome.
Assuming you have the method to gather the navigation link data in a Model.
App::import('Controller', $controllerName);
$controller = new $controllerName;
$controller->loadModel('YourModel');
$yourModel = $controller->YourModel;
$yourData = $yourModel->your_method();
There are a variety of other ways to do this. But, without knowing more about where you're actually going to be calling this function I can't really provide anymore suggestions.