Pass argument to method - php

I have functions that I use in my Article model, they add likes to cookies for a specific article and record the time
public static function hasLikedToday($articleId, string $type)
{
$articleLikesJson = \Cookie::get('article_likes', '{}');
$articleLikes = json_decode($articleLikesJson, true);
// Check if there are any likes for this article
if (! array_key_exists($articleId, $articleLikes)) {
return false;
}
// Check if there are any likes with the given type
if (! array_key_exists($type, $articleLikes[$articleId])) {
return false;
}
$likeDatetime = Carbon::createFromFormat('Y-m-d H:i:s', $articleLikes[$articleId][$type]);
return ! $likeDatetime->addDay()->lt(now());
}
public static function setLikeCookie($articleId, string $type)
{
// Initialize the cookie default
$articleLikesJson = \Cookie::get('article_likes', '[]');
$articleLikes = json_decode($articleLikesJson, true);
// Update the selected articles type
$articleLikes[$articleId][$type] = today()->format('Y-m-d H:i:s');
$articleLikesJson = json_encode($articleLikes);
return cookie()->forever('article_likes', $articleLikesJson);
}
The php.blade page itself has buttons
Like Heart
Like Finger
Here are the routes web.php
Route::get('/article', function () {
$articleLikesJson = \Cookie::get('article_likes', '{}');
return view('article')->with([
'articleLikesJson' => $articleLikesJson,
]);
});
Route::get('article/{id}/like', 'App\Http\Controllers\ArticleController#postLike');
And the postLike() function itself in the controller
public function postLike($id) {
$article = Article::find($id);
$like = request('like');
if ($article->hasLikedToday($article->id, $like)) {
return response()
->json([
'message' => 'You have already liked the Article #'.$article->id.' with '.$like.'.',
]);
}
$cookie = $article->setLikeCookie($article->id, $like);
$article->increment('like_{$like}');
return response()
->json([
'message' => 'Liked the Article #'.$article->id.' with '.$like.'.',
'cookie_json' => $cookie->getValue(),
])
->withCookie($cookie);
}
In general, what is the problem, I have 2 types of likes that can be seen in php.blade, and the problem is to pass the choice of the type of like to the postLike() function, if in my function instead of $like I write 'heart', then everything will be work, but I need to determine which type we choose (heart or finger), tell me how this can be done?

You can use Laravel's Request object.
https://laravel.com/docs/8.x/requests#input
Like this:
use Illuminate\Http\Request;
public function postLike($id, Request $request)
{
$type = $request->input('type');
}

Related

Laravel CustomRequest authorize, pass request data to validate the auth user customer id and the model id match

I'm trying to fix an if-else statement in the request for my controller. What I'm trying to do is: if the auth::user-companyID == $request-companyID then true else false; The companyID for the request is in a hidden field on the blade file.
CustomRequest
public function authorize()
{
$user = Auth::user();
if ($user->companyID == $request->companyID) {
return true;
} else {
return false;
}
}
Controller
public function edit(EquipmentRequest $request, $id)
{
$validated = $request->validated();
$user = Auth::user();
$equipment = EquipmentModel::where('id', '=', $id)->first();
$equipment->Year = $request->Year;
$equipment->Make = $request->Make;
$equipment->Model = $request->Model;
$equipment->Type = $request->Type;
$equipment->unitNumber = $request->unitNumber;
$equipment->AnnualInspectionDate = $request->AnnualInspectionDate;
$equipment->userID = $request->userID;
$equipment->companyID = $user->companyID;
$e = $equipment->save();
if ($e) {
$request->session()->flash('success', 'The equipment was successfully updated.');
} else {
$request->session()->flash('error',
'An error occurred while saving. Please refresh your browser and try again.');
}
return redirect()->route('equipmentlist');
}
This form worked before I started messing with it so I know the form is working correctly on the blade file. I'm not sure if you can pass the request data the way I'm doing it or if I have to do a construct to do it this way. I would really appreciate any advice.
use Illuminate\Http\Request;
public function authorize()
{
$user = auth()->user();
return $user->companyID === request()->companyID;
}

what is the correct way to pass 3 where () conditions in the same function, multiple filters | eloquente - laravel

