Gathering data from multi page form, and adding additional data - laravel

So I'm data from a multi-page form, the data is stored like this.
I'm using this tutorial https://www.5balloons.info/multi-page-step-form-in-laravel-with-validation/
public function store(Request $request)
{
$user = $request->session()->get('user');
$user->save();
return redirect('/home');
}
That works fine. But how do I add additional data manually using the arrow function? For example, I need to set a status, the ip address, ect. Something like 'status' => 1

Assuming this is the only place you want to add these values to users, you could just add the values after you got it from the session:
public function store(Request $request)
{
$user = $request->session()->get('user');
$user->ip_address = '127.0.0.1';
$user->status = 1;
$user->save();
return redirect('/home');
}

you can add addition data like:
if your $user is laravel object then
$user->setAttribute('status', '1');
or $user if array then
$user['status']=1;

Related

Data that has been edited is not updated into database

i had some problem for updating my data, i was able to catch the file that was going to be updated by using dd and they are there, but when i was submitting the form the data remains the same, nothing change and no error whatsoever, here are my store(update) controller :
public function store(Request $request)
{
//dd($request);
$request->validate([
'attachment_name' => 'required|file|image|mimes:jpeg,png,jpg,gif,svg|max:10048',
]);
$storedImage = $request->attachment_name->store('public/image/');
MediaOrder::updateOrCreate(['id' => $request->id],
[
'nomor'=> $request->nomor,
'nomor_reference'=> $request->nomor_reference,
'periode_start'=> $request->periode_start,
'periode_end'=> $request->periode_end,
'category_id'=> $request->category_id,
'type_id'=> $request->type_id,
'agency_code'=> $request->agency_code,
'agency_name'=> $request->agency_name,
'advertiser_code'=> $request->advertiser_code,
'advertiser_name'=> $request->advertiser_name,
'brand_code'=> $request->brand_code,
'brand_name'=> $request->brand_name,
'nett_budget'=> $request->nett_budget,
'gross_value'=> $request->gross_value,
'nett_cashback'=> $request->nett_cashback,
'nett_bundling'=> $request->nett_bundling,
'version_code'=> $request->version_code,
'spot'=> $request->spot,
'accountexecutive_name'=> $request->accountexecutive_name,
'group_id'=> $request->group_id,
'userto_name'=> $request->userto_name,
'notes'=> $request->notes,
'attachment_name'=> $storedImage,
]);
flash_success_store('Media Order successfully updated.');
if ($request->ajax()) {
return redirect_ajax_notification('media-order.index');
} else {
return redirect_ajax('media-order.index');
}
}
i already find and change it myself but to no avail, maybe someone can have a solution?, thank you for your time.
The most common reason that the model is not updated or entered is that you do not make the attributes fillable in the model itself. Go to your model MediaOrder and check if you have set all attributes fillable.
protected $fillable = [..];

How do I loop over json data

I am using FlutterWave as my payment gateway, I want to loop through a transaction whenever a customer finish making payment and check for the chargeamount which the user paid. the return data is in json format. Here is my code.
public function callback(Request $request)
{
$resp = $request->resp;
$body = json_decode($resp, true);
$txRef = $body['data']['data']['txRef'];
$data = Rave::verifyTransaction($txRef);
dd($data);
return redirect()->route('success');
}
I want to loop and check for the chargedamount but I couldn't. Thanks for your help
Seems like you are dumping an object. I don't think you need a loop to access the data you need. According to your image dump, to access the chargedamount attribute:
dd($data->data->chargedamount);
The data name here repeats, perhaps $payload in this case, its a better name.
public function callback(Request $request)
{
$resp = $request->resp;
$body = json_decode($resp, true);
$txRef = $body['data']['data']['txRef'];
$payload = Rave::verifyTransaction($txRef);
dd($payload->data->chargedamount);
return redirect()->route('success');
}
Some clean up:
public function callback(Request $request)
{
$json = json_decode($request->resp);
$payload = Rave::verifyTransaction($json->data->data->txRef);
dd($payload->data->chargedamount);
return redirect()->route('success');
}
Again that data->data repeating stuff, but in this case, nothing we can do, its in the API.

