Undefined Variable $Member when trying to save data in my database phpmyadmin - laravel

This is the controller code.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Member;
class membercontroller extends Controller {
function addmemb(Request $req) {
$member = new $Member;
$member->name = $req->name;
$member->age = $req->age;
$member->email = $req->email;
$member->save();
}
}
View Code:
<h1>Enter The Member Info </h1>
<form action= "addmembers" method="POST">
#Csrf
<input type="text" name="name" placeholder="Enter Your Name."> <br><br>
<input type="text" name="age" placeholder="Enter Your Age."> <br><br>
<input type="text" name="email" placeholder="Enter Your Email."> <br><br>
<button type="submit">Add Member</button> <br><br>
</form>
Route::view('addmembers', 'addmembers');
Route::post('addmembers', [membercontroller::class, 'addmemb']);
These are the Routes for the view and functions. Can anyone point out my mistake? It's saying Undefined Variable.
Whenever I try to save data in my database it shows an error which says $Member is undefined. Please point out what should I do to make this run?

$Member is not a Variable! its a model so you have to use it like this:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Member;
class membercontroller extends Controller
{
function addmemb(Request $req){
$member = new Member; ////////
$member->name = $req->name;
$member->age = $req->age;
$member->email = $req->email;
$member->save();
}
}

Related

Laravel with VueJS 500 (Internal Server Error)

I have the issue when I fetch states with VueJs.
getStates(){
axios.get("/api/employees/"+ this.form.country_id + "/states")
.then(res => {
this.states = res.data
}).catch(error => {
console.error(error.response.data)
})
}
This api.php .
<?php
use App\Http\Controllers\Api\EmployeeDataController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
Route::get('/employees/countries', [EmployeeDataController::class, 'countries']);
Route::get('/employees/{country}/states', [EmployeeDataController::class, 'states']);
Route::get('/employees/departments', [EmployeeDataController::class, 'departments']);
Route::get('/employees/{state}/cities', [EmployeeDataController::class, 'cities']);
This is EmployeeDataController contains:
public function states(Country $country)
{
return response()->json($country->states);
}
This is country model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Country extends Model
{
use HasFactory;
protected $fillable = ['country_code', 'name'];
public function states()
{
return $this->hasMany(States::class);
}
}
This is create.vue blade which I store datas.
<div class="row mb-3">
<label for="country" class="col-md-4 col-form-label text-md-end">Country</label>
<div class="col-md-6">
<select v-model="form.country_id" #change="getStates()" name="country" class="form-control" aria-label="Default select example">
<option v-for="country in countries" :key="country.id" :value="country.id">{{country.name}}</option>
</select>
</div>
</div>
This create form that when I tapped select country, automatically state must fill with state data which I want to fetch.
But I have face to 500 internal server error
I hope to explain my issue.

Add toomanyattempts in my login controller in laravel