I would like to know if I am doing it right, if my code is semantic and safe.
I need to load a page from the database, but to do this I need to cross some data and all must be compatible, the verification takes place as follows:
url: mysite.com/company/page/code
1 - Check the first parameter to see if company exists or not.
2 - check the second parameter to find out if company X has that page or not
3 - Check the third parameter to find out if page Y has a code and if that code matches what you typed.
4 - If the user arrived here and all the data are correct, please load the page and the corresponding data.
Here he checks if user X has page Y, my doubt is if I can do it like this or is there another way.
$page = Page::where('name', $name)->where('page_name', $page_name)->first();
if ($page === null) {
return view('company.pages.erros.404', compact('name', page));
}
Here is similar to the other, he checks if user X has page Y and if the code of page Y is correct, as in others, my doubt is if it is correct to put several WHERE clauses in the code
$pagecode = Page::where('name', $name)->where('page_name', $pagen_name)->where('code', $pcode)->first();
if ($pagecode === null) {
return view('company.pages.erros.invalid_code', compact('company, name', page, pcode));
}
as I thought you would want the code in general context, here is the full function code
public function loadpage($name, $page_name, $pcode)
{
$company = Company::where('name', $name)->first();
if ($company === null) {
return view('company.not_register', compact('name'));
}
$page = Page::where('name', $name)->where('page_name', $page_name)->first();
if ($page === null) {
return view('company.pages.erros.404', compact('name', page));
}
$pagecode = Page::where('name', $name)->where('page_name', $page_name)->where('code', $pcode)->first();
if ($pagecode === null) {
return view('company.pages.erros.invalid_code', compact('company, name', page, pcode));
}
$personality = DB::table('personalities')->where('name', $name)->first();
return view('company.pages.index', compact('company', 'name', 'personality', 'page', pcode));
}
based on laravel's documentation, I came up with the following result:
public function loadpage($name, $page_name, $pcode)
{
$company = Company::where('name', $name)->first();
if ($company === null) {
return view('company.not_register', compact('name'));
}
$page = Page::where([ ['name', $name],
['page_name', $page_name],])->first();
if ($page === null) {
return view('company.pages.erros.404', compact('name', page));
}
$pagecode = Page::where([ ['name', $name],
['page_name', $page_name], ['code', $pcode],])->first();
if ($pagecode === null) {
return view('company.pages.erros.invalid_code', compact('company, name', page, pcode));
}
$personality = DB::table('personalities')->where('name', $name)->first();
return view('company.pages.index', compact('company', 'name', 'personality', 'page', pcode));
}
now it's up to colleagues more experienced than me to see if that's right or is it possible to improve / simplify
In case you are searching in a single model and you are not sure when which fields occur you might want to use laravel's when() method. It will help you search the database only when the field is available for you. for an example:
$sortBy = null;
$users = DB::table('users')
->when($sortBy, function ($query, $sortBy) {
return $query->orderBy($sortBy);
}, function ($query) {
return $query->orderBy('name');
})
->get();
You will get the idea from here conditional query in laravel
if you have multiple where clause to pass to a where clause, you can have a array containing arrays with the structure as you would have in the where method, like:
$where = [
['name', $name],
['page_name', $page_name],
['code', $pcode],
/*
the arrays should have one of this two structure
["field", "value"], //using = operator as default
["field", "operator", "value"],
*/
];
and than call ->where() with this array as parameter, like:
Page::where($where)->first();
So your code can become
public function loadpage($name, $page_name, $pcode)
{
$name = ['name', $name];
$pageName = ['page_name', $page_name];
$pageCode = [ 'code', $pcode ];
$company = Company::where([$name])->first();
if ($company === null) {
return view('company.not_register', compact('name'));
}
$page = Page::where([$name, $pageName])->first();
if ($page === null) {
return view('company.pages.erros.404', compact('name', page));
}
$pagecode = Page::where([$name, $pageName, $pageCode])->first();
if ($pagecode === null) {
return view('company.pages.erros.invalid_code', compact('company, name', page, pcode));
}
$personality = DB::table('personalities')->where([$name])->first();
return view('company.pages.index', compact('company', 'name', 'personality', 'page', pcode));
}

In laravel in controller pass variable to one function to another function

