ReflectionException (-1) Class App\Http\Controllers\AnswersController does not exist - laravel

I want to save data in a database using the form
I tried to use a form with input text, radios ... and controller to save data in a database with post method
Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Auth;
use App\Survey;
use App\Answer;
use App\Http\Requests;
class Answerscontroller extends Controller
{
public function store(Request $request, Survey $survey)
{
$request->validate([
'answer'=>'required'
]);
$answers = new Answer([
'answer' => $request->get('answer'),
'commentaire' => $request->get('commentaire'),
'user_id' => auth()->id(),
'last_ip' => request()->ip(),
'survey_id' => $survey->id
]);
$answers->save();
return redirect('/survey')->with('success', 'Stock has been added');
}
}
View:
{!! Form::open(array('action'=>array('AnswersController#store', $survey->id))) !!}
#forelse ($survey->questions as $key=>$question)
<p class="flow-text">Question {{ $key+1 }} - {{ $question->title }}</p>
#if($question->question_type === 'text')
<div class="form-group">
<div class="input-field col s12">
<input id="answer" type="text" name="{{ $question->id }}[answer]">
<label for="answer">Answer</label>
</div>
</div>
#elseif($question->question_type === 'textarea')
<div class="form-group">
<div class="input-field col s12">
<textarea id="textarea1" class="materialize-textarea" name="{{ $question->id }}[answer]"></textarea>
<label for="textarea1">Textarea</label>
</div>
</div>
#elseif($question->question_type === 'radio')
#foreach($question->option_name as $key=>$value)
<p style="margin:0px; padding:0px;">
#if($value === 'else')
<div class="form-group" style="margin-left: 20px;">
<input name="answer" class="custom-control-input" type="radio" id="{{ $value }}" value="{{$value}}"/>
<label class="custom-control-label" for="{{ $value }}">{{ $value }}</label>
<div id="textboxes" style="display: none">
<br>
<textarea class="form-control" name="commentaire" id="exampleFormControlTextarea1" rows="3" placeholder="Write a large text here ..."></textarea>
</div>
</div>
#else
<p style="margin:0px; padding:0px;">
<div class="form-group" style="margin-left: 20px;">
<input name="answer" class="custom-control-input" type="radio" id="{{ $value }}" value="{{ $value}}"/>
<label class="custom-control-label" for="{{ $value }}">{{ $value }}</label>
</div>
</p>
#endif
#endforeach
#elseif($question->question_type === 'checkbox')
#foreach($question->option_name as $key=>$value)
<p style="margin:0px; padding:0px;">
<div class="form-group">
<input type="checkbox" id="{{ $value }}" name="answer" value="{{$value}}"/>
<label for="{{$value}}">{{ $value }}</label>
</div>
</p>
#endforeach
#endif
<div class="divider" style="margin:10px 10px;"></div>
#empty
<span class='flow-text center-align'>Nothing to show</span>
#endempty
<div class="form-group">
{{ Form::submit('Submit Survey', array('class'=>'btn btn-success mt-4')) }}
</div>
{!! Form::close() !!}
model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Answer extends Model
{
protected $fillable = ['answer','commentaire','user_id','survey_id','last_ip'];
protected $table = 'Answer';
public function survey() {
return $this->belongsTo(\App\Survey::class);
}
public function question() {
return $this->belongsTo(\App\Question::class);
}
public function user() {
return $this->belongsTo('App\User');
}
}
Error:
ReflectionException (-1) Class App\Http\Controllers\AnswersController
does not exist
Please could you help me to fix that
ps: in the router, I put post method and controller

The problem is about naming. Your controller is Answerscontroller but the Laravel Looks fo AnswersController with capital C. So, check your controller name that should be AnswersController.php and the class name (inside the file AnswersController.php) that sould be AnswersController.

Related

Laravel Livewire uploads file only after second submit / button press