I need to add the functionality that toomanyloginattempts with my login . now its not working. Iam using Laravel Framework 5.1.45 (LTS).
The code that i used is mentioned below.
My controller function is
<?php
use App\Libraries\SessionHelper;
use App\Libraries\ConfigUtils;
use App\Libraries\GeneralLib;
use App\Models\OrgSettings;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
class LoginController extends Controller {
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
public function doLogin() {
$email = Input::get('email');
$pass = Input::get('password');
$candidate_login_user = User::getUserByEmail($email);
$data = User::authenticate($email, $pass);
if (empty($data)) {
User::logFailedAuthentication($email, $candidate_login_user->organization);
Session::flash('error', "Incorrect email or password.");
return Redirect::to('/login');
}
}
my view page is as follows
<form action="login" method="post">
<div class="body bg-gray">
<div class="alert alert-danger">
<strong >Whoops!</strong> There were some problems with your input.<br><br>
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<?php
Session::forget('error');
Session::forget('success');
?>
<div class="form-group">
<input type="email" name="email" class="form-control"
placeholder="email"/>
</div>
<div class="form-group">
<input type="password" name="password"
class="form-control" placeholder="password"/>
</div>
Since you're implementing your own login action it is not enough to just add the traits to your LoginController to implement throttling.
You need to be checking the hasTooManyLoginAttempts method from your doLogin action, and firing the lockout event yourself, if necessary.
public function doLogin(\Illuminate\Http\Request $request) {
if ($this->hasTooManyLoginAttempts($request)) {
$this->fireLockoutEvent($request);
return $this->sendLockoutResponse($request);
}
$email = Input::get('email');
$pass = Input::get('password');
$candidate_login_user = User::getUserByEmail($email);
$data = User::authenticate($email, $pass);
if (empty($data)) {
User::logFailedAuthentication($email, $candidate_login_user->organization);
Session::flash('error', "Incorrect email or password.");
$this->incrementLoginAttempts($request);
return Redirect::to('/login');
}
}
Altogether, I think you'd be better off simply using the built-in Auth controllers to handle your login (or at least using them as a starting point) rather than reimplement your own.

Get textarea Info Value LARAVEL

How get value / info in LARAVEL?
admin.blade.php: https://pastebin.com/XHMM3PN9
GeneralController.php
$socket = $_POST['compr'];
ROUTE:
Route::post('sname', 'GeneralController#Sname')->middleware(['auth','admin:6']);
Your form should be :
<form name="fname" action="{{ url('sname') }}" method="post">
<legend>Server Name</legend>
<textarea name="compr" rows="1" placeholder="Server Name" class="form-control">{{ old('compr') }}</textarea>
<input class="btn btn-success btn-sm btn-block" type="submit" value="Set Server Name" />
</form>
Controller Method:
public function Sname(Request $request) {
$socket = $request->compr;
}
Good luck.
As I understand you would like to get input value from the request in your controller.
You can do this as follows:
use Illuminate\Http\Request;
class GeneralController extends Controller
{
public function Sname(Request $request)
{
$compr = $request->input('compr');
// do something with your input
}
}

how to Pass array from view to controller in Laravel?

I make a form in blade.php, Here I can select multiple checkbox, and I want to pass selected input’s value to controller in a array.But I failed, I can not send the data.
Here is code from view. The selected input’s value can be 1,2 etc;
<form method="post" action="{{action('votesubmitController#votesubmit')}}" class="form">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
#foreach($candidate_list[$post_list->id] as $candidate_list[$post_list->id])
<li>
<input type="checkbox" name= "selected[]" value= {{
$candidate_list[$post_list->id]->id }}>
<label>
<!-- some code here -->
</label>
</li>
#endforeach
<button type="submit" id="login-button">Submit</button>
</form>
Here is route-
Route::post('/votesubmit','votesubmitController#votesubmit');
If I write return $input in controller I find –
{"_token":"TQIUxVz0LjzM84Z7TaUPk3Y7BLZPjmXUyhXhlQfp","selected":
["1","2"]}
That’s I need. I do not know how to get selected value. When I get specific route error exception happens . and says "Undefined variable: selected".
Here is my Controller’s code-
class votesubmitController extends Controller
{
public function votesubmit()
{
$input = Input::all();
// return $input;
foreach ($selected as $selected) {
echo $selected;
}
}
}
// You can try this
class votesubmitController extends Controller
{
public function votesubmit()
{
//$input = Input::all();
//$selected = $input['selected'];
$selected = Input::get('selected');
foreach ($selected as $selected)
{
echo $selected;
}
}
}
Either use
$selected = $input['selected']
Or
pass it using Ajax.

I want to do some api work in codeigniter,i am beginner with codeIgninter , I started studing codeigniter.know i want to know get the value from

I want to do some api work in codeigniter,I am beginner with codeIgniter , I started studying codeigniter. know i want to know how to send the value to view.
In View folder I have written like this:
<h1 align="center">Test Sample CI Framwork</h1>
<form name="f1" action="" method="post"/>
<input type="text" name="firstname" value="" size="100" />
<input type="text" name="lastname" value="" size="100"/>
<div><input type="submit" value="Submit" name="submit"/></div>
</form>
<h1>The values from form:<?php echo $result; ?></h1>
IN controller:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class add extends CI_Controller
{
public function index()
{
$this->load->view('add');
$this->getvalue();
}
function getvalue()
{
if ($this->input->post('submit'))
{
$data=$this->input->post('firstname');
$data=$this->input->post('lastname');
//help me what i have to do here.
}
}
}
?>
Yes, basically for transmit a data to the view, use :
$this->load->view('you-page',$data);
Your controller should be like this:
class add extends CI_Controller {
public function index() {
$data = array();
if ($this->input->post()) {
$data['firstname'] = $this->input->post('firstname');
$data['lastname'] = $this->input->post('lastname');
}
$this->load->view('add', $data);
}
}
////add.php////
<h1 align = "center">Test Sample CI Framwork</h1>
<form name = "f1" action = "" method = "post"/>
<input type = "text" name = "firstname" value = "" size = "100" />
<input type = "text" name = "lastname" value = "" size = "100"/>
<div>
<input type = "submit" value = "Submit" name = "submit"/>
</div>
</form>
<h1>The values from form:<?=$firstname . '-' . $lastname?></h1>

Resources