I need update some record and this is form action in my blade
<form class="form-vertical" role="form" method="post" action="/projects/{{ $project->id }}/collaborators/{{ $collaborator->user()->first()->id }}">
My controller
public function update(Request $request, $projectId, $collaboratorId)
{
$this->validate($request, [
'status' => 'required',
]);
DB::table('permissions')
->where('project_id', $projectId)
->where('collaborator_id', $collaboratorId)
->update(['status' => $request->input('status')]);
return redirect()->back()->with('info','Your Permission has been updated successfully');
}
routes
Route::put('projects/{projects}/collaborators/{id}',['uses'=>'ProjectCollaboratorsController#update',]);
when click update button generate following error
Undefined variable: project (View: C:\Users\Nalaka\Desktop\c\resources\views\collaborators\permissionedit.blade.php)
how to fix this?
With your route Route::put('projects/{projects}/collaborators/{id}',['uses'=>'ProjectCollaboratorsController#update',]); you need to send a PUT request. Your form send a POST request.
Change your route by Route::post or use some javascript to send a PUT request with ajax.
Try that and let's see if there are other error after.
Also, if there are "undefined variable", show the code around this variable.
Related
**i am confused with routes and how to specify the path . **
web.php- this route is inside my module called events
<?php
use Illuminate\Support\Facades\Route;
Route::prefix('event')->group(function() {
Route::get('/create', 'EventController#index');
});
Route::post('/create', 'EventController#store');
blade file-i have a form which calls create
<form class="form-horizontal" action="../create" enctype="multipart/form-data" method="post">
**controller-here in store method i store the get thevalues fromthe user and storeitin the db **
function store(StoreCompanyRequest $req)
{
//
$req->validate([
'name'=>'required',
'title'=>'required',
'description'=>'required',
'category'=>'required',
'sdate'=>'required',
'edate'=>'required',
'address_address'=>'required',
'address_latitude'=>'required',
'address_longitude'=>'required',
'images' => 'required',
'images.*' => 'mimes:jpeg,jpg,png,gif,csv,txt,pdf|max:2048'
]);
abort_unless(\Gate::allows('company_create'), 403);
if($req->hasfile('images')) {
foreach($req->file('images') as $file)
{
$image_name = $file->getClientOriginalName();
$file->move(public_path().'/uploads/', $image_name);
$imgData[] = $image_name;
}
$event = new Event;
$event->name=$req->name;
$event->save();
return view('/home');
}
}
Try to use action="/create" in your form like so:
<form class="form-horizontal" action="/create" enctype="multipart/form-data" method="post">
I don't work with PHP and have no prior experience with Laravel but I can see that you are creating a route for the POST /create endpoint. With your current form object you reference the route action with a relative URL. So, if the form is on a page under the /path/to/form route, then the form submission results in a call to the POST /path/to/create route instead of the (maybe) intended POST /create route.
I have a submit form which I want to send to my RegisterController and I get this error
""Too few arguments to function App\Http\Controllers\Auth\RegisterController::create(), 0 passed and exactly 1 expected""
The create method demands an array.How to convert my request ot an array?
the form:
<form method="POST" action="{{ route('posting') }}">
#csrf....and so on
the routes:
Route::get('administration/register', function () {
return view('vregister');
})->name('registration');
Route::post('/insert','Auth\RegisterController#create')->name('posting');
method of RegisterController.php
protected function create(array $data)
{
$user= User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
$role=Role::select('id')->where('name','Support')->first(); //??
$user->roles()->attach($role);
return $user;
}
You are using wrong instance in your create method. When using forms, you should use Request class to actually send form data to your controller. The way you have it, no data is being sent to create() method, hence the error. Edit your create() method to this:
protected function create(Request $data)
And import Request class at the top:
use Illuminate\Http\Request;
Read more at the official documentation
.
EDIT:
To redirect to a specific page after saving the data, change your return statement to this:
return redirect()->back();
This will return you back to the previous page. You can also add any route here, that you wish to redirect to:
return redirect()->rote('route.name');
working with Laravel 5.6 and Mysql. and need delete table data using following data.
<td><a class="button is-outlined" href="/student/{{$student->id}}/delete">Delete</a></td>
and Controller delete function is,
public function delete($id)
{
DB::table('students')
->where('id', $id)
->delete();
return redirect()->back();
}
and route is like this,
Route::resource('student','StudentController');
but when click delete button it is generated following error message,
(1/1) NotFoundHttpException
how can fix this problem?
If you use resource controller, you can't generate a link for the DELETE method.
By the way, it's not delete method, but destroy method and link.
The DELETE method expects the request to have DELETE header (like POST, GET or PUT).
The simplest way is to define an URL for you delete method :
Route::get('student/{site}/delete', ['as' => 'student.delete', 'uses' => 'StudentController#delete']);
Or you must use a form like this to call DELETE header :
<form action="{{ route('student.destroy', $studentId) }}" method="POST">
#method('DELETE')
#csrf
<button>Delete</button>
</form>
And you need to change the name of your method :
public function destroy($id)
If you are using Resource Controller you can try this. You can try to DELETE using AJAX Call too.
<td><a class="button is-outlined" id="delete-record">Delete</a></td>
DELETE verb will automatically call destroy action from your Resource Controller.
public function destroy($id)
{
$deleted = DB::table('students')
->where('id', $id)
->delete();
// return number of deleted records
return $deleted;
}
And to perform DELETE use AJAX call like this.
$('#delete-record').click(function(){
$.ajax({
url: '/student/'+{{$student->id}},
type: 'DELETE', // user.destroy
success: function(result) {
console.log("Success");
console.log("No. Of Deleted Records = "+result);
}
});
});
I hope this solution will also work like above one that is already provided.
I am having a problem with uri action in laravel.
When I submitted the form, it redirects to the full url if it is successful. But if there is an error, it remains to the current url address.
example:
The current URL is : http://localhost:8000/test?url=test_sample
and my form looks like below:
<form action="{{ url('test?url=action') }}" method="POST" enctype="multipart/form-data">
{{ csrf_field() }}
<input type="file" name="pdf_file">
<input type="submit" name="submit" value="Submit"/>
</form>
If the above form contains an error, it will just remain to test?url=test_sample url. If it is successful, it redirects to test?url=action
EDIT
Controller
class MyFormController extends Controller
{
public function uploadfile(Request $request)
{
$this->validate($request, [
'pdf_file' => 'required|mimes:pdf|max:5000'
]);
return 'uploaded';
}
}
web.php
Route::post('/test', 'MyFormController#uploadfile');
I need this feature to take effect on my site.
Does anybody know?
The URL you're using (http://localhost:8000/test?url=test_sample) contains a url parameter. To use this parameter in the controller, you need to get the value from the $request object injected into the uploadfile() controller method:
$request->get('url');
You can use it to redirect the user from the uploadfile() controller method after processing the upload:
public function uploadfile(Request $request)
{
// ...
return redirect($request->get('url'));
}
Because you're using the validate() method in the controller method, the request will automatically redirect back if the validation fails. You can replace this with manual validation to handle the result yourself:
$validator = Validator::make($request->all(), [
'pdf_file' => 'required|mimes:pdf|max:5000',
]);
if ($validator->fails()) {
return redirect($request->get('url'))
->withErrors($validator)
->withInput();
}
return redirect($request->get('url'));
Edit - I think I misunderstood part of your question. It doesn't look like you want to use the url parameter in the controller method. If not, just change the validation statement.
i am trying to access one route like pages.trashmultiple this in my view. But its throwing error of Route [pages.trashmultiple] not defined.. Here is how my view looks like:
{!!Form::open([
'method' => 'put',
'route' => 'pages.trashmultiple'
])!!}
<th class="table-checkbox">
<input type="checkbox" class="group-checkable" data-set="#sample_3 .checkboxes"/>
</th>
{!!Form::close()!!}
This is how my controller looks like:
public function trashmultiple(Request $request){
//return "trash multiple page";
return Input::all();
}
And this is how my routes look like:
Route::group(['middleware' => ['web']], function () {
Route::get('pages/trash', 'PagesController#trashpage');
Route::get('pages/movetotrash/{id}', 'PagesController#movetotrash');
Route::get('pages/restore/{id}', 'PagesController#restore');
Route::get('pages/trashmultiple', 'PagesController#trashmultiple'); //this is not working
Route::resource('pages', 'PagesController');
});
When i load my url http://localhost:8888/laravelCRM/public/pages it shows me this below error:
ErrorException in UrlGenerator.php line 307:
Route [pages.trashmultiple] not defined. (View: /Applications/MAMP/htdocs/laravelCRM/resources/views/pages/pages.blade.php)
I want to know why i am unable to access the pages.trashmultiple when i have already defined it.
Is there any way i can make it accessible through pages.trashmultiple?
Thank you! (in advance)
chnage your route to like this:
Route::get('pages/trashmultiple', 'PagesController#trashmultiple');
To
Route::get('pages/trashmultiple', [
'as' => 'pages.trashmultiple', 'uses' => 'PagesController#trashmultiple'
]);
Change your route so that it becomes:
Route::get('pages/trashmultiple',
['as'=>'pages.trashmultiple',
'uses'=>'PagesController#trashmultiple']);