Compact Two Functions in One View In Laravel - laravel

In my Controller
public function startQ(Request $request){
$users = question::limit(2)->get();
return view('answerDesk',compact('users'));
}
public function answerQuestions(Request $request){
$answers =$request->all();
return view('answerDesk',compact('answers'));
}
in My View
<form action="getAnswer" method="post" role="search"> #csrf #foreach($users as $answer) <input type="hidden" name="question[]" value="{{$answer->id}}">
<label class="form-check-label" for="inlineRadio1">
<h4 style=""><i class="fas fa-circle"></i>
{{$answer->id}}:{{$answer->question}}
</h4>
</label><br>
<input type="radio" name="answer[{{$answer->question}}]" value="{{$answer->a}}">(A) : <label class="form-check-label" for="inlineRadio1">{{$answer->a}}</label><br>
<input type="radio" name="answer[{{$answer->question}}]" value="{{$answer->b}}">(B) : <label class="form-check-label" for="inlineRadio1">{{$answer->b}}</label><br>
<input type="radio" name="answer[{{$answer->question}}]" value="{{$answer->c}}">(C) : <label class="form-check-label" for="inlineRadio1">{{$answer->c}}</label><br>
<input type="radio" name="answer[{{$answer->question}}]" value="{{$answer->d}}">(D) : <label class="form-check-label" for="inlineRadio1"> {{$answer->d}}</label><br>
<input type="hidden" name="ans[{{$answer->question}}]" value="{{$answer->ans}}"> #endforeach <input type="submit" value="submit">
</form>
I don't know how to use the getAnswer function
Can I Compact Two Functions in Controller To One View In Laravel?
If Yes How can I Do That?

Related

Create not saving to database laravel

For a school assignment we are to have a site that lets you Create Update Edit and Delete a player and Add a Country.
I am having trouble with my create as it is not saving to the database and returns no error. I have a feeling it is because of my foreign key and I've been looking all over stackoverflow and laravelforums as to how to do this or why it isn't saving to my database.
(As a note all my inputs are text for now until I get it working or get an error i can work with)
Player Model
protected $primaryKey = 'Id';
protected $fillable =['name','age','role','batting','bowling','image','odiRuns','countries_id'];
public function country()
{
return $this->belongsTo('App\Country','countries_id');
}
Store Fuction
public function store(Request $request)
{
//
$player = new Player;
$player->name = $request->name;
$player->age = $request->age;
$player->role = $request->role;
$player->batting = $request->batting;
$player->bowling = $request->bowling;
$player->image = $request->image;
$player->odiRuns = $request->odiRuns;
$player->countries_id = $request->countries_id;
$player->save();
return redirect('index');
}
Form
<form action="{{ route('player.store') }}" method=“post”>
#csrf
<div class="form-group">
<label for="name">Name </label>
<input type="text" class="form-control" name="name" id="name" placeholder="First and Last" >
</div>
<div class="form-group">
<label for="age">Age </label>
<input type="text" class="form-control" name="age" id="age" placeholder="Age" >
</div>
<div class="form-group">
<label for="role">Role </label>
<input type="text" class="form-control" name="role" id="role" placeholder="Role" >
</div>
<div class="form-group">
<label for="batting">Batting </label>
<input type="text" class="form-control" name="batting" id="batting" placeholder="Batting">
</div>
<div class="form-group">
<label for="Bowling">Bowling</label>
<input type="text" class="form-control" name="bowling" id="bowling" placeholder="Bowling">
</div>
<div class="form-group">
<label for="odiRuns"> OdiRuns </label>
<input type="number" class="form-control" name="odiRuns" id="odiRuns" value="odiRuns" placeholder="OdiRuns" required>
</div>
<div class="form-group">
<label for="image">Add Image</label>
<input type="file" name="image" class="form-control-file" id="InputFile" value="image">
</div>
<div class="form-group">
<label for="Country">Country</label>
<input type="text" class="form-control" name="countries_id" id="countries" placeholder="country">
</div>
<button type=“submit” class=“btn btn-primary”>Create</button>
</form>
Player Database
public function up()
{
Schema::create('players', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('age');
$table->string('role');
$table->string('batting');
$table->string('bowling');
$table->string('image');
$table->string('odiRuns');
$table->integer('countries_id')->unsigned();
$table->foreign('countries_id')->references('id')->on('countries');
$table->timestamps();
});
}
Your form is posting a GET request instead of POST request
It's a bit difficult to notice but method=“post” should be method="post"
double quotes instead of that MS word weird character
Specify that your form can post files such as images like so
<form action="{{ route('player.store') }}" method="post" enctype="multipart/form-data">
Otherwise it won't post the image and it's not nullable in your migration
change:
$player = new Player();
and why you don't use 'select' for countries_id
like :
<select name="countries_id">
<option value=""></option>
</select>
method="post"

