Before question , This is my code
Show.blade.php
#foreach ($comments as $comment)
#if ($comment->post_id == $post->id)
.....
<form action="{{ route('createreply', ['rid' => $comment->id]) }}" method="POST">
#csrf
<input type="text" class="form-control" name="replyContent" id="replyContent" style="color:white" />
<button type=" submit" style=" color :lavender " class="btn btn-secondary">comment</button>
</form>
....
#endif
#endforeach
(( comment is original comment, and i made reply for answer of comment ))
web.php (route)
Route::post('/replycreate', [CommentController::class, 'createReply'])->name('createreply');
my controller (comment)
public function createReply(Request $request, $rid)
{
$reply = new Reply();
$reply->replyContent = $request->replyContent;
$reply->user_name = Auth::user()->name;
$reply->comment_id = $rid;
dd($reply);
$reply->save();
return redirect()->back();
}
I have problem with making re-reply,
and my code returns error and it says
Too few arguments to function App\Http\Controllers\CommentController::createReply(), 1 passed in C:\Users\ALSACE\post-app\vendor\laravel\framework\src\Illuminate\Routing\Controller.php on line 54 and exactly 2 expected
I tought i had used two argument , but this kind of error occurs.
so i tought using dd($reply),
dd() can't even complete.
you can guess $rid is empty .. but in the same Show.blade.php , I had used $comment with no problem so i think i can not be problem.
plz help with newbe Laravel student!!
Your createReply method expect 2 arguments, BUT you don't have variable in your route.
You must change the route to:
Route::post('/replycreate/{rid}', [CommentController::class, 'createReply'])->name('createreply');
You forgot to add the id to the route itself. If you're going to require it in the function parameters, then it needs to be added on the route URL.
Route::post('/replycreate/{rid}', [CommentController::class, 'createReply'])->name('createreply');
Related
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');
This is error says. Im inserting an static value for now in controller to check if the controller is okay. the code is below
protected function methodNotAllowed(array $others)
{
throw new MethodNotAllowedHttpException($others);
}
//IM JUST inserting static value for now to be able to check if inserting
DataController
public function update(Request $request, $name=null)
{
$insert = new leave([
'bio_id' => '10258',
'vacation_balance' => '25',
'sick_balance' => '25'
]);
$insert->save();
return view('pages/admin/data');
}
Route web.php
Route::post('admin/pages/admin/data', 'Admin\DTRDataController#update');
data.blade.php
<form action="{{url('admin/pages/admin/dtrdata')}}" method="post">
{{ csrf_field() }}
<input type="hidden" name="_method" value="PATCH">
<input type='text' class='total_undertimes' name='total_undertimes' id='total_undertimes' style='width:70px' />
<input type="submit" class="btn btn-primary" value="Edit" />
</form
What is ther error in my code
Remove
<input type="hidden" name="_method" value="PATCH">
line from your view. This makes your FORM submitted as PATCH method.
FYI:
methodNotAllowed exception throws when request using wrong method.
Ex:) When u POST to URL that is configured as GET in your Route file. In your situation you are requesting POST url using PATCH method.
You are showing the route for the URI admin/pages/admin/data but your form is going to admin/pages/admin/dtrdata. I am not sure what that 'dtrdata' URI is but it doesn't accept the PATCH method.
admin/pages/admin/data != admin/pages/admin/dtrdata
Your Form Url and route are different :
route : admin/pages/admin/data
form : admin/pages/admin/dtrdata
you need to do two correction in your code.
your Form Url and route are different :
route url : admin/pages/admin/data
form_url : admin/pages/admin/dtrdata
methodNotAllowed this error you got because you have submit form using patch method and you define post method in route. so should be used
Route::patch('admin/pages/admin/data', 'Admin\DTRDataController#update');
instead of
Route::post('admin/pages/admin/data', 'Admin\DTRDataController#update');
Route::patch('admin/pages/admin/data', 'Admin\DTRDataController#update');
My link href on the view is like this :
<a href="{{ route('message.inbox.detail.id', ['id' => $message->id]) }}" class="btn btn-default">
<span class="fa fa-eye"></span> {{ trans('button.view') }}
</a>
My routes is like this :
Route::get('message/inbox/detail/id/{id}', ['as'=>'message.inbox.detail.id','uses'=>'MessageController#detail']);
When clicked, the url display like this :
http://myshop.dev/message/inbox/detail/id/58cadfba607a1d2ac4000254
I want get id with Request $request
On the controller, I try like this :
public function detail(Request $request)
{
dd($request->input('id'));
}
The result : null
How can I solve it?
You can get it like this
$request->route('id')
Inside request class you can access it this way
$this->route('id')
I usually use it when I'm validating field to make sure it's unique, and I need to exclude model by ID when I'm doing update
This is because you're passing the id parameter through url, you should send it using a form like this
{!! Form::open(['route'=>' route('message.inbox.detail.id', 'method'=> 'POST']) !!}
<input type="hidden" name="id" value="{{ $message->id }}" />
{!! Form::submit({{ trans('button.view') }}, ['class'=>'btn btn-default']) !!}
{!! Form::close() !!}
and now you can access via request in your controller
public function detail(Request $request)
{
dd($request->input('id'));
}
For people who are looking for answers using newer versions of laravel, you can get the route id using the following:
Example route with model binding
Route::put('/post/{post}, [PostController::class, 'update]);
$this->route('id') will not work in this case due to model binding.
instead you can do this:
// using in controller
request()->route('post.id);
or
// using in form request
$this->route('post.id');
Get the id via this:
public function detail($id)
{
dd($id);
}
I built a laravel 5 application and now I am testing how it handles different inputs. Thus I encountered a weird problem. In the header I have a search field. It returns results, paginated by 10.
The problem
If a user inputs a letter, for an example "e" in English, everything works just fine. However, when a user enters a letter, for an example "e" in Bulgarian - the first page of the results is shown correctly and when a user hits page 2 the query in the search from "е" in Bulgarian changes to "%D0%B5" and no more results are shown. Here is an actual link to the website. http://podobri.eu
I guess this has something to do with the encoding but I can't see what I am doing wrong.
Here is the actual code
Route
Route::get('/search', [
'uses' => '\Podobri\Http\Controllers\SearchController#getResults',
'as'=>'search.results',
]);
SearchController
public function getResults(Request $request){
$query = $request->input('query');
$comments = Comment::where(function($query){
return $query;
})->orderBy('created_at', 'desc')->get();
if(!$query || $query==''){
return view('problems.index')->with('comments', $comments);
}
$problems = Problem::where(DB::raw("CONCAT(problem_title, ' ', problem_description)"), 'LIKE', "%$query%")
->orWhere('location', 'LIKE', "%$query%")
->orWhere('category', 'LIKE', "%$query%")
->orderBy('created_at', 'desc')->paginate(10);
Carbon::setLocale('bg');
return view('search.results')
->with('comments', $comments)
->with('problems', $problems)
->with('title', 'Резултати за "'."$query".'" | Подобри')
->with('description', 'Резултати за "'."$query".'" в системата на Подобри');
}
View
#foreach($problems as $problem)
<div>
#include('problems.partials.problemblock')
</div>
#endforeach
<!-- Paginating-->
{!! $problems->appends(Request::except('page'))->render() !!}
Search form
<form action="{{ route('search.results') }}" role="search" class="navbar-form navbar-left head-form-responsive">
<div class="form-group">
<input type="text" required id='searchQuery' title="Търсете за проблеми" value="{{ Request::input('query') }}" name="query" class="form-control"
placeholder="Търсете за проблеми"/>
</div>
<button type="submit" id='searchBtn' class="btn btn-default">Търсете</button>
</form>
It looks to me like your issue is happening because the paginator is appending a trailing slash with some odd redirect (not sure if you guys are using custom htaccess). Example, if you search for e, this is the URL:
http://podobri.eu/search?query=e
However, the URL for the second page is this:
http://podobri.eu/search/?query=e&page=2
Notice the slash in front of ?query. If you remove the slash, it works. So, how can you fix this?
This was actually fixed a few months ago. You can see this commit here: https://github.com/laravel/framework/commit/806fb79f6e06f794349aab5296904bc2ebe53963
So, if you are using L5.1 or 5.2, you can run composer update, and it'll fix itself. However, if you are using 5.0, it seems like it still has this bug so you can use the setPath method and try this instead:
{!! $problems->setPath('')->appends(Request::except('page'))->render() !!}
I had a similar problem and my solution was changed the method of the route.
Route::post('uri', 'Controller#function')
->name ('view.function');
for:
Route::any('uri', 'Controller#function')
->name ('view.function');
It's works for me.
Regards and good luck.
I have setup nested RESTful resource routes like so:
Route::group(array('prefix'=>'opening-hours'), function(){
Route::resource('library', 'LibraryController');
Route::resource('library.interval', 'LibraryIntervalController');
});
I have a blade form which has a select dropdown with options populated from the db, like this:
{{ Form::open(array('route' => 'opening-hours.library.show', 'method' => 'GET')) }}
<legend>Select a library to edit</legend>
<div class="form-group">
<label for="">Please select a library to modify its opening hours:</label>
<select class="form-control" name="id" required>
#foreach ($library_options as $id => $name)
<option value="{{ $id }}">{{ $name }}</option>
#endforeach
</select>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
{{ Form::close() }}
The form submits to the named route:
opening-hours.library.show
The routing itself works but I have a couple of questions about the logistics (I'm a little confused about how to use the routing):
I can get the form to submit to the correct route if I make it a
'GET' request (GET/HEAD opening-hours/library/{library} =>
opening-hours.library.show), as the corresponding POST route does not
exist
how do I pass the user-selected $id to the controller show($id)
method?
when I submit the form, it goes to the URI
"../opening-hours/library/%7Blibrary%7D?id=3". So if the user selects
the library with id=3, how do I make the URI "../opening-hours/library/3"?
Here is my Library controller show method:
/**
* Display the specifed library.
*
* #param int $id
* #return Response
*/
public function show($id)
{
//
return "This is a library with id: " . $id . "!";
}
When I submit the form, the following is displayed:
This is a library with id: {library}!
Of course, I'd want it to display:
This is a library with id: 3!
I'm obviously not understanding something critical about how REST or Laravel or both work here. Any pointers would be very much appreciated, I've spent a day knocking about with this!
Thanks a lot
Try using
$id = Input::get('id');
Documentation
I would also recommend using the provided helpers for forms:
echo Form::select('size', array('L' => 'Large', 'S' => 'Small'));
Documentation
Writing out the problem seemed to help me come up with the solution. It seemed silly to use a form to send a 'GET' request, so I thought (bing!) why not just construct the URIs as anchor tag href attributes. So instead of using a form, I did:
#foreach( $library_options as $id => $name )
<a class="btn btn-primary" href="{{ URL::to('opening-hours/library/' . $id) }}">Edit {{ $name }}</a>
#endforeach
That solved the issue of dodgy URIs and also the id was directly available from the RESTful controller method like so:
public function show($id)
{
return "This is a library with id: " . $id . "!";
}