InvalidArgumentException on Laravel 5.7 - php

i'm an absolute noob to Laravel this is my first day trying to use it, i'm doing an internship and the mentor over there recommended me the usage of Laravel 5.7 to create a CRUD as an example and to get me familiar with the framework so i can progress through my final year project, he also referred me to a tutorial to follow with, so i followed the tutorial step by step (was really troublesome at first as it turned out there was some error with migrations but managed to get it fixed after an hour or two of depression) later i progressed with the tutorial to the point where i'm supposed to test my create view as it turns it out everytime i try it returns an Invalid Argument Exception on produits.create, i checked comments from the tutorial and some people had my problem but no one suggested a working solution, i looked allover stackoverflow but it seems people had different problems to mine so it yielded no results
InvalidArgumentException
View [produits.create] not found.
This is the error i get
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ShareController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('produits.create');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
This is the Controller class
#extends('layout')
#section('content')
<style>
.uper {
margin-top: 40px;
}
</style>
<div class="card uper">
<div class="card-header">
Ajouter Produit
</div>
<div class="card-body">
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div><br />
#endif
<form method="post" action="{{ route('produits.store') }}">
<div class="form-group">
#csrf
<label for="nom">Nom Produit:</label>
<input type="text" class="form-control" name="nom_produit"/>
</div>
<div class="form-group">
<label for="prix">Prix Produit :</label>
<input type="text" class="form-control" name="prix_produit"/>
</div>
<div class="form-group">
<label for="quantite">Quantité Produit:</label>
<input type="text" class="form-control" name="quantite_produit"/>
</div>
<button type="submit" class="btn btn-primary">Add</button>
</form>
</div>
</div>
#endsection
this is create.blade.php
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::resource('produits', 'ShareController');
This is web.php
Thanks in advance.

Related

The POST method is not supported for this route. Supported methods: GET, HEAD, PUT, PATCH, DELETE. in Laravel 8

I'm getting this error when I'm trying to register something from a form. I'm going to post the code and after that explain the situation
View Code
#extends('layouts.app')
#section('content')
<div class="container">
<h1>Crear Especialidad</h1>
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Register') }}</div>
<div class="card-body">
<form method="POST" action="{{ route('especialidades.create') }}">
#csrf
<div class="form-group row">
<label for="nombre" class="col-md-4 col-form-label text-md-right">{{ __('Nombre') }}</label>
<div class="col-md-6">
<input id="nombre" type="text" class="form-control #error('nombre') is-invalid #enderror" name="nombre" value="{{ old('nombre') }}" required autocomplete="nombre" autofocus>
#error('nombre')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('Registrar') }}
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
Controller code
public function create(array $data)
{
return Especialidades::create($data);
}
Routes
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::get('/nuevaEspecialidad', [EspecialidadesController::class,'nuevo'])->name('nuevaEspecialidad');
Route::resource('/especialidades', EspecialidadesController::class);
Route::get('/gestionarMedicos', [PersonaController::class,'mostrarMedicos'])->name('personaMostrarMedicos');
Dunno if it is a problem with overwriting or something similar but the resource route should support post for the create function. I'm trying to insert the data from the form, a name as you can see in the view to the database.
I'm new on Laravel so dunno what can be the cause. I will gladly provide you with extra code if needed or explain better the situation if it isn't explained enough.
In your case, it should be {{ route('especialidades.store') }}
The reason being that default method for creating a row in db in a resource controller is App\Http\Controllers\<YourResourceControllerName>#store
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
Here the create method is only for showing the form of the store method and not the function for storing the data itself.
You are using resource, where especialidades.create it will accept get method. Your submit method is post, so you need to change :
{{ route('especialidades.store') }}

Undefined variable when using error is coming

