SQLSTATE[22P02]: Invalid text representation error - php

Can't figure out why I am getting this error? I am using posgres I feel like I am missing some important link to figure this whole thing out ?
Basically I need to link the comment to the blog post with the id
SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input
syntax for integer: "{blog}" (SQL: select * from "blogs" where "id" =
{blog} limit 1)
Comments controller
use App\Blog;
use App\Comment;
class CommentsController extends Controller
{
// add store method
public function store(Blog $blog)
{
Comment::create([
'body'=> request('body'),
'blog_id' => $blog->id
]);
return back();
}
}
web.php
Route::post('blog/{blog}/comments', 'CommentsController#store');
form head with request:
<form method="POST" action="/blog/{blog}/comments">

As you have it now, action="/blog/{blog}/comments"> will return just /blog/{blog}/comments as action, since {blog} is not parsed by Blade.
Give your route a name:
Route::post('blog/{blog}/comments', 'CommentsController#store')->name('blogcomments');
In your <form> set the action to the named route (blogcomments), giving it the current $blog as parameter:
<form method="POST" action="{{ route('blogcomments', ['blog' => $blog] }}">
Read more on named routes and parameters at https://laravel.com/docs/5.7/routing#named-routes
Not tested! ;)

Related

Laravel contact form is not working correctly