I want to create an almost simple file upload via Laravel/Livewire, but once submitting the form with two inputs and one file input, the form fields focus and standstill. However, after pressing the submit button again, it uploads as anticipated with a success message. How can this be achieved with the first button submit?
Livewire component
namespace App\Http\Livewire\Files;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Livewire\Component;
use Livewire\WithFileUploads;
use App\Models\File;
class FilesForm extends Component
{
use WithFileUploads;
public $file;
public $name;
public $doctype;
public $doctypes;
public $doctypeparent;
public $docs;
public function mount($id)
{
$this->doctypeparent = $id;
}
public function save()
{
$validatedData = $this->validate([
'doctype' => 'required',
'file' => 'required|image|mimes:jpg,jpeg,png,svg,gif|max:10240',
]);
$filename = $this->file->store('files','public');
$validatedData['content_type_id'] = $this->doctype;
$validatedData['name'] = $this->name;
$validatedData['file'] = $filename;
$validatedData['user_id'] = Auth::id();
$validatedData['slug'] = uniqid();
File::create($validatedData);
session()->flash('message', 'Datei erfolgreich gespeichert.');
$this->doctype="";
$this->name="";
$this->file="";
$this->render();
//return redirect()->to('/fileupload');
}
public function render()
{
$this->doctypes = DB::table('content_type')
->select('content_type.id', 'content_type.strTitleDe')
->where('content_type.fkintContentGroup', '=', $this->doctypeparent)
->orderBy('content_type.intPriority')
->get();
$this->docs = File::all();
//dd($this->docs);
return view('livewire.files.files-form');
}
}
Livewire blade
<div class="file-form">
<form wire:submit.prevent="save">
#if (session()->has('message'))
<div class="alert alert-success">
{{ session('message') }}
</div>
#endif
<div class="form-group">
<label for="name">Typ</label>
<select class="form-control" id="doctype" name="doctype" wire:model="doctype">
<option value="">- - -</option>
#foreach ($doctypes as $doctype)
<option value="{{ $doctype->id }}">{{ $doctype->strTitleDe }}</option>
#endforeach
</select>
#error('doctype') <span class="error">{{ $message }}</span> #enderror
</div>
<div class="form-group">
<label for="name">Bezeichnung</label>
<input type="text" class="form-control" max="255" id="name" placeholder="" wire:model="name">
#error('name') <span class="text-danger">{{ $message }}</span> #enderror
</div>
<div class="form-group">
<div class="custom-file">
<label for="file">Datei:</label>
<input type="file" class="form-control" id="file" wire:model="file">
#error('name') <span class="error">{{ $message }}</span> #enderror
</div>
</div>
<button type="submit" class="btn btn-primary">Speichern</button>
</form>
<p></p>
<h3>Dokumente</h3>
<table>
<thead>
<tr>
<td>Typ</td>
<td>Title</td>
</tr>
</thead>
<tbody>
#foreach($docs as $doc)
<tr>
<td>Typ</td>
<td>{{ $doc->name}}</td>
</tr>
#endforeach
</tbody>
</table>
</div>
Not sure if this helps but.....While it looks like this could work, I never do actual form submits when using livewire - all of the data is bound with wire:model. There's no need to use a form at all. I just use buttons with wire:click and call whatever function saves the file.
Doing it that way I've had zero problems with file uploads.

How to insert this array of mySbjects in a database?

I've been trying to insert this into the database and I don't have any idea how to store this information into the database... The relationship between user and grade is many-to-many and between grade and mySubject is One-to-many.
<div class="row">
#foreach($grades as $grade)
<div class="card shadow mx-2 my-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold">
<div class="custom-control custom-checkbox">
<input class="custom-control-input #error('grade') is-invalid #enderror" type="checkbox" id="{{$grade->id}}" name="grade[]" value="{{ $grade->id }}"/>
<label class="custom-control-label pt-1" for="{{$grade->id}}">{{$grade->name}}</label>
</div>
</h6>
</div>
<div class="card-body">
#foreach($grade->subjects as $subject)
<div class="custom-control custom-checkbox mb-2">
<input class="custom-control-input" type="checkbox" id="{{$grade->id}}{{$subject->id}}" name="mySubjects[{{$grade->id}}][]" value="{{ $subject->name }}"/>
<label class="custom-control-label" for="{{$grade->id}}{{$subject->id}}">
{{$subject->name}}
</label>
</div>
#endforeach
</div>
</div>
#endforeach
</div>
The store function
public function store(Request $request)
{
$user->grades()->sync($request->grade);
if($request->mySubjects){
//. . . .
}
}
}
Try below code:
public function store(Request $request)
{
$user->grades()->sync($request->grade);
if($request->mySubjects){
$mySubjects = $request-> mySubjects;
$data = [];
foreach($mySubjects as $subject){
array_push($data,
'your database field name'=> $subject,
'created_at' => now()->toDateTimeString(),
'updated_at' => now()->toDateTimeString(),
}
YourModelName::insert($data);
}
}
}

Laravel save related data in database