How to check data selected using LOG:INFO Laravel

How I can check the selected elements that I pass from front end using Log::info? I tried using this but I dont know how to check the result, or maybe the code is wrong?
public function filterQuery(Request $request){
$name= $request->name;
$age= $request->age;
Log::info('Showing user profile for user: '.$name);
$query = user::query();
if(!empty($request->name)){
$query->where('name',$name);
}

How to add expiry date condition to login function in laravel 5.2

In laravel 5.2, i want to add the condition so that only users where their expiry date is greater than today's date to login.
protected function getCredentials(Request $request)
{
return ['email' => $request->{$this->loginUsername()}, 'password' => $request->password];
}
The code does not accept adding:
'expires' => gte(Carbon::now())
Any help is appreciated
I don't think this is possible, even in Laravel 5.5. Taking a look at the retrieveByCredentials method in Illuminate\Auth\EloquentUserProvider which is used to get the user from the database, you can see that the query passes simple key/value combinations to the where method on the $query object, which equate to where key = value. This is from 5.5:
public function retrieveByCredentials(array $credentials)
{
if (empty($credentials) ||
(count($credentials) === 1 &&
array_key_exists('password', $credentials))) {
return;
}
// First we will add each credential element to the query as a where clause.
// Then we can execute the query and, if we found a user, return it in a
// Eloquent User "model" that will be utilized by the Guard instances.
$query = $this->createModel()->newQuery();
foreach ($credentials as $key => $value) {
if (! Str::contains($key, 'password')) {
$query->where($key, $value);
}
}
return $query->first();
}
To achieve what you are after I would recommend doing this check after the user has logged in, in your controller for instance:
// Imagine this is the controller method where you're dealing with user logins
public function login(array $credentials)
{
if (! auth()->attempt($credentials)) {
// Handle what happens if the users credentials are incorrect.
}
$user = auth()->user();
if (Carbon::now()->gte($user->expires)) {
// User's account has expired, lets log them out.
auth()->logout();
// Return a redirect with a message or something...
}
// Handle a successful login.
}
I'm not sure if the auth() helper is available in 5.2, but you should be able to use the Auth facade to do the same thing, e.g. Auth::attempt(...).

Attach authenticated user to create

I'm trying to attach the currently logged in user to this request, so that I can save it in the database. Can someone point me in the right direction, please?
public function store(CreateLeadStatusRequest $request)
{
$input = $request->all();
$leadStatus = $this->leadStatusRepository->create($input);
Flash::success('Lead Status saved successfully.');
return redirect(route('lead-statuses.index'));
}
So, I have come up with the following using array_merge, but there must be a better way, surely?
public function store(CreateLeadStatusRequest $request)
{
$input = $request->all();
$userDetails = array('created_by' => Auth::user()->id, 'modified_by' => Auth::user()->id);
$merged_array = array_merge($input, $userDetails);
$leadStatus = $this->leadStatusRepository->create($merged_array);
Flash::success('Lead Status saved successfully.');
return redirect(route('lead-statuses.index'));
}
So you can use Auth Facade to get information of currently logged user.
For Laravel 5 - 5.1
\Auth::user() \\It will give you nice json of current authenticated user
For Laravel 5.2 to latest
\Auth::guard('guard_name')->user() \\Result is same
In laravel 5.2, there is new feature called Multi-Authentication which can help you to use multiple tables for multiple authentication out of the box that is why the guard('guard_name') function is use to get authenticated user.
This is the best approach to handle these type of scenario instead of attaching or joining.
public function store(CreateLeadStatusRequest $request)
{
$input = $request->all();
$userDetails = \Auth::user(); //Or \Auth::guard('guard_name')->user()
$leadStatus = $this->leadStatusRepository->create($input);
Flash::success('Lead Status saved successfully.');
return redirect(route('lead-statuses.index'));
}
Hope this helps.

Resources