I have a problem with the Laravel site's form, when I send the form through the site it shows an error, using devTools it is noted that it is an error 500 of the Post method, but the form is sent anyway.
the following error appears in the laravel log file
[2021-12-15 11:27:49] production.ERROR: Missing required parameters for [Route: contato] [URI: {lang}/contato]. {"exception":"[object] (Illuminate\Routing\Exceptions\UrlGenerationException(code: 0): Missing required parameters for [Route: contato] [URI: {lang}/contato]. at /home/corstonecom/public_html/vendor/laravel/framework/src/Illuminate/Routing/Exceptions/UrlGenerationException.php:17)
In the form view it like this:
<form id="frm-contato" class="site-form" action="{{ route('contato-enviar', app()->getLocale()) }}" method="post">
In the routes file web.php it like this:
Route::view('/contato', 'fale-conosco')->name('contato');
Route::post('/contato', 'HomeController#enviarContato')->name('contato-enviar');
and in the controller it like this:
public function enviarContato(EnviaContatoRequest $request)
{
$inputs = $request->all();
$inputs['localidade'] = $inputs['cidade'] . '/' . $inputs['uf'];
$contato = Contatos::create($inputs);
Lead::fastSave([
'name' => $inputs['nome'],
'email' => $inputs['email'],
]);
Mail::send(new FaleConosco($contato));
Session::flash('contato_enviado', 'sucesso');
return redirect()->route('contato');
}
Where am i going wrong?
You would need to pass the lang parameter when calling the route method when doing the redirect in your Controller method as the route contato requires the lang parameter:
return redirect()->route('contato', ['lang' => app()->getLocale()]);
It is best to get in the habit of using an associative array for the parameters as any more than 1 parameter and they will need to be in an associative array for the URL generator to match them up.
If you don't want to have to constantly be passing this app()->getLocale() value to the URL helpers you can set a default for the lang parameter and it will be added for you. You could use a middleware to do this. Example of the functionality of setting a default value for a parameter:
public function handle($request, $next)
{
\URL::defaults([
'lang' => app()->getLocale(),
]);
return $next($request);
}
Now you don't have to pass the parameter for lang when generating URLs as it has a default value set.
If you already have a "locale" middleware that is handling the locale you can add this to it.
When you pass parameters to the route, you should pass it as an array with keys, so that Laravel knows what the parameter is. In this case, you would do
<form id="frm-contato" class="site-form" action="{{ route('contato-enviar', ['lang' => app()->getLocale()]) }}" method="post">
See https://laravel.com/docs/8.x/routing#generating-urls-to-named-routes

Laravel - Missing required parameters for Route

I have a problem with my routes in Laravel 7 not sure where I a, going wrong here.
I have this route declaration:
Route::group(['prefix'=>'config', 'namespace'=>'Config'], static function () {
Route::resource('id-generation', 'IDSettingsController', ['names'=>'config.id_generation'])->only(['index', 'edit', 'update']);
});
Then a controller:
public function edit(IdSetting $setting)
{
return view('config.id.edit')->with(['setting'=>$setting]);
}
Then a view:
<form method="post" action="{{route('config.id_generation.update', ['id_generation'=>$setting])}}">
#method('patch')
#csrf
<x-inputs.text-input name="prefix" :model-object="$setting" />
<x-inputs.button/>
</form>
But I keep getting errors:
Illuminate\Routing\Exceptions\UrlGenerationException
Missing required parameters for [Route: config.id_generation.update] [URI: config/id-generation/{id_generation}].(View: F:\PROJECTS\PHP\app\resources\views\config\id\edit.blade.php)
In as much as I can see, I have done everything correctly. Even artisan command route:list can clearly show the route with its parameters etc.
Where might I be getting it wrong.
Surprisingly if i change it to {{url('config/id-generation/', $setting)}} everthing seeems to be working fine.
you have to match your model variable name to your ressource name, you can customize it with as:
Route::group(['prefix'=>'config', 'namespace'=>'Config'], static function () {
Route::resource('id-generation', 'IDSettingsController', ['names'=>'config.id_generation', 'as' => 'setting'])->only(['index', 'edit', 'update']);
});

Too few arguments to function App\Http\Controllers 1 passed and exactly 2 expected

I'm trying to pass an array, and an ID as parameters to my controller function, but it's not working.
I tried multiple time with differents ways, but it's still not working.
view.blade.php
<form method="post" action="{{ route('clients.insert_clients', [$campagne->id, 'importData_arr' => $importData_arr, 'id_campagne' => $campagne->id]) }}">
web.php
Route::post('clients/importer/{campagne}', 'CampagneController#upload_clients')->name('clients.upload_clients');
CampagneController.php
public function insert_clients($importData_arr, $id_campagne)
The error I get
Too few arguments to function App\Http\Controllers\CampagneController::insert_clients(), 1 passed and exactly 2 expected
Any idea?
Change,
<form method="post" action="{{ route('clients.insert_clients', [$campagne->id, 'importData_arr' => $importData_arr, 'id_campagne' => $campagne->id]) }}">
To
<form method="post" action="{{ route('clients.insert_clients', ['campagne' => $campagne->id, 'importData_arr[]' => $importData_arr]) }}">
Change,
public function insert_clients($importData_arr, $id_campagne)
To,
public function insert_clients($campagne, Request $request)
You have a required parameter (campagne) as defined by your route so you need to pass it explicitly in that array of data.
I think you're trying to achieve you goal in a very messy way. First of all your route
Route::post('clients/importer/{campagne}', 'CampagneController#upload_clients')
You're declaring a single variable campagne but in your controller you're declaring the corresponding fuction as
public function insert_clients($importData_arr, $id_campagne)
and that's why you're getting the error, you're passing a single variable ($campagne), but the controller's method expects two variables ($importData_arr, $id_campagne).
You should update the method as follow
public function insert_clients($campagne)
as well as your form:
<form method="post" action="{{ route('clients.insert_clients', ['campagne' => $campagne->id]) }}">
#foreach($importData_arr as $value)
<input type="hidden" name="importData_arr[]" value="{{ $value }}" />
#endforeach
<!-- Other fields -->
After submitting the form, you can recover your data as follows:
public function insert_clients($campagne) {
$importData_arr = request()->get('importData_arr');
}
You need to define your route with 2 params instead of 1.
Route::post('clients/importer/{importData}/{campagne}', 'CampagneController#upload_clients')->name('clients.upload_clients');

How to display textbox value dynamically which is get from database using laravel

I got value from database and then i need to set those value to textbox. I have created a controller file with the method name of edit look like below
userdata.blade.php:
public function edit($id)
{
echo "You have clicked edit link".$name;
$editdata = DB::table('newuser')->where('Id','=',$id)->get();
return View::make('editdata',array('list' => $editdata));
}
I have passed array of value as parameter to the view file.now i need to diaplay the value of name to textbox.how to do that in html page using laravel. My html page look like below
editdata.blade.php:
<html>
<head></head>
<body>
<div>
{{Form::open(array('url' => 'login', 'method' => 'post'))}}
{{Form::label('name','Name',array('id'=>'label-name'))}}
{{Form::text('name',{{$list->Name}}}}
{{ Form::close() }}
</div>
</body>
</html>
can anyone tell me that what mistake i did.Thanks in advance
Just remove the curly brackets, you are already "inside" PHP code and don't need them:
{{ Form::text('name',$list->Name) }}
Also you get a collection from your controller you probably want to do:
$editdata = DB::table('newuser')->where('Id','=',$id)->first();
Or even:
$editdata = DB::table('newuser')->find($id);
get() returns a collection (multiple rows) and not the model itself. You can use User::find($id) which gives you direct access to the model with the specified Id.
When not using eloquent just replace get() with first()

Laravel Passing Variable from Forms

I am brand new to Laravel, and following a super basic tutorial.
However the tutorial did not come with an edit record section, which I am attempting to extend myself.
Route:
Route::controller('admin/products', 'ProductsController');
Controller:
class ProductsController extends BaseController
{
public function getUpdate($id)
{
$product = Product::find($id);
if ($product) {
$product->title = Input::get('title');
$product->save();
return Redirect::to('admin/products/index')->with('message', 'Product Updated');
}
return Redirect::to('admin/products/index')->with('message', 'Invalid Product');
}
..ECT...
I realise the controller is requesting an ID to use, but I cannot figure out how to pass it a product ID when the form is posted/get.
Form:
{{Form::open(array("url"=>"admin/products/update",'method' => 'get', 'files'=>true))}}
<ul>
<li>
{{ Form::label('title', 'Title:') }}
{{ Form::text('title') }}
{{ Form::hidden('id', $product->id) }}
..ECT...
{{ Form::close() }}
my initial idea was to pass the product id within the form URL like:
{{Form::open(array("url"=>"admin/products/update/{{product->id}}", 'files'=>true))}}
But no luck with that either.
The error I get is:
Missing argument 1 for ProductsController::postUpdate()
Interestingly if I type directly into the URL:
http://localhost/laravel/public/admin/products/update/3
It works and the item with id 3 is altered fine.
So can anyone help and inform me how to pass the id with a form?
Thanks very much
The first Problem here ist the following:
{{Form::open(array("url"=>"admin/products/update/{{product->id}}", 'files'=>true))}}
the {{product->id}} is wrong in two ways:
it should be {{$product->id}}
BUT it wouldn't work anyway because the inner {{..}} inside of the {{Form::...}} won't be recognized since it is inside a string and therefore part of the string itself.
You either have to write it this way:
{{Form::open(array("url"=>"admin/products/update/".$product->id, 'files'=>true))}}
or you give your route a name in your routes.php file and do it this way:
{{Form::open(array('route' => array('route.name', $product->id, 'files'=>true)))}}
I prefer the second way.
You also might want to look into Form Model Bingin

Categories