I am coding in Laravel, How can I pass variable to one function to another function in Controller,
In controller file I have 2 functions like this
public function hiringEmployee(Request $request)
{
$hireEmployee = new EmployeeHire();
$hireEmployee->candidateName = $request->get('candidateName');
$file = $request->file('file');
$name = $file->getClientOriginalName();
$file->move('uploads/cv', $name);
$hireEmployee->file = $name;
$hireEmployee->save();
return redirect('list-candidate');
}
public function assignInterview(Request $request, $id)
{
$assignInterview = EmployeeHire::find($id);
$interview = $request->get('interview');
$assignto = $request->get('assignto');
$dateTime = $request->get('dateTime');
$note = $request->get('note');
$interviewDetails = ([
'interview' => $interview,
'assign_to' => $assignto,
'date_time' => $dateTime,
'note' => $note,
]);
$assignInterview->interview_details = $interviewDetails;
$assignInterview->save();
Mail::send('emails.hireemployee', ['candidateName' => $candidateName], function ($message) use ($assignto, $name) {
$message->subject('Interview For New Candidate!');
$message->from('hrm#wcg.com', 'HRM');
$message->to($mail);
$message->attach('uploads/cv/'.$name);
});
return redirect('list-candidate');
}
I want to use $candidateName and $name in assignInterview() function from hiringEmployee() function.
How can I do it?
You won't be able to use the $name and $candidateName directly from the other function as they look like they are for two different requests, however, it looks like you're saving that data to database when you're creating a new EmployeeHire in your hiringEmployee() method so you should already have access to that information in your assignInterview() method:
$assignInterview = EmployeeHire::find($id); // this is where you loading the model
$candidateName = $assignInterview->candidateName;
$name = $assignInterview->file;
In your situation , you can use two approach:
#1
Use Session Variable as below:
Session::put('candidateName', $candidateName);
Then:
$value = Session::get('candidateName');
#2
Use class attribute:
class acontroller extends Controller
{
private $classCandidateName;
}
You can try something like this:
public function hiringEmployee(Request $request)
{
$hireEmployee = new EmployeeHire();
$hireEmployee->candidateName = $request->get('candidateName');
$file = $request->file('file');
$name = $file->getClientOriginalName();
$file->move('uploads/cv', $name);
$hireEmployee->file = $name;
$hireEmployee->save();
return redirect('list-candidate');
}
public function assignInterview(Request $request, $id)
{
$assignInterview = EmployeeHire::find($id);
if(is_null($assignInterview)){
return redirect()->back()->withErrors(['Error message here']);
}
$interviewDetails = ([
'interview' => $request->get('interview'),
'assign_to' => $request->get('assignto'),
'date_time' => $request->get('dateTime'),
'note' => $request->get('note'),
]);
$assignInterview->interview_details = $interviewDetails;
$assignInterview->save();
Mail::send('emails.hireemployee', ['candidateName' => $assignInterview->candidateName], function ($message) use ($assignto, $assignInterview->file) {
$message->subject('Interview For New Candidate!');
$message->from('hrm#wcg.com', 'HRM');
$message->to($mail);
$message->attach('uploads/cv/'.$assignInterview->file);
});
return redirect('list-candidate');
}
Please, you should to be careful with find($id). If it is a null, you will get an error.
Have fun!

Laravel save multi part form

