Route not found laravel - php

I'm getting an error 404|Not found , while the route is existing and the debugger is on I have cleared route cachebut still showing the error, when I php artisan route:list it shows POST | products/{product}/favourites | product.fav.store | App\Http\Controllers\HomeController#store How can I solve this?
controller
public function store(Request $request, Product $product)
{
$request->user()->favouriteProducts()->syncWithoutDetaching([$product->id]);
return back();
}
Blade
<span class="pull-right">
Add to Fav
<form id="product-fav-form" class="hidden" action=" {{route('product.fav.store', $product) }}" method="POST">
{{ csrf_field()}}
</form>
</span>
Route
Route::post('/products/{product}/favourites', 'HomeController#store')->name('product.fav.store');

Try this ,
<form method="POST" action="{{ route('product.fav.store', ['product' => $product->id]) }}">
Change the controller function
public function store(Request $request,$product)
{
$request->user()->favouriteProducts()->syncWithoutDetaching($product);
return back();
}

You should change your route like.
<form action="{{ route('product.fav.store', ['product' => $product->id]) }}" >

Try this :
<form id="product-fav-form" class="hidden" action="{{route('product.fav.store', $product->name)}}" method="POST">
controller :
public function store(Request $request, $name) {
$request->user()->favouriteProducts()->syncWithoutDetaching($name);
return back();
}

Related

How to define a variable of global in laravel?

I have a table of slider-groups like this and I'm on this page right now or in address. http://localhost:8000/admin/slider-groups
When I click on this +, it redirects me to this address, http://localhost:8000/admin/slider-groups/1/sliders or in these tables
web.php
Route::resource('admin/slider-groups', 'Admin\SliderGroupController');
Route::prefix('admin/slider-groups/{sliderGroup}')->group(function(){
Route::resource('admin/sliders', 'Admin\SliderController');
});
SliderGroupController.php
public function index(Request $request)
{
$sliderGroups = SliderGroup::all();
return view('admin.groups.index', compact('sliderGroups'));
}
admin.groups.index.blade.php
#foreach($sliderGroups as $sliderGroup)
...
<a href="{{ route('admin::sliders.index', $sliderGroup->id) }}" class="mr-1">
<i class="la la-plus text-grey text-shadow-custom font-medium-4 font-weight-normal"></i>
</a>
#endforeach
SliderController.php
public function edit($sliderGroupId,Slider $slider)
{
return view('admin.sliders.edit', compact('slider', 'sliderGroupId'));
}
admin.sliders.edit.blade.php
<form method="post" action="{{ route('admin::sliders.update', [$sliderGroupId,$slider->id ]) }}" enctype="multipart/form-data">
#csrf
#method('PUT')
...
SliderController.php
public function update(Request $request, Slider $slider, $sliderGroupId)
{
dd($sliderGroupId);
}
I get this error.
Argument 2 passed to Modules\Slider\Http\Controllers\Admin\SliderController::update() must be an instance of Modules\Slider\Entities\Slider, string given,
Update second parameter to the route function in admin.sliders.edit.blade.php as follows:
<form method="post" action="{{ route('admin::sliders.update', ['slider' => $slider, 'sliderGroupId' => $sliderGroupId]) }}" enctype="multipart/form-data">
#csrf
#method('PUT')
...

PUT method not supported for the route when trying to do an update method

This is what my code looks like
Route:
Route::put('/articles/{$article}', 'ArticlesController#update');
Controller:
public function update($id){
$article=Article::find($id);
$article ->title = request('title');
$article->excerpt=request('excerpt');
$article->body=request('body');
$article->save();
return redirect('/articles/'. $article->id);
}
Blade:
<form method="POST" action="/articles/{{$article->id}}" >
#csrf
#method('PUT')
And everytime I try to submit an update I get this:
The PATCH method is not supported for this route. Supported methods: GET, HEAD.
I'm currently stuck on this one.
Try this
<form action="/articles/{{$article->id}}" method="POST">
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
and the route
Route::put('/articles/{article}', 'ArticlesController#update');
It's better to do:
in routing
Route::put('/articles/{id}', 'ArticlesController#update')->name('articles.update');
in controller
public function update(Request $request, $id)
{
// logic
}
don't forget to use Request in controller
in blade
it's better to use naming for routes but it might be a problem in your action
<form method="POST" action="{{ route('articles.update', $article->id) }}">
simple way using blade
<form action="/articles/{{$article->id}}" method="POST">
#method('put')
#csrf
</form>