I want to save question_id in answer table using post form
I tried to use foreach or functions but i always got null data
Controller
public function store(Request $request, Survey $survey)
{
$request->validate([
'answer' => 'required',
]);
$survey->option_name = unserialize($survey->option_name);
$answers = new Answer([
'answer' => $request->get('answer'),
'commentaire' => $request->get('commentaire'),
'user_id' => auth()->id(),
'last_ip' => request()->ip(),
'question_id' => $survey->questions,
'survey_id' => $survey->id,
]);
$answers->save();
return redirect()->action('SurveyController#view_survey_answers', [$survey->id]);
}
answers table
question table's row :
id
survey_id
title
timestamp
I got always null data or i tried using where but i got errors like id index doesn't exists...
View :
{!! Form::open(array('action'=>array('AnswerController#store', $survey->id))) !!}
#forelse ($survey->questions as $key=>$question)
<p class="flow-text">Question {{ $key+1 }} - {{ $question->title }}</p>
#if($question->question_type === 'text')
<div class="form-group">
<div class="input-field col s12">
<input id="answer" type="text" name="answer">
<label for="answer">Answer</label>
</div>
</div>
#elseif($question->question_type === 'textarea')
<div class="form-group">
<div class="input-field col s12">
<textarea id="textarea1" class="materialize-textarea" name="{{ $question->id }}[answer]"></textarea>
<label for="textarea1">Textarea</label>
</div>
</div>
#elseif($question->question_type === 'radio')
#foreach($question->option_name as $key=>$value)
<p style="margin:0px; padding:0px;">
#if($value === 'else')
<div class="form-group" style="margin-left: 20px;">
<input name="answer" class="custom-control-input" type="radio" id="{{ $value }}" value="{{$value}}"/>
<label class="custom-control-label" for="{{ $value }}">{{ $value }}</label>
<div id="textboxes" style="display: none">
<br>
<textarea class="form-control" name="commentaire" id="exampleFormControlTextarea1" rows="3" placeholder="Write a large text here ..."></textarea>
</div>
</div>
#else
<p style="margin:0px; padding:0px;">
<div class="form-group" style="margin-left: 20px;">
<input name="answer" class="custom-control-input" type="radio" id="{{ $value }}" value="{{ $value}}"/>
<label class="custom-control-label" for="{{ $value }}">{{ $value }}</label>
</div>
</p>
#endif
#endforeach
#elseif($question->question_type === 'checkbox')
#foreach($question->option_name as $key=>$value)
<p style="margin:0px; padding:0px;">
<div class="form-group">
<input type="checkbox" id="{{ $value }}" name="answer" value="{{$value}}"/>
<label for="{{$value}}">{{ $value }}</label>
</div>
</p>
#endforeach
#endif
<div class="divider" style="margin:10px 10px;"></div>
#empty
<span class='flow-text center-align'>Nothing to show</span>
#endempty
<div class="form-group">
{{ Form::submit('Submit Survey', array('class'=>'btn btn-success mt-4')) }}
</div>
{!! Form::close() !!}

Add [title] to fillable property to allow mass assignment on [App\Profile]