How to add permission for a user on specific page

I have view, edit and delete permissions to a specific page, so I need to get these values:
page_id, user_id, edit, view, delete
I have a form with page name and 3 checkboxes for each page
<div class="container">
<form action="/test" method="POST">
#csrf
<div class="form-group">
#foreach( $tmp as $key=>$val )
<li>
#if( !isset($val['children']) && empty($var['children']))
<b>{{ $val['data']['name'] }}</b>
#else
<b>{{ app()->getLocale() == 'ar' ? $val['data']->ar_name : $val['data']->display_name }}</b>
#endif
#if( isset($val['children']) and !empty($val['children']))
<ul>
#foreach( $val['children'] as $c=>$child)
<li>
{{ $child->name }} :
<div class="form-group">
<input type="hidden" name="user_id" value="{{ $user_id }}">
<input type="hidden" name="page_id" value="{{ $child->name }}">
<label class="checkbox-inline">
<input type="checkbox" name="arr[]" value="view">View
</label>
<label class="checkbox-inline">
<input type="checkbox" name="arr[]" value="edit">Edit
</label>
<label class="checkbox-inline">
<input type="checkbox" name="arr[]" value="delete">Delete
</label>
</div>
</li>
#endforeach
</ul>
#endif
</li>
#endforeach
<br>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
but when i sent data, i can't get the correct page_id, is there anything else can i do
I solved it by adding $child->id
<div class="form-group">
<input type="hidden" name="user_id" value="{{ $user_id }}">
<input type="hidden" name="page_id[{{$child->id}}]" value="{{ $child->id }}">
<label class="checkbox-inline">
<input type="checkbox" name="view[{{$child->id}}]" value="1">View
</label>
<label class="checkbox-inline">
<input type="checkbox" name="edit[{{$child->id}}]" value="1">Edit
</label>
<label class="checkbox-inline">
<input type="checkbox" name="del[{{$child->id}}]" value="1">Delete
</label>
</div>

how to store answer into a database

I have created a multiple choice question and able to store them into a database, I need when a user select the correct answer it stores the id of the question and the answer. Please anyone who can help me how to do it
Here is my form for creating the questions
#foreach($qns as $qn)
{{$qn->question_name}}
<div class="form-check">
<label class="form-check-label" for="opt1">
<input type="radio" class="form-check-input" id="opt1"
name="opt{{ $qn->id }}" value="a">{{$qn->opt1}}
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="opt2">
<input type="radio" class="form-check-input" id="opt2"
name="opt{{ $qn->id }}" value="b">{{$qn->opt2}}
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="opt3">
<input type="radio" class="form-check-input" id="opt3"
name="opt{{ $qn->id }}" value="c">{{$qn->opt3}}
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="opt4">
<input type="radio" class="form-check-input" id="opt4"
name="opt{{ $qn->id }}" value="d">{{$qn->opt4}}
</label>
</div>
#endforeach
Here is my controller
public function store(Request $request){
// dd($request->all());
$this->validate($request,[
'question_id' =>'required|string',
'opt'.$qn->id =>'required|string',
]);
$qn= Answer::create([
'question_id'=>$request['question_id'],
'opt'=>$request['opt'.$qn->id],
]);
}
Do it like
public function saveAnswers(Request $request)
{
$questions = Questions::all();
foreach($questions as $qns){
$name = "opt".$qns->id;
Answer::create([
'question_id'=>$qns->id,
'answer'=>$request->$name,
]);
}
}

why auth its not working in my validation and its showing anything wrong

