Use of undefined constant contacts - assumed 'contacts' error - php

Recently I have been working to insert data into a database with laravel. Everything went fine. But know I am stuck with the error: "Use of undefined constant contacts - assumed 'contacts'". I do not know how to fix this.
I have already on the Laracast website to see if I could find an explanation on how to solve this, but I couldn't since the answers were not the once I was looking for. I have also looked in StackOverflow. But there were (again) no answers that could help me fix this the problem. I also have asked a classmate of mine but he also couldn't fix it.
My controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\contacts;
class ContactsController extends Controller
{
/*Haal alle data op uit de contact tabel return een view file*/
public function index($contact = contacts){
$Contact = contacts::all();
return view('contact',compact('contacts'));
}
/*Return een view file*/
public function create(){
return view('contacts');
}
public function storeContact(){
/*Maakt nieuwe variabele, wanneer er op de knop wordt geklikt worden er 2 waardes opgehaald.*/
$contacts = new contacts();
$contacts->name = request('voornaam');
$contacts->description = request('bericht');
$contacts->save();
return redirect('/contacts');
}
}
My view:
#extends ('Layout')
#section('title')
Contact
#endsection
#section('content')
<br>
<h2>Contact</h2>
<p>Voor verdere informatie (die u niet op onze website kunt vinden) kunt u ons bereiken op onze email of telefoonnummer:</p>
<p>Email: Cronensteynfake#gmail.com</p>
<p>telefoonnummer: 06-11122234</p>
<br>
U kunt ook ons contactformulier invullen.
<br><br>
<h2>Cronenstyn Contactformulier</h2>
<form action="" method="post">
#csrf
<div class="form-group">
<label for="exampleInputPassword1">Voornaam</label>
<input type="text" class="form-control" id="Vnaam" placeholder="Voornaam">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Achternaam</label>
<input type="text" class="form-control" id="Anaam" placeholder="Achternaam">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Email</label>
<input type="email" class="form-control" id="Email" aria-describedby="emailHelp" placeholder="Uw email">
<small id="emailHelp" class="form-text text-muted">Wij delen uw email met niemand</small>
</div>
<label for="exampleInputEmail1">Bericht</label>
<div class="form-group shadow-textarea">
<textarea class="form-control z-depth-1" id="bericht" rows="3" placeholder="Uw bericht"></textarea>
</div>
<button onclick="Message()" type="submit" class="btn btn-primary">Verzenden</button>
<script>
function Message() {
alert("Uw bericht is met succes verstuurd.");
}
</script>
</form>
#endsection
My route:
<?php
Route::get('/', function () {
return view('welcome');
});
Route::GET('/home',('CronensteynController#home'));
Route::GET('/nieuws',('CronensteynController#nieuws'));
Route::GET('/galerij',('CronensteynController#gallerij'));
Route::GET('/activiteit',('CronensteynController#activiteit'));
Route::GET('/contact',('CronensteynController#contact'));
Route::GET('/about',('CronensteynController#about'));
Route::get('/beheer', 'PageController#beheer');
Route::get('/create_beheer', 'PageController#Create_beheer');
Route::get('/update_beheer', 'PageController#Update_beheer');
Route::get('/delete_beheer', 'PageController#delete_beheer');
Route::post('/create_beheer', 'CreateBeheerController#store');
Route::post('/contact', 'contactsController#index');
Hopefully I have given you enough info and code about the problem.
Greetings,
Parsa_237

Follow a convention to avoid errors regarding cases.
Generally models start with a capital letter.
Always make your model name singular. For model Contact laravel will look the table contacts.
For more information on naming convention you can follow this link
Here you are passing $contact to index method you don't have to do that.
public function index(){
$Contacts = contacts::all();
return view('contact',compact('Contacts'));
}

Related

Sending parameters in LARAVEL 5.8

