I encounter a problem on Laravel, I followed the Bootcamp step by step and it was nice, very clear I finished it entirely,
I'm facing trouble when I try to reproduce it on a project of my own, everything goes cool until I try to implement the "edit" part see: https://bootcamp.laravel.com/blade/editing-chirps
I'm getting this error : Missing required parameter for [Route: Region.update]
I've been looking for some times but didn't find anything that worked for me, here is the code :
Controller :
public function edit(Region $region)
{
return view('regions.edit', [
'region' => $region,
]);
}
public function index()
{
$regions = Region::all();
return view('regions.index', [
'regions' => $regions,
]);
}
public function update(Request $request, Region $region)
{
$validated = $request->validate([
'name' => 'required|string|max:255',
]);
$region->update($validated);
return redirect(route('Region.index'));
}
Index view where I'm passing the data :
<x-dropdown-link :href="route('Region.edit', $region)">
<img src={{url("build/img/edit.png")}} width="40">
</x-dropdown-link>
Edit view specifically where problems happens.
<form method="POST" action="{{ route('Region.update', $region) }}">
Web route file
Route::resource('Region',RegionController::class)
->only(['index', 'create', 'store', 'edit', 'update'])
->middleware(['auth', 'verified']);
When I click the link in the index view, I'm redirect on the right link : http://localhost/Region/5/edit ( 5 is an exemple of id ).
But the error from the title appears.
I tried this because I read it here :
<form method="POST" action="{{ route('Region.update', ['Region' => $region]) }}">
But it didn't change anything.
Any help would be really appreciate.
Thank you for advance
Laravel Resource is case sensitive I think... So, we need to name parameter converter with uppercase like in resource name.
public function edit(Region $Region);
public function update(Request $request, Region $Region);
Related
I want to redirect to my new post when I created a new post in Laravel
But I get a ArgumentCountError
Too few arguments to function App\Http\Controllers\ArticlesController::store(), 1 passed in C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Routing\Controller.php on line 54 and exactly 2 expected
How can I fix it? Thanks
web.php
<?php
Route::resource('articles', ArticlesController::class);
Route::get('/', [ArticlesController::class, 'index'])->name('root');
Route::resource('articles.comments', CommentsController::class);
ArticlesController.php
public function store(Request $request, $id) {
$content = $request->validate([
'title' => 'required|max:30',
'content' => 'required|min:10'
]);
//限制只有透過登入才能CREATE文章
auth()->user()->articles()->create($content);
return redirect('articles/'. $id)->with('notice', '文章發表成功!');
}
create.blade.php
<form class="container-fluid" action="{{ route('articles.store') }}" method="post">
Check your store() method. I think it should get only Request $request.
Example
public function store(Request $request) {
$content = $request->validate([
'title' => 'required|max:30',
'content' => 'required|min:10'
]);
//限制只有透過登入才能CREATE文章
$article = Article::create($content); // static is not best practice, only for example
return redirect('articles/'. $article->id)->with('notice', '文章發表成功!');
}
But before using the create method, you will need to specify either a fillable or guarded. Check docs
Presumably you need / have a way of viewing an article anyway, whether it's just been added or not, so in your web.php you would want a GET request to retrieve an article by passing its ID:
Route::get('/article/{id}', [ArticleController::class, 'viewArticle'])-> name('article.view');
Then you would want a POST request to add a new article :
Route::post('/addarticle', [ArticleController::class, 'addArticle'])-> name('article.add');
In your ArticleController, at the end of your addArticle method, once your new article has been created, you can then return a redirect to your "view article" route referencing its name, and passing in the parameter that it expects - the new article's ID - as part of the route, like so :
$article = new Article();
... populate the article's details here ...
return redirect()->route('article.view', ['id' => $article->id]);
I already solved it by my way
remove $id from store() function
just add $article before auth()->user()->articles()->create($content)
$id change to $article->id from redirect()
Example
public function store(Request $request) {
$content = $request->validate([
'title' => 'required|max:30',
'content' => 'required|min:10'
]);
$article = auth()->user()->articles()->create($content);
return redirect('articles/'. $article->id)->with('notice', '文章發表成功!');
}
Thank you
I tried looking for all the possible solutions none of it worked and this is very basic trying to send data from a controller to view in Laravel.
Paymentcontroller
public function payment() {
$plans =[
'Basic' => "Monthly"
];
$intent = $user->createSetupIntent();
return view('pages.subscription', compact('intent', 'plans'));
}
PageController
public function index(string $page)
{
if (view()->exists("pages.{$page}")) {
return view("pages.{$page}");
}
return abort(404);
}
View pages.subscription
<div>
{{ $intent }}
</div>
route
Route::get('{page}', ['as' => 'page.index', 'uses' => 'PageController#index']);
Route::get('/subscription', 'PaymentController#payment');
This makes the page work but doesn't display the data
Move Route::get('/subscription', 'PaymentController#payment'); before Route::get('{page}',.... (it should be your last route in the list).
Currently when you call /subscription endpoint you are calling PageController#index, but it doesn't contain logic of your PaymentController#payment and doesn't pass any data to view.
I have one route which is accepting one argument perfectly as
Route::get('view-request/type/{type}/id/{id}', 'CustomerReqController#testing')->name('request.manage');
and also call it in blade by this
<a href="{{route('request.manage',['type'=>'new','id'=>'data'])}}"
and the controller is
public function testing(Request $request,$type,$id){
dd($request->all());
}
it gives me error
Missing required parameters for [Route: request.manage] [URI: admin/view-request/type/{type}/id/{id}]. (View: /var/www/html/ehs_crm_laravel/resources/views/common/navbar.blade.php) (View: /var/www/html/ehs_crm_laravel/resources/views/common/navbar.blade.php) (View: /var/www/html/ehs_crm_laravel/resources/views/common/navbar.blade.php)
What am i doing wrong?
use:
<a href="{{ route('request.manage', ['type' => 'new', 'id' => 'data']) }}">
you can get your route parameter by simply using this code. hope this will work for you. for Get and Post method both.
public function testing(Request $request)
{
$type= $request->type;
$id= $request->id;
}
follow steps this code is working to me.
1 : declare route
Route::get('view-request/type/{type}/id/{id}', 'UserController#index')->name('request.manage');
2: create link
Register
3: get data in controller
public function index($type,$id,Request $request){
echo $type;
echo $id;
}
I need to pass an additional parameter($uid) from my index.blade.php to my edit.blade.php by clicking on a button.
My index.blade.php:
Edit
My FlyersController:
public function edit($id, $uid)
{
return view('backend.flyers.edit')->withUid($uid);
}
With the code above I get an error: "Missing argument 2 for App\Http\Controllers\FlyersController::edit()"
What am I doing wrong here?
The error is not throwing from the action method. It is coming from route for that URL.
Check the URL for passing argument to the the controller.
If this is the your desire URL localhost:8000/backend/flyers/10/edit?%24uid=1 then the second argument is in $request variable not in controller function argument.
You should pass an array into action() helper:
action('FlyersController#edit', ['id' => Auth::user()->id, 'uid' => 1])
Ok,
the only way I can solve this is by using the following in My FlyersController:
public function edit(Request $request, $id)
{
return view('backend.flyers.edit')->withRequest($request);
}
and access then the uid with {{request->uid}} in my view.
If anybody has a better solution for this, let me know.
Use this code
return view('backend.flyers.edit', ['var1' => $var1, 'var2' => $var2]);
That will pass two or more variables to your view
Following are my codes:
Model:
class Slide extends \Eloquent {
// Add your validation rules here
public static $rules = [
'title' => 'required|between:3,100',
'image' => 'required',
'url' => 'url',
'active' => 'integer'
];
// Don't forget to fill this array
protected $fillable = ['title', 'image', 'url', 'active'];
}
Controller Update Method:
public function update($id)
{
$slide = Slide::find($id);
$validator = Validator::make($data = Input::all(), Slide::$rules);
if ($validator->fails())
{
return Redirect::back()->withErrors($validator)->withInput();
}
$slide->update($data);
return Redirect::route('admin.slides.index')
->with('message', 'Slide has been updated.')
->with('message-type', 'alert-success');
}
Route:
Route::group(array('prefix' => 'admin'), function() {
# Slides Management
Route::resource('slides', 'AdminSlidesController', array('except' => array('show')));
});
Form in View:
{{ Form::model($slide, array('route' => 'admin.slides.update', $slide->id, 'method' => 'put')) }}
#include('admin/slides/partials/form')
{{ Form::close() }}
Partial Form is simple form, not sure if I need to share it here or not. Let me know.
Error:
Edit page loads perfectly and populates data from db, but when I submit the edit form, I get following error:
Call to a member function update() on a non-object
The following line seems to be creating problems:
$slide->update($data);
I have searched over the internet for solution but nothing is working. Have tried composer dump_autoload, even tried doing everything from scratch in a new project, still same issue. :(
Help please!!
---- Edit ----
Just quickly tried following:
public function update($id)
{
$slide = Slide::find($id);
$slide->title = Input::get('title');
$slide->save();
return Redirect::route('admin.slides.index')
->with('message', 'Slide has been updated.')
->with('message-type', 'alert-success');
}
Now the error:
Creating default object from empty value
----- Solution: -----
The problem was with my form as suggested by #lukasgeiter
I changed my form to following at it worked like a charm:
{{ Form::model($slide, array('route' => array('admin.slides.update', $slide->id), 'method' => 'put')) }}
use $slide->save(); instead of $slide->update($data);
to update a model please read the laravel doc here
To update a model, you may retrieve it, change an attribute, and use the save method:
EX :
$user = User::find(1);
$user->email = 'john#foo.com';
$user->save();
The actual problem is not your controller but your form.
It should be this instead:
{{ Form::model($slide, array('route' => array('admin.slides.update', $slide->id), 'method' => 'put')) }}
This mistake causes the controller to receive no id. Then find() yields no result and returns null.
I recommend besides fixing the form you also use findOrFail() which will throw a ModelNotFoundException if no record is found.
$slide = Slide::findOrFail($id);