im just new in laravel and i dont know whats wrong with my code its not redirecting
public function login(Request $request){
$this->validate($request, [
'email'=> 'required|max:32',
'password'=> 'required|confirmed|max:32|min:8',
]);
if (Auth::attempt(['email'=>$request->email,'password'=>$request->password])) {
return redirect('/welcome');
}
return "Oooops something wrong happen";
}
this my login form
<form action="login" method="post">
<input type="hidden" name="_token" value="{{csrf_token()}}">
<div class="form-group">
<input type="email" name="" value="" class="form-control" placeholder="Enter Your Email">
</div>
<div class="form-group">
<input type="password" name="" value="" class="form-control" placeholder="Enter Your password">
</div>
<div class="form-group">
<input type="submit" name="" value="login" class="btn btn-success btn-lg btn-block">
</div>
and my route
Route::post('/login', 'UserController#login');
Your form is wrong, your name values are empty, and you're missing the confirmation password field
<form action="login" method="post">
{{ csrf_field() }}
<div class="form-group">
<input type="email" name="email" class="form-control" placeholder="Enter Your Email">
</div>
<div class="form-group">
<input type="password" name="passowrd" class="form-control" placeholder="Enter Your password">
</div> <div class="form-group">
<input type="password" name="passowrd_confirmation" class="form-control" placeholder="Enter Your password">
</div>
<div class="form-group">
<input type="submit" value="login" class="btn btn-success btn-lg btn-block">
</div>
</form>

Laravel 5.2 cannot update record

I cannot seem to update my record.
My controller
public function add()
{
return view('cars.add');
}
public function edit($id)
{
$car = Cars::whereId($id)->firstOrFail();
return view('cars.edit', compact('car'));
}
public function store(CarFormRequest $request)
{
$car = new Cars(array(
'name' => $request->get('name'),
'color_id' => $request->get('color')
));
$car->save();
$car->position_id = $car->id;
$car->save();
session()->flash('status', 'Successfully Added a Car!');
return view('cars.add');
}
public function update($id, CarFormRequest $request)
{
$car = car::whereId($id)->firstOrFail();
$car->name = $request->get('name');
$car->color_id = $request->get('color');
if($request->get('status') != null) {
$car->status = 0;
} else {
$car->status = 1;
}
$car->save();
return redirect(action('CarController#edit', $car->id))->with('status', 'The ticket '.$id.' has been updated!');
}
my routes:
Route::get('/', 'PagesController#home');
Route::get('/about', 'PagesController#about');
Route::get('/contact', 'PagesController#contact');
Route::get('/cars', 'CarsController#index');
Route::get('/cars/edit/{id?}', 'CarsController#edit');
Route::post('/cars/edit/{id?}', 'CarsController#update');
Route::get('/cars/add', 'CarsController#add');
Route::post('/cars/add', 'CarsController#store');
here is my view:
<div class="container col-md-8 col-md-offset-2">
<div class="well well bs-component">
<form class="form-horizontal" method="post">
<input type="hidden" name="_token" value="{!! csrf_token() !!}">
<input type="text" id="color_id" name="color_id" value="{!! $car->color_id !!}">
<fieldset>
<legend>Edit Car Information</legend>
<div class="form-group">
<label for="title" class="col-lg-2 control-label">Car Name</label>
<div class="col-lg-10">
<input type="text" value="{{ $car->name }}" class="form-control" id="name" placeholder="Car Name">
</div>
</div>
<div class="form-group">
<label for="title" class="col-lg-2 control-label">Car Color</label>
<div class="col-lg-10">
<div class="btn-group" data-toggle="buttons">
<label id="opt1" class="btn btn-primary">
<input type="radio" name="color" id="option1" autocomplete="off"> Red
</label>
<label id="opt2" class="btn btn-primary">
<input type="radio" name="color" id="option2" autocomplete="off"> Blue
</label>
<label id="opt3" class="btn btn-primary">
<input type="radio" name="color" id="option3" autocomplete="off"> Yellow
</label>
<label id="opt4" class="btn btn-primary">
<input type="radio" name="color" id="option4" autocomplete="off"> Green
</label>
<label id="opt5" class="btn btn-primary">
<input type="radio" name="color" id="option5" autocomplete="off"> Black
</label>
<label id="opt6" class="btn btn-primary">
<input type="radio" name="color" id="option6" autocomplete="off"> White
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<button class="btn btn-default">Cancel</button>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</fieldset>
</form>
</div>
</div>
The $id variable in whereIn must be array and you need to specify the database column too. This should be like -
public function edit($id)
{
$car = Cars::whereId('id', [$id])->firstOrFail();
return view('cars.edit', compact('car'));
}
Change all occurrence of
$car = car::whereId($id)->firstOrFail();

Resources