I am trying to create edit profile, but when I click on edit profile button I'm getting below error:
Illuminate \ Database \ Eloquent \ MassAssignmentException Add [title]
to fillable property to allow mass assignment on [App\Profile]
show.blade.php :
<#extends('layouts.app')
#section('content')
<div class="container">
<div class="row">
<div class="col-4">
<img src="https://scontent-cdt1-1.cdninstagram.com/vp/dcca3b442819fc8b9b63f09b2ebde320/5DA9E3CB/t51.2885-19/s150x150/40101184_290824334847414_1758201800999043072_n.jpg?_nc_ht=scontent-cdt1-1.cdninstagram.com" class="rounded-circle">
</div>
<div class="col-8">
<div class="d-flex align-items-baseline">
<div class="h4 mr-3 pt-2">{{ $user->username }}</div>
<button class="btn btn-primary">S'abonner</button>
</div>
<div class="d-flex">
<div class="mr-3">{{ $user->posts->count() }} article(s) en vente
</div>
Modifier Profile
<div class="mt-3">
<div class="font-weight-bold">
{{ $user->profile->title }}
</div>
<div class="font-weight-bold">
{{ $user->profile->description }}
</div>
</div>
</div>
</div>
<div class="row mt-5">
#foreach ($user->posts as $post)
<div class="col-4">
<img src="{{ asset('storage') . '/' . $post->image }}" class="w-100">
</div>
#endforeach
</div>
</div>
#endsection
ProfileController :
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
class ProfileController extends Controller
{
public function show(User $user)
{
return view('profile.show', compact('user'));
}
public function edit(User $user)
{
return view('profile.edit', compact('user'));
}
public function update(User $user)
{
$data = request()->validate([
'title' => 'required',
'description' => 'required'
]);
$user->profile->update($data);
return redirect()->route('profile.show', ['user' => $user]);
}
}
edit.blade.php :
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Modifier profile</div>
<div class="card-body">
<form method="POST" action="{{ route('profile.update', ['user' => $user]) }}" enctype="multipart/form-data">
#csrf
#method('PATCH')
<div class="form-group">
<label for="title">Titre</label>
<div class="col-md-6">
<input id="title" type="text" class="form-control #error('title') is-invalid #enderror" name="title" value="{{ old('title') ?? $user->profile->title }}" autocomplete="title" autofocus>
#error('title')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group">
<label for="description">Description</label>
<div class="col-md-6">
<textarea id="description" type="text" class="form-control #error('description') is-invalid #enderror" name="description" autocomplete="description" autofocus>{{ old('description') ?? $user->profile->description }}</textarea>
#error('description')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group">
<div class="custom-file">
<input type="file" name="image" class="custom-file-input #error('image') is-invalid #enderror" id="validatedCustomFile" >
<label class="custom-file-label" for="validatedCustomFile">Choisir une image</label>
#error('image')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
Modifier profile
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
Profile.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Profile extends Model
{
protected $guarder = [];
public function user()
{
return $this->belongsTo('App\User');
}
}
What am I doing wrong here and how can I get rid of this error?
You have a spelling error, instead of $guarder add this in your model:
protected $guarded = [];
I won't advise using empty guarded but use $fillable instead.
In the same controller model, free access to database fields with
protected $guarded = [];
or
protected $fillable = [];

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'pk5.roles' doesn't exist

I am able to insert records to employee_roles through tinker but unable to insert them through code
Role.php
<?php
namespace App\Models\Admin\Employee;
use Illuminate\Database\Eloquent\Model;
class Role extends Model
{
protected $table = 'employee_roles';
public function permissions()
{
return $this->belongsToMany('App\Models\Admin\Employee\Permission', 'employee_permission_role', 'role_id', 'permission_id');
}
}
RoleController.php
<?php
namespace App\Http\Controllers\Admin\Employee;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Employee\Employee;
use App\Models\Admin\Employee\Role;
use App\Models\Admin\Employee\Permission;
class RoleController extends Controller
{
public function __construct()
{
$this->middleware('auth:admin');
}
public function index()
{
$roles = Role::all();
return view('admins.employees.roles.role', compact('roles'));
}
public function create()
{
$permissions = Permission::all();
return view('admins.employees.roles.create',compact('permissions'));
}
public function store(Request $request)
{
//dd($request);
$this->validate($request, [
'name' => 'required|unique:roles'
]);
$role = new Role();
$role->name = $request['name'];
$role->save();
$role->permissions()->sync($request->permission);
return redirect(route('admin.employee.role.index'));
}
admins.employees.roles.create.blade.php
#extends('layouts.admin')
#section('title', 'Role - Create')
#section('left-menu')
#endsection
#section('right-menu')
#endsection
#section('content')
<h1>Add an Employee Role</h1>
<br><br>
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<form action="{{ route('admin.employee.role.store') }}" method="post">
#csrf
<div class="form-group">
<label for="name">Role Name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Role Name" value="{{ old('name') }}">
</div>
<div class="form-row">
<div class="col-md-4">
<label for="name"><b>Customer Permissions</b></label>
#foreach ($permissions as $permission)
#if ($permission->for == 'Customer')
<div class="checkbox">
<label><input type="checkbox" name="permission[]" value="{{ $permission->id }}">{{ $permission->name }}</label>
</div>
#endif
#endforeach
</div>
<div class="col-md-4">
<label for="name"><b>Despatch Permissions</b></label>
#foreach ($permissions as $permission)
#if ($permission->for == 'Despatch')
<div class="checkbox">
<label><input type="checkbox" name="permission[]" value="{{ $permission->id }}">{{ $permission->name }}</label>
</div>
#endif
#endforeach
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
Back
</div>
</form>
#endsection
#section('pagescript')
#stop
$this->validate($request, [
'name' => 'required|unique:roles'
]);
You should fix table name as employee_roles
Because you don't have table as roles you said that in your Role model you're using employee_roles table ( protected $table = 'employee_roles';)

Resources