I'm trying to display a query in a view. Each result I want to be a submit button that would submit his value when clicked.
In ListController I have this code:
public function index()
{
$tables = DB::table('tables')->select('name')->get();
return view('welcome', compact('tables'));
}
In welcome.blade.php I have:
<form action="{{ route('get_table') }}" method="POST">
{{ csrf_field() }}
#foreach($tables as $data)
<input type="submit" name="tables[]" value="{{ $data }}">
#endforeach
</form>
In routes web.php I have:
Route::get('/', 'ListController#index');
And I get this error:
"htmlspecialchars() expects parameter 1 to be string, object given (View:...welcome.blade.php)"
What am I doing wrong?
$data is an object, despite you only selecting one column. You have two options:
Use the name property for $data:
<input type="submit" name="tables[]" value="{{ $data->name }}">
Or use pluck to retrieve a collection of single names.
$tables = DB::table('tables')->pluck('name');
Related
I'm very confused, I need your help, this is the error:
Missing required parameter for [Route: single.temp] [URI: singlepost/{name}] [Missing parameter: name]. (View: C:\Users\Toshiba\Desktop\working\mouhawla\resources\views\index.blade.php)
The search field:
<div class="search">
<form role="form" action="{{route('single.temp')}}">
<i class="fa fa-search"></i>
<div class="field-toggle">
<input type="text" name="name" class="search-form" autocomplete="off" placeholder="Search">
</div>
</form>
</div>
The method:
public function getPostByName($name) {
$products = DB::table('templates')
->where('name', $name)
->first();
return view('singlepost', compact('products'));
}
The route:
Route::get('/singlepost/{name}', 'App\http\controllers\TemplatesController#getPostByName')->name('single.temp');
The final view:
<h1 style="text-align: center;">ACH-template</h1>
<table>
<tr>
<td><img src="/storage/{{$products->image_path}}"></td>
<td><img src="/storage/{{$products->image_path2}}"></td>
<td><img src="/storage/{{$products->image_path3}}"></td>
<td>
<p>
<h2 style="text-align:center;">{{$products->name}}</h2>
</br>
<p>{{$products->description}}</p>
</p>
</td>
</tr>
</table>
<a href="/storage/{{$products->file_path}}" class="btn btn-primary">
<button class="btn" style="width:100%">
<i class="fa fa-download"></i> Download
</button>
</a>
The error is very explanatory. You are trying to use /singlepost/{name} route, but on your blade file, you are doing route('single.temp'), it is telling you that it needs the parameter name, else it cannot create the URL as it is a missing parameter.
You should have something like:
<form role="form" action="{{route('single.temp', ['name' => VALUE'])}}">
But that will not solve your problem, as you are trying to do a search, so you want something like /singlepost/John, and John is going to be input by the user on the input field. So you have to do an AJAX call because {{ route('single.temp') }} is going to be rendered by PHP and served to the user, so it is always going to miss the needed parameter.
What you can also do is get that value from the Request instead of a URL parameter.
You have defined a route which requires a parameter: {$name}. You have also used the route helper to generate a URL which takes the name of a route as the first argument and an array of parameters as an optional second argument.
When you have used route('single.temp') in your form action, you have not specified any parameters and so Laravel is throwing the error you're seeing. To resolve this error, you would need to specify a $name parameter as the second argument (i.e. route('single.temp', ['name' => 'something'])). This is not ideal though as if you're using $name as a search term, you don't know the value when the page is first rendered and so can't provide that value.
There are a few ways you could achieve your goal of searching records, a basic example of how you could do this follows.
web.php
Define two routes, the first to return a view with a form and another to process the form submission and show the results.
Route::get('/templates', [TemplateController::class, 'index'])
->name('templates.index');
Route::get('/templates/search', [TemplateController::class, 'search')
->name('templates.search');
TemplateController.php
Define the two functions which will be used when one of the routes defined above is requested.
class TemplateController extends Controller
{
// return a view
public function index()
{
return view('templates.search', ['templates' => []]);
}
// process the form submission
// perform a search for the $request search term
// return a view with the results
public function search(Request $request)
{
$request->validate([
'term' => ['required', 'string']
]);
$templates = Template::where('name', $request->term)->get();
return view('templates.search', ['templates' => $templates]);
}
}
templates/search.blade.php
{{-- create a form which will submit to the search route --}}
{{-- note I use GET rather than POST here, explained later --}}
<form action="{{ route('templates.search') }}" method="GET">
#csrf
<input type="text" id="term" name="term" />
<button type="submit">
{{ __('Search') }}
</button>
{{-- loop over and show results if there are any --}}
#forelse ($templates as $template)
{{ $template->name }}
#empty
{{ __('Empty') }}
#endforelse
</form>
The above should be self explanatory. My reason for using GET rather than POST in the search is because the value will be added to the URL as a query string parameter meaning it can be bookmarked or shared with ease.
I am trying to bind form action(which has id related to a model/table record) to controller method.
My web.php has
Route::post('/rejectControlTransfer/{id}', 'ControlTransferController#rejectControlTransfer')->name('controltransfers.rejectTransfer');
My form has
<form id='form_process_rejectControl' action="{{route('controltransfers.rejectTransfer', [$controlTransferId])}}" method="POST" style="display: inline;">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
And my controller has
public function rejectControlTransfer(Request $request, ControlTransfer $controlTransfer)
{
dd($controlTransfer->id);
}
I am trying to bind ControlTransfer $controlTransfer with the actual id passed so that when I try get value of $controlTransfer->id or $controlTransfer->name would give me their values.
Current I am not getting any value.
If you are using id in the route '/rejectControlTransfer/{id} then you can only access it through $id variable in your controller, which is a raw variable int.
Besides that, your action's 'route' function is not used correctly, you need to put 'id' as your key, like:
route('controltransfers.rejectTransfer', ['id' => $controlTransferId])
However, if your ControlTransfer is a model, you can use Model Binding. By:
Route::post('/rejectControlTransfer/{controlTransfer}', 'ControlTransferController#rejectControlTransfer')->name('controltransfers.rejectTransfer');
<form id='form_process_rejectControl' action="{{route('controltransfers.rejectTransfer', ['controlTransfer' => $controlTransferId])}}" method="POST" style="display: inline;">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
public function rejectControlTransfer(Request $request, ControlTransfer $controlTransfer)
{
dd($controlTransfer->id);
}
Disclaimer: Above code is untested.
I'm having a problem with my code now and it seems like that the route that I specify is not found whenever I try to access it.
Route:
Route::post('nniscaseassociates/pushreliever/{id}', 'NnisCaseAssociateController#pushreliever');
Route::get('nniscaseassociates/{id}/reliever', 'NnisCaseAssociateController#reliever');
View:
<form method="POST" action="/nniscaseassociates/pushreliever/{{ $caseassociate->nnis_case_id }}">
{{ method_field('PUT') }}
{{ csrf_field() }}
<...Fields...>
</form>
Controller:
public function reliever($id)
{
//this will be nniscase id then
$caseassociate = NnisCaseAssociate::findOrFail($id);
//return dd($caseassociate);
return view('nniscaseassociates.reliever', compact('caseassociate'));
}
public function pushreliever(Request $request, $id)
{
...Statements...
return redirect('nniscases/'.$caseassociates->nnis_case_id.'/edit');
}
By the end of submitting I want to redirect to my edit page and display the changes that I made from the previous form. And this is the error that I got when click submit.
You are making a PUT request and not POST.
Either remove this line from your form:
{{ method_field('PUT') }}
Or
Change your POST route to PUT:
Route::put
You have not defined PUT method in your web.php
Route::put('nniscaseassociates/pushreliever/{id}', 'NnisCaseAssociateController#pushreliever');
in your view :
<form method="POST" action="/nniscaseassociates/pushreliever/{{ $caseassociate->nnis_case_id }}">
{{ method_field('PUT') }}
{{ csrf_field() }}
<...Fields...>
</form>
in your controller :
public function pushreliever(Request $request, $id)
{
echo $id;
}
You should try this:
<form method="POST" action="{{ url('nniscaseassociates/pushreliever',[$caseassociate->nnis_case_id]) }}">
Missing argument 2 for App\Http\Controllers\Company\OrderController::store()
I got this error in my store controller, my form is passing 2 parameters but the second one is not found.
Route: Route::resource('order', 'OrderController');
$company gets converted to a model in the controller.
The form:
<form class="form-horizontal" role="form" method="POST" action="{{action('Company\OrderController#store', [$company,$orderid])}}">
{{ csrf_field() }}
<button type="submit" class="btn btn-primary">Accept</button>
</form>
Any ideas?
Thanks!
If you created store route with Route::resource() it doesn't expect any parameters and should look like this:
public function store(Request $request)
So, you need to pass data using hidden inputs, like:
{!! Form::hidden('data', 'some data') !!}
And then get data in controller with:
$data = $request->data;
You should specify the key-value pair like this:
['company_id' => $company->id, 'order_id' => $order->id]
So your form would look like:
<form action="{{ action('Company\OrderController#store', ['company_id' => $company->id, 'order_id' => $order->id]) }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<button type="submit" class="btn btn-primary">Accept</button>
</form>
Hope this helps!
I am using laravel 5.2. I want to print the database content that is stored in my database dynamically on the desired page. I tried but an error appears everytime i.e;( undefined variable:). I just want to print whatever content I store in my database table dynamically.
My code is here:
My model name is:gallery
My routes:
Route::get('/gallery/list' ,[
'uses'=>'gallerycontroller#viewgallerylist',
'as'=>'viewgallery'
]);
Route::post('/gallery/save' ,[
'uses'=>'gallerycontroller#savegallery',
'as'=>'savegallery'
]);
My controller:
public function viewgallerylist()
{
$galleries = gallery::all();
return view('gallery')->with('galleries', $galleries);
}
public function savegallery(Request $request)
{
$gallery1=$request['gallery_name'];
$gallery=new gallery();
$gallery->name=$gallery1;
$gallery->save();
return redirect()->route('viewgallery');
}
My desired page:
<form method="post" action="{{ route('savegallery') }}">
<input class="form-control" type="text" name="gallery_name">
<button type="submit" class="btn btn-primary" id="upl">Create+</button>
<input type="hidden" value="{{ Session::token() }}" name="_token">
</form>
#foreach($galleries as $gallery)
<p>{{ $gallery->name }}</p>
#endforeach
Most model Classes will have a capital letter. Are you sure your Model isn't called Gallery instead of gallery?
which means that you need to call Gallery::all() in your controller and make sure use App\Gallery; is at the top of your page.
public function viewgallerylist()
{
$galleries = Gallery::all();
return view('gallery')->with('galleries', $galleries);
}
The problem might be with your model.... please provide a larger view
Route::get('/' ,[
'uses'=>'gallerycontroller#viewgallerylist',
'as'=>'viewgallery'
]);
Route::post('/' ,[
'uses'=>'gallerycontroller#savegallery',
'as'=>'savegallery'
]);