I have a 3 part form that I want to be able to update the database after each submit. There is one table that holds all the fields below.
form1 asks for first and last name
form2 asks for email and phone
form3 asks for city and state
In my controller I have 3 separate functions to save each step of the form:
public function name(Request $request){
$lead = Lead::firstOrNew(123);
$lead->firstName = $request->get('firstName ');
$lead->lastName = $request->get('lastName');
$lead->save();
return redirect('/form2');
}
public function info(Request $request){
$lead = Lead::find(123);
$lead->email = $request->get('email');
$lead->phone = $request->get('phone');
$lead->save();
return redirect('/form3');
}
public function address(Request $request){
$lead = Lead::find(123);
$lead->city = $request->get('city');
$lead->state = $request->get('state');
$lead->save();
return redirect('/done');
}
Is there any way to combine that to one update function?
Just do conditional check, update the model and define redirect url:
public function info(Request $request) {
$lead = Lead::firstOrNew(123);
if ($request->has('firstName') && $request->has('lastName')) {
$lead->firstName = $request->get('firstName ');
$lead->lastName = $request->get('lastName');
$redirect = '/form2';
} else if ($request->has('email') && $request->has('phone')) {
$lead->email = $request->get('email');
$lead->phone = $request->get('lastName');
$redirect = '/form3';
} else if ($request->has('city') && $request->has('state')) {
$lead->city = $request->get('city');
$lead->state = $request->get('state');
$redirect = '/done';
}
$lead->save();
return redirect($redirect);
}
Also, you can probably do group update via update just make sure you whitelist the attributes in your model
public function info(Request $request) {
$lead = Lead::firstOrNew(123);
$lead->update($request->all());
if ($request->has('firstName') && $request->has('lastName')) {
$redirect = '/form2';
} else if ($request->has('email') && $request->has('phone')) {
$redirect = '/form3';
} else if ($request->has('city') && $request->has('state')) {
$redirect = '/done';
}
return redirect($redirect);
}
Or I'd better just add a redirect variable to your form like:
<input type="hidden" name="redirect" value="form1">
and simplify your controller method like:
public function info(Request $request) {
$lead = Lead::firstOrNew(123);
$lead->update($request->all());
return redirect($request->input('redirect'));
}
You could refactor the methods to call a common "update" function. See example code below.
public function name(Request $request){
$this->update(123, $request);
return redirect('/form2');
}
public function info(Request $request){
$this->update(123, $request);
return redirect('/form3');
}
public function address(Request $request){
$this->update(123, $request);
return redirect('/done');
}
private function update($id, $request) {
$lead = Lead::find($id);
foreach ($field as ['firstName', 'lastName', ...]) {
if ($request->has($field)) {
$lead->{$field} = $request->get($field);
}
}
$lead->save();
}
You can add a hidden field to all three forms (but with the name name, e.g form_name), and set their values to identify the form (form1, form2, form3) when it is submitted. Then in your controller, you check the value of the form_name field on the request to determine where you want to redirect to, like this:
public function info(Request $request) {
$lead = Lead::firstOrNew(123);
$lead->update($request->all());
//this will be from the hidden field (form_name)
$form_type = $request->get('form_name');
if ($form_type == 'form1') {
$redirect = '/form2';
} else if ($form_type == 'form2') {
$redirect = '/form3';
} else if ($form_type == 'form3') {
$redirect = '/done';
}
return redirect($redirect);
}
If you have the option for using javaScript then save the first and second form data on cookies or local storage and when the user reaches last part of the form then take data out of cookies or local storage then added with the last form, but keep these in the hidden input.
Given that HTTP requests are stateless (which means each request know nothing about the one before and after it), I would rather prefer you use sessions, so that you can be able to store information as you redirect from one form to the other. In that case, your code should look like so:
<?php
public function name(Request $request){
Session::put('nameData', $request->all()); //Store the info from form1 in session and redirect to form2
return redirect('/form2');
}
public function info(Request $request){
$validSessionData = Session::has('nameData');
if (!$validSessionData) { //Check if the user filled form1, if not, go back to form1
return redirect('/form1');
}
$nameAndInfo = Session::pull('nameData', []) + $request->all(); //Merge the info from form1 with info from form2. You could decide to keep them separate and merge later.
Session::put('nameAndInfo', $nameAndInfo);
return redirect('/form3');
}
public function address(Request $request){
$validSessionData = Session::has('nameAndInfo');
if (!$validSessionData) { Another check. You could also extend this by checking for form2 session data
return redirect('/form1');
}
$allData = Session::pull('nameAndInfo', []) + $request->all(); //Merge all session data
$lead = Lead::firstOrNew(123);
$lead->firstName = $allData['firstName'];
$lead->lastName = $allData['lastName'];
$lead->email = $allData['email'];
$lead->phone = $allData['phone'];
$lead->city = $allData['city'];
$lead->state = $allData['state'];
$lead->save();
return redirect('/done');
}

Laravel5.2 Session use forget() but no work

I use Laravel5.2 to build a E-commerce platform and get some troubles.When I loginout user state and I use Session::forget('user') or $request->session()->forget('user'), it works in current page,and I redirect to the login page and dd(Session::all()) find the session 'user' still exist!!! so I am confused to know what is wrong with it? plz tell me the reason,thanks all.
ps. loginout code
public function logout(Request $request)
{
if ($request->session()->has('user')) {
$is_forgotten = $request->session()->forget('user');
if ($is_forgotten === null)
echo json_encode(['result' => 1]);
exit;
}
echo json_encode(['result'=>0,'msg'=>'loginout error']);
exit;
}
The session doesn't get saved until the response is sent and events/middleware are triggered.
Try doing:
public function logout(Request $request)
{
if ($request->session()->has('user')) {
$is_forgotten = $request->session()->forget('user');
if ($is_forgotten === null)
return Response::json(['result' => 1]);
return '';
}
return Response::json(['result'=>0,'msg'=>'loginout error']);
}
Or if your using the helpers:
public function logout(Request $request)
{
if ($request->session()->has('user')) {
$is_forgotten = $request->session()->forget('user');
if ($is_forgotten === null)
return response()->json(['result' => 1]);
return '';
}
return response()->json(['result'=>0,'msg'=>'loginout error']);
}

Categories