How to define a variable of global in laravel? - php

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')
...

Related

Too few arguments to function App\Http\Controllers\AdminController::updateSlider(), 1 passed in.... on line 54 and exactly 2 expected - laravel

I got an error
Too few arguments to function App\Http\Controllers\AdminController::updateSlider(), 1 passed in E:\localhost\JMS\vendor\laravel\framework\src\Illuminate\Routing\Controller.php on line 54 and exactly 2 expected
Controller
public function updateSlider(Request $request,$id)
{
$this->validate(request(),[
//put fields to be validated here
]);
$oldImage=DB::table('slider')->where('id',$id)->first();
$oldFile=$oldImage->image;
$task=array();
$current_date_time = date('Y-m-d H:i:s');
$task['updated_at']=$current_date_time;
$file=$request->file('image');
$file_name=hexdec(uniqid());
$ext=strtolower($file->getClientOriginalExtension());
$file_full_name=$file_name.'.'.$ext;
$upload_path='Gallery/';
$file_url=$upload_path.$file_full_name;
$success=$file->move($upload_path,$file_full_name);
$task['photo']=$file_url;
$data=DB::table('slider')->update($task);
if ($data) {
unlink($oldFile);
return back()->with('success', 'Photo Has Been Saved Successfully.');
}
}
Here is the form
<form action="{{ url('admin/slider/update/.$data->id') }}" method="POST"enctype="multipart/form-data">
#csrf
<input type="file" name="image" class="custom-file-input" >
<button type="submit"class="btn btn-primary">Update </i></button>
</form>
Instead of:
<form action="{{ url('admin/slider/update/.$data->id') }}" blabla>
Try this:
<form action="/admin/slider/update/{{ $data->id }}" blabla>

The DELETE method is not supported for this route. Supported methods: GET, HEAD, POST is not working

I want to delete bookings this is my form
<form action="{{ route('bookings.delete', $booking->id) }}" method="POST">
#csrf
#method('delete')
<button type="submit" class="btn btn-outline-danger">Delete</button>
</form>
this is my controller
public function delete($id){
$booking = Booking::find($id);
$booking->delete();
}
and this is my route
Route::post('/bookings/delete', 'BookingController#delete')->name('bookings/delete');
Your route should be defined as
Route::delete('/bookings/delete/{id}', 'BookingController#delete')->name('bookings.delete');
You should also pass the id as a route parameter
In the delete method of your controller
public function delete($id)
{
$booking = Booking::findOrFail($id);
$booking->delete();
return redirect()->route('bookings.index');
}
In your route file
name('bookings/delete');
Should be
name('bookings.delete');
Try it and let us know
if you need to use #method('delete') then your route should be delete() not post
Route::delete('/bookings/delete/{id}', 'BookingController#delete')->name('bookings.delete');
And form:
<form action="{{ route('bookings.delete', $booking->id) }}" method="POST">
#csrf
#method('delete')
<button type="submit" class="btn btn-outline-danger">Delete</button>
</form>

Route not found laravel

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();
}

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();
}

Categories