Laravel - set session from form input

I cannot display session value in my view. I only want to display it to see if it's correctly set inside the controller. Is it correctly set in controller? How can I check?
I have this in view:
<div class="panel panel-success">
<form action="{{ route('get_table') }}" method="POST">
{{ csrf_field() }}
#foreach($tables as $data)
<button type="submit" name="tables" class="btn btn-primary" value="{{ $data->id }}">
{{$data->name }}
</button>
#endforeach
</form>
{{ Session::get('table_id') }}
</div>
This in ListController:
public function index() {
$tables = DB::table('tables')->get();
return view('welcome', compact('tables'));
}
public function getTable(Request $request) {
$table_id = $request->get('tables');
$request->session()->put('table_id', $table_id);
}
And this in web.php routes:
Route::get('/', 'ListController#index')->name('get_table');
Route::post('/', 'ListController#getTable');
I even tried
public function getTable(Request $request) {
$request->session()->put('table_id', '1234');
}
Nothing shows up in the view at {{ Session::get('table_id') }}
What am I doing wrong?
You can try this:
public function getTable(Request $request) {
$table_id = $request->get('tables');
return redirect()->back()->with('table_id',$table_id);
}
if you want to redirect to specific route then:
public function getTable(Request $request) {
$table_id = $request->get('tables');
return redirect()->route('RouteName')->with('table_id',$table_id);
}
and then in view:
#if(Session::has('table_id'))
{{ Session::get('table_id') }}
#endif
Hope you got your answer

Error while editing a post using Laravel update

This is my html form
<form class="form-horizontal" action="{{action('BlogController#update',[$blog->id]) }}" method="post">
<input name="method" type="hidden" value="patch"/>
<div class="form-group">
<input name="_token" type="hidden" value="{{ csrf_token() }}"/>
Here is route:
Route::patch('blog/{id}','BlogController#update');
Controller :
public function update(Request $request,$id){
$input = $request->all();
$blog =findOrFail($id);
Blog::update($input);
//var_dump($input);
return back();
}
Can you please show me where is the issue?
In your code you have write $blog = findOrFail($id); to get blog which is not correct. You can do it using
$blog = Blog::findOrFail($id);
Now you have the blog, you need to update the blog. So, the update code should be
$blog->update($input);
To make this update method works, you need to make the fields(the fields you are updating) fillable in Blog model.
You're using the wrong syntax. Do something like this to make it work:
public function update(Request $request, $id)
{
Blog::where('id', $id)->update($request->all());
return back();
}
give the name whatever you wish say blog:
Route::patch('blog/{id}','BlogController#update')->name('blog');
your HTML code
<form class="form-horizontal" action="{{route('blog', $blog->id)}}" method="post">
hope this help you!!
you have many syntax problems!
try it this way:
form:
<form class="form-horizontal"
action="{{ route('blog.update', ['id' => $blog->id]) }}"
method="post">
{{ csrf_field() }}
<input name="_method" type="hidden" value="patch"/>
<!-- other inputs -->
</form>
Route:
Route::any('blog/{id}','BlogController#update')->name('blog.update');
Controller:
public function update(Request $request, $id){
$blog = Blog::findOrFail($id);
$blog->update([
'key' => 'value'
]);
// never use $request->all() because of security issues!
return back();
}
<form class="form-horizontal" action="{{route('blog.update',[$blog->id]) }}" method="post">
{{csrf_field()}}
{{ method_field('PATCH') }}
Your Route Like This
Route::resource('blog', 'BlogController');
Your Controller
public function update(Request $request,$id){
$blog =Blog::findOrFail($id);
$blog->database_fieldname1=$request->value1;
$blog->database_fieldname2=$request->value2;
$blog->database_fieldname3=$request->value3;
$blog->save();
return back();
}

how to print the database content on a desired page?

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'
]);

Categories