I need to send parameters to my "show" function in Laravel 5.8 via a select HTML tag but I am not able. I've been there for several days and I'm a little desperate.
In the action of the Form I put the following:
This is the route: {{route('users.show', ['id' => {{$list->id}}])}} but since the variable does not exist, it gives me error. Another option I considered is to use a link <a href='#'></a> where '#' is the route. But $list->id returns the last ID in the List in the Users table.
How can I capture the selected data in my drop-down list and send it as a parameter to the controller's show function?
Route::group(['middleware' => ['verified']], function () {
// Panel de control
Route::get('/home', 'HomeController#index')->name('home');
Route::resource('users', 'UserController');
This is the Controller:
public function index()
{
# Panel de control
$log = Auth::id();
$users = User::orderBy('created_at', 'asc')->where('id',$log)->with('imagenes')->get()->toArray();
$users=Array_chunk($users,3,true);
$imgBorrado = array();
$imgB=null;
//dd($imgBorrado);
$user=User::find('id');
$userslist=User::all();
//$list=User::orderBy('created_at', 'asc')->get('id','name')->toArray();
//dd($userslist);
return view('users.index', compact('user','users','imgBorrado','imgB','userslist'));
}
public function show($id)
{
# Mostrar usuario con sus atributos
$user=User::find($id);
dd($id);
return view('users.show', compact('user'));;
}
This is the view:
<form method="POST" action="#" id="selector" enctype="multipart/form-data">
#csrf
<label for="desplegable" class="col-md-4 col-form-label text-md-right">{{ __('Productor:') }}</label>
<div class="col-md-6">
<select class="form-control{{ $errors->has('id') ? ' is-invalid' : '' }}" id="desplegable" name='workers[]' required>
<option selected>...</option>
#foreach($userslist as $list)
<option value="{{$list->id}}">
{{$list->name}}
</option>
#endforeach
</select>
#if ($errors->has('id'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('id') }}</strong>
</span>
#endif
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cerrar</button>
<button type="submit" class="btn btn-primary" form="selector" Value="desplegable">Seleccionar Perfil</button>
</div>
Thanks you!
You cannot have access to the ID of the selected user in your <form> tag.
One option would be to catch the request in your show() method and then fetch the desired user through it.
Here is an example based on your code:
// In your Controller (don't forget to import Request facade)
use Illuminate\Support\Facades\Request
public function show(Request $request)
{
$userId = $request->get('workers'); // Name of the select input
$user = User::find($userId);
return view('users.show', compact('user'));
}
You can even validate the Request passed to the show() method. Read more about that here
I add the modification here:
public function show(Request $request)
{
# Nombre del select (input)
$userId = $request->get('workers');
# Mostrar usuario con sus atributos
$usu=User::find($userId);
foreach($usu as $user) {
$user->name;
}
return view('users.show', compact('user'));;
}

Symfony\Component\Routing\Exception\RouteNotFoundException: Route [Lala.search] not defined

I created a model and its migration like this:
php artisan make:model Lala -m
And I made this : php artisan migrate
I was going to call this road, but I have a mistake. Did I write it wrong? How can I call the search method when my form is submitted?
formular:
<?php
use App\Models\Lala;
?>
<form action="{{ route('Lala.search')}}" method="GET" >
<div class="input-group mb-3">
<input type="text"
name="name" class="form-control" placeholder="Geben Sie etwas an"
aria-label="Geben Sie etwas an"
aria-describedby="basic-addon2" autocomplete="off">
<div class="input-group-append">
<span class="input-group-text" id="basic-addon2">🎓</span>
</div>
</div>
<input type="submit" class="btn btn-primary" value="search">
</form>
I defined the route as follows in web.php :
use App\Models\Lala;
Route::get('/search',[
'as' =>'Lala.search',
'uses' =>'\App\Http\Controllers\stipendiensController#search']);
stipendiensController is defined like this:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Stipendien;
class stipendientsController extends Controller
{
public $name;
public function search()
{
return view('seite.Stipendien');
}
}
how to avoid this error? could I write this code differently? I try indeed to enter the data in my search bar and I compare in my database if the value entered in the search bar is there.
Thank you for helping me . Please
Try defineing your route like so:
use App\Http\Controllers\stipendiensController;
Route::get('/search', [stipendiensController::class, 'search'])
->name('Lala.search');

inserting user in database from regitration form laravel 8

i am working on a e-commerce website using laravel 8 for the first time i created a login and registration views, when i try registering first it flashed iccorrect email adresse in session error that is displayed in register.blade.php after fixing that it redirected me to an error page with this text :
The GET method is not supported for this route. Supported methods: POST.
http://localhost:3000/create?_token=7vVwL33yaBmkmsJIKHl78XfzZNmk4vJDBG9qumhf&email=theoghmir.kodia%40laposte.net&name=HELLh&password=2223541
my route is acctually set to POST did i forget some thing ? here is my web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\authController;
Route::get('/', function () {
return view('welcome');
});
Route::get('/auth', [authController::class, 'authpage']);
Route::get('/auth/check', [authController::class, 'check'])->name('auth.check');
Route::get('/register', [authController::class, 'register']);
Route::post('/create', [authController::class, 'create'])->name('auth.create');
Route::get('/logout', [authController::class, 'logout']);
here the register.blade.php
<form action="{{ route('auth.create')}}" methode="POST">
#if(Session::get('fail'))
<div class="alert alert-danger">{{Session::get('fail')}}</div>
#endif
#if(Session::get('success'))
<div class="alert alert-success">{{Session::get('success')}}</div>
#endif
#csrf
<label>name :</label>
<input type="text" class="form-control" name="name" placeholder=" name..." >
<label>Email :</label>
<input type="text" class="form-control" name="email" placeholder=" email..." value="{{old('email')}}">
<span class="text-danger">
#error ('email') {{$message}} #enderror</span><br>
<label>Password :</label>
<input type="password" class="form-control" name="password" placeholder="Mot de passe...">
<span class="text-danger">
#error ('password') {{$message}} #enderror</span><br>
<button type="submit">S'inscrir</button>
</form>
here is the auth controller with the methode create() :
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
use App\Http\Controllers\hash;
class authController extends Controller
{
public function authpage()
{
return view('login');
}
public function register()
{
return view('register');
}
public function create(Request $request)
{
$request->validate([
'email'=>'required|email|unique:users',
'password'=>'required|min:5|max:12',
]);
$user = new User ;
$user -> name = $request->name;
$user -> email = $request->email;
$user -> password = $request->password;
$query = $user->save();
if($query) {
return back()->with('success',' vous êtes inscris avec succées ! ' );
}else{
return back()->with('fail',' il y as quelque chose qui cloche ! ' );
}
}
You have an syntax error, it is not methode= but method="POST"
<form action="{{ route('auth.create')}}" method="POST">

Not being able to select the same name into a form

So I have this form where i can fill in companies but right now I am being able to insert multiple companies with the same name but I don't want that.. I want the name of the company to be unique or else if I put in the same name thats already in the database it should give me an error.
How can I do that?
This is my form in my view html:
<br>
<center> <h3> Instituut toevoegen </h3> </center>
<br>
<?php echo form_open('index.php/Instituut/Add_instituut'); ?>
<div class="form-group">
<label>Instituut:</label>
<input class="form-control" name="instituut" id="instituut" type="text">
</div>
<div class="form-group">
<label>Telefoonnummer van instituut:</label>
<input class="form-control" name="instituuttelefoon" id="instituuttelefoon" type="text">
</div>
<div>
<button class="btn btn-primary" name="Add_instituut" >Toevoegen</button>
</div>
</form>
</div>
This is the controller function:
public function Add_instituut()
{
if (isset($_POST['Add_instituut'])) {
$this->form_validation->set_rules('instituut', 'Instituut', 'required');
$this->form_validation->set_rules('instituuttelefoon', 'instituuttelefoon', 'required');
//If form validation true
if ($this->form_validation->run() == TRUE) {
//voeg werknemer toe in database
$data = array (
'instituut'=>$_POST['instituut'],
'instituuttelefoon'=>$_POST['instituuttelefoon'],
);
$this->db->insert('instituut',$data);
$this->session->set_flashdata("success", "u heeft een nieuw instituut toegevoegd");
redirect("index.php/Instituut", "refresh");
}
}
}
I probably should add something in the form validation rules?
instituut means company name
Thanks
any kind of help is appreciated
You can add is_unique in form validation rules
$this->form_validation->set_rules('instituut', 'Instituut', 'required|is_unique[your_table_name.instituut]');
check tutorial. for unique value:
https://www.codeigniter.com/userguide3/libraries/form_validation.html#rule-reference

Laravel redirect route with id variable won't work

I have made an update function to my laravel 4.2 website which work. It updates the table in the database with the right infomation but when i want to redirect to the same view as i came from i get a problem.
the url shows:
http://localhost:8000/admin/page/%7BpageID%7D
when it schould say:
http://localhost:8000/admin/page/1
Here is a link to an image showing the error in my browser:
http://i.imgur.com/ggO5ELg.png
this is from my view ( admin.pages )
#section('content')
<div class='box box-info'>
<form method="post" action="/admin/page/{{$page->id}}">
<div class='box-header'>
<div class='box-body pad'>
<input type="text" name="title" value="{{$page->title}}" class="form-control">
</div>
</div>
<div class='box-body pad'>
<textarea id="editor1" name="text" rows="2" cols="80">
{{ $page->text }}
</textarea>
<br>
<input type="submit" class="btn btn-primary" value="Opdater">
</div>
</form>
</div>
#stop
This is from my controller ( PageController ) where i have the redirect in the update function.
<?php
class PageController extends BaseController {
public function index($pageID)
{
$data['pageID'] = $pageID;
$page = Page::find($pageID);
return View::make('admin.pages', $data)
->with(compact('page'));
}
public function update($pageID)
{
DB::table('pages')
->where('id', $pageID)
->update(array(
'title' => Input::get('title'),
'text' => Input::get('text')));
return Redirect::route('page')->with('succes', 'Du har sendt en besked');
}
}
This is the two routes which i use in this case
Route::get('/admin/page/{pageID}', array('as'=>'page', 'uses'=>'PageController#index'));
Route::post('/admin/page/{pageID}', array('as'=>'page', 'uses'=>'PageController#update'));
If you need anymore information just let me know.
The page route requires a parameter named {pageID}, and you need to pass that when redirecting:
return Redirect::route('page', ['pageID' => $pageID])->with('succes', 'Du har sendt en besked');
You can read more about this in the Laravel Redirects Documentation.

Categories