I have made a simple form when I am calling the form from api route it is showing an error that "errors" is an undefined variable when I am calling using web route it just works fine and shows no error. Why is this happening? Since error is a pre defined variable but why is it showing error.
Layout file:
#extends('layout')
#section('content')
<h1 class="title">Simple Form</h1>
<form method="POST" action="/website/atg/public/projects">
#csrf
<div class="field">
<label class="label" for="name">Name</label>
<div class="control">
<input type="text" class="input" name="name" placeholder="Enter Name" value="{{old('name')}}" required>
</div>
</div>
<div class="field">
<label class="label" for="email">E-mail</label>
<div class="control">
<input type="text" class="input" name="email" placeholder="Enter E-mail Address" value="{{old('email')}}" required>
</div>
</div>
<div class="field">
<label class="label" for="pincode">Pincode</label>
<div class="control">
<input type="text" class="input" name="pincode" placeholder="Enter Pincode" value="{{old('pincode')}}" required>
</div>
</div>
<div class="field">
<div class="control">
<button type="submit" class="button">Submit</button>
</div>
</div>
#if($errors->any())
<div class="notification">
<ul>
#foreach($errors->all() as $error)
<li>{{$error}}</li>
#endforeach
</ul>
</div>
#endif
</form>
#endsection
Routes file:
<?php
use Illuminate\Http\Request;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
/*Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
*/
Route::apiResource('projects','ATGController');
Controller file:
<?php
namespace App\Http\Controllers;
use App\Project;
use App\Mail\ProjectCreated;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;
class ATGController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
return view('projects.index');
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
request()->validate([
'name'=>'required|unique:projects,name',
'email'=>'required|email|unique:projects,email',
'pincode'=>'required|digits:6'
]);
if ($validator->fails()) {
return redirect('/projects')
->withErrors($validator)
->withInput();
}
else{
$project=Project::create(request(['name','email','pincode']));
\Mail::to('sbansal1809#gmail.com')->send(
new ProjectCreated($project)
);
//echo '<script>alert("User added sucessfully!")</script>';
return response()->json($project);
}
}
/**
* Display the specified resource.
*
* #param \App\Project $project
* #return \Illuminate\Http\Response
*/
public function show(Project $project)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param \App\Project $project
* #return \Illuminate\Http\Response
*/
public function edit(Project $project)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param \App\Project $project
* #return \Illuminate\Http\Response
*/
public function update(Request $request, Project $project)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param \App\Project $project
* #return \Illuminate\Http\Response
*/
public function destroy(Project $project)
{
//
}
}
you have to return validator fails like this, to be able to get $errors in your blade. Check below example for reference and change the code to your parameters
don't forget to import use Illuminate\Support\Facades\Validator;
public function store(Request $request){
$validator = Validator::make($request->all(), [
'name'=>'required|unique:projects,name',
'email'=>'required|email|unique:projects,email',
'pincode'=>'required|digits:6'
]);
if ($validator->fails()) {
return redirect('/projects/create')
->withErrors($validator)
->withInput();
}else{
$project=Project::create(request(['name','email','pincode']));
\Mail::to('sbansal1809#gmail.com')->send(
new ProjectCreated($project)
);
//echo '<script>alert("User added sucessfully!")</script>';
return response()->json($project);
}
}

When i am clicking submit button it shows "object not found" error

I have made a form to submit user data but when I am clicking submit button it shows an error "Object not found"
View file`
#extends('layout')
#section('content')
<h1 class="title">Simple Form</h1>
<form method="POST" action="/projects">
#csrf
<div class="field">
<label class="label" for="name">Name</label>
<div class="control">
<input type="text" class="input" name="name" placeholder="Enter Name">
</div>
</div>
<div class="field">
<label class="label" for="email">E-mail</label>
<div class="control">
<input type="text" class="input" name="email" placeholder="Enter E-mail Address">
</div>
</div>
<div class="field">
<label class="label" for="pincode">Pincode</label>
<div class="control">
<input type="text" class="input" name="pincode" placeholder="Enter Pincode">
</div>
</div>
<div class="field">
<div class="control">
<button type="submit" class="button">Submit</button>
</div>
</div>
</form>
#endsection
Controller file
<?php
namespace App\Http\Controllers;
use App\Project;
use Illuminate\Http\Request;
class ATGController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
return view('projects.index');
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('projects.create');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store()
{
return request()->all();
}
/**
* Display the specified resource.
*
* #param \App\Project $project
* #return \Illuminate\Http\Response
*/
public function show(Project $project)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param \App\Project $project
* #return \Illuminate\Http\Response
*/
public function edit(Project $project)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param \App\Project $project
* #return \Illuminate\Http\Response
*/
public function update(Request $request, Project $project)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param \App\Project $project
* #return \Illuminate\Http\Response
*/
public function destroy(Project $project)
{
//
}
}
Route file
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::get('/projects','ATGController#index');
Route::get('/projects/create','ATGController#create');
Route::post('/projects','ATGController#store');
Layout file
<!DOCTYPE html>
<html>
<head>
<title>ATG</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.5/css/bulma.min.css">
</head>
<body>
<div class="container">
#yield('content')
</div>
</body>
</html>
Object not found!
The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.
If you think this is a server error, please contact the webmaster.
Error 404
localhost
Apache/2.4.39 (Win64) OpenSSL/1.1.1c PHP/7.3.7
You need not to use a / before a url.
web.php:
Route::post('projects','ATGController#store');
view file:
<form method="POST" action="{{ url('projects') }}">
If this does not resolve the issue kindly check your app_url in your .env file.

Route issue:Class Controller does not exist in Laravel 5.8

I am creating a profile section.
And show.blade.php is the profile edit part.
But I cannot see show.blade.php.
I got a following error.
Here is my code.
web.php
Route::resource('channels', 'ChannelController');
php artisan route:list in terminal. And this is the result.
app.blade.php once I click here I can jump to show.blade.php
<a class="dropdown-item" href="{{ route('channels.show', auth()->user()->channel->id) }}"> My Channel</a>
controller.php
<?php
namespace Laratube\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}
ChannelController.php
<?php
namespace Laratube\Http\Controllers;
use Illuminate\Http\Request;
class ChannelController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show(Channel $channel)
{
return view('channels.show', compact('channel'));
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
show.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">
{{ $channel->name }}
</div>
<div class="card-body">
<form id="update-channel-form" action="{{ route('channels.update', $channel->id) }}" method="POST" enctype="multipart/form-data">
#csrf
#method('PATCh')
<div class="form-group">
<label for="name" class="form-control-label">
Name
</label>
<input id="name" name="name" value="{{ $channel->name }}" type="text" class="form-control">
</div>
<div class="form-group">
<label for="description" class="form-control-label">
Description
</label>
<textarea name="description" id="description" cols="3" rows="3" class="form-control">
{{ $channel->description }}
</textarea>
</div>
<button class="btn btn-info" type="submit">Update</button>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
I tried following things.
php artisan cache:clear
composer update
composer dump-autoload
php artisan db:seed
But still it doesn't work.
I am glad if someone helps me out.
I guess, I have something wrong with my route. This route also did not work.
Route::resource('channels', 'ChannelController')->name('channels.show');
My guess (without your controller code) is that you didn't import the model Channel. The message is quite clear, it tries to retrieve the model class from it's current directory: Laratube\Http\Controllers\.
You didn't import the Channel Model in your controller.
Add the line in the top use section of your controller.
use Laratube\Channel;
//In Your Controller
<?php
namespace Laratube\Http\Controllers;
use Laratube\Channel;
use Illuminate\Http\Request;
class ChannelController extends Controller
{
//in show function
public function show()
{
$channel = Channel::get();
return view('channels.show', compact('channel'));
}

Array to string conversion (SQL: insert into taches (libelle_tache, Tarif, metier_id,

I have two tables, a table "métier" and a table "tâche" with one to many connection, a "métier" has several "tâche". In my form "ajouter tâches" I have a combobox from which I choose the "métier" associated with the "tâche". When i validate the creation i got the error above. Thank you for helping me.
Create blade.php
#extends('Layouts/app')
#section('content')
#if(count($errors))
<div class="alert alert-danger" role="alert">
<ul>
#foreach($errors ->all() as $message)
<li>{{$message}}</li>
#endforeach
</ul>
</div>
#endif
<div class="container">
<div class="row"></div>
<div class="col-md-12">
<form action=" {{url ('tache') }}" method="post">
{{csrf_field()}}
<div class="form-group">
<label for="">Libelle Tache</label>
<input type="text" name ="libelle_tache" class="form-
control"value="{{old('libelle_tache')}}">
</div>
<div class="form-group">
<label for="">Tarif</label>
<input type="text" name ="Tarif" class="form-
control"value="{{old('tarif')}}">
</div>
<div class="form-group">
<label for="metier">metier</label>
<select name="metier_id" id="metier" class="form-
control">
#foreach($metiers as $metier)
<option value="{{$metier->id}}">
{{$metier->libelle_metier}}
</option>
#endforeach
</select>
</div>
<div class="form-group">
<input type="submit" value = "enregistrer"
class="form-control btn btn-primary">
</div>
</form>
</div>
</div>
<script>
#endsection
tachecontroller.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Tache;
use App\Metier;
class TacheController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$Listmetier=metier::orderBy('libelle_metier')->get();
$Listtache=tache::all();
return view('tache.index',['tache'=>$Listtache]);
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//$metiers = Costcenter::lists('libelle_metier', 'id');
$metiers = Metier::orderBy('id', 'desc')->get();
return view('tache.create')->with('metiers', $metiers);
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$tache = new Tache();
$tache ->libelle_tache =$request->input('libelle_tache');
$tache ->Tarif =$request->input('Tarif');
$tache ->metier_id =$request->input($tache->id);
$tache->save();
return redirect('tache');
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
$tache=Tache::find($id);
return view('tache.edit',['libelle_tache'=>$metier],
['Tarif'=>$tache]);
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
$tache =Tache::find($id);
$tache->delete();
return redirect('tache');
}
}
Model1
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class tache extends Model
{
public function metier()
{
return $this->belongsTo(Metier::class);
}
public function tarificationtache()
{
return $this->hasMany(Tarificationtache::class);
}
}
Model2
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class metier extends Model
{
public function metier()
{
return $this->hasMany(Tache::class);
}
}
screenshot of the form
Change:
$tache ->metier_id = $request->input($tache->id);
To:
$tache ->metier_id = $request->input('metier_id');

Categories