Laravel - set session from form input - php

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

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

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

PUT method throws BadMethodCallException

I'm learning Laravel from Lacast and i'm trying to create a CRUD application.I've implemented the index,show,create and store correctly,but with the edit form when i try to submit data it's throwing BadMethodCallException.
Here are my routes
Route::get('/projects','ProjectsController#index');
Route::get('/projects/{id}','ProjectsController#show')->where('id','[0-9]+');
Route::get('/create','ProjectsController#create');
Route::post('/projects','ProjectsController#store');
Route::get('/projects/{id}/edit','ProjectsController#edit')->where('id','[0-9]+');
Route::put('/projects/{id}','ProjectsController#update')->where('id','[0-9]+');
Route::delete('/projects/{id}','ProjectsController#destroy');
Here is the edit form:
#extends('template');
#section('content')
<h2>Create new project</h2>
<p>/projects/{{ $project->id }}</p>
<form method="POST" action="/projects/{{ $project->id }}">
{{ method_field('PUT') }}
{{ csrf_field() }}
<div>
<input value="{{ $project->title }}" type="text" name="title" id="title" placeholder="Project title">
</div>
<div>
<textarea name="description" placeholder="Enter the project description">{{ $project->description }}</textarea>
</div>
<div>
<button type="submit">Update project</button>
</div>
</form>
#endsection
The controller code:
public function edit($id){
$project= Project::find($id);
return view('projects.edit',compact('project'));
}
public function update($id){
$project= Project::find($id);
$project->title=request('title');
$project->description('description');
$project->save();
return redirect('/projects');
}
The edit form displays as expected with data coming from the database,after submit i get the following error page:
With the example of the instructor the code has worked perfectly,and before using the PUT either on my form and in my controller i used PATCH like the instructor but always the same result.
Here is the instructor video link: Faking PATCH and DELETE Requests
The error is pretty straightforward.
public function update($id){
$project= Project::find($id);
$project->title=request('title');
$project->description('description'); // You don't have a method description()
$project->save();
return redirect('/projects');
}
This is what you probably meant to do:
public function update($id)
{
$project= Project::find($id);
$project->title = request('title');
$project->description = request('description');
$project->save();
return redirect('/projects');
}

store in table, Laravel

I have a problem to store data in a table. And after hours I still couldnt figure out, what the problem is. I would be so happy for some help!
WishlistController.php:
public function store($book_id)
{
$user_id=Auth::id();
$wishlist=new Wishlist;
$wishlist->book_id=$book_id;
$wishlist->user_id= $user_id;
$wishlist->save();
return redirect()->route('wishlistCRUD.show' , $book_id->id)
->with('success', 'Buch gewünscht');
The Model:
class Wishlist extends Model
{
public $table = 'wishlist';
public $fillable = ['book_id','user_id',];
the view.blade:
{!! Form::open(array('route' => 'wishlistCRUD.store', 'method'=>'POST')) !!}
<form action="someaction" method="POST">
<input type="hidden" name="book_id" value="{{$book->id}}"/>
</form>
<a class="btn btn-primary" href="{{ route('wishlistCRUD.store',$book->id) }}">wünschen</a>
{!! Form::close() !!}
the Route:
Route::post('wishlistCRUD.store', 'WishlistController#store');
When I check the table, nothing new is added.
Its frustrating :-(
Just change post to get in this line:
Route::post('wishlistCRUD.store', 'wishlistCRUD#store');
Try to do this. In your Controller, you don't need $book_id as a parameter because you can get it from the $request:
public function store(Request $request)
{
$user_id = Auth::id();
$book_id = $request->book_id;
$wishlist = new Wishlist;
$wishlist->book_id = $book_id;
$wishlist->user_id = $user_id;
$wishlist->save();
return redirect()->route('wishlistCRUD.show' , $book_id)
->with('success', 'Buch gewünscht');
}
Don't forget to add use Illuminate\Http\Request;
And then in your blade, you don't need to set a route in an anchor, because it is already defined in the form. And you don't need two forms, because Form::open already does that:
{!! Form::open(array('route' => 'wishlistCRUD.store', 'method'=>'POST')) !!}
{{ csrf_field() }}
<input type="hidden" name="book_id" value="{{$book->id}}"/>
{!! Form::submit('wünschen') !!}
{!! Form::close() !!}
This should work.
You have created POST method in your web.php for adding book to wish list, in your blade template you need to submit hidden form on click of link.
in your web.php
Route::get('wishlistCRUD/book/{book_id}','WishlistController#get_book_by_id')->name('get_book_by_id');
Route::post('wishlistCRUD.store', 'WishlistController#store')->name('store');
In your blade template
<a class="btn btn-primary" href="javascript:void(0)" onclick="event.preventDefault();document.getElementById('addToWishlist').submit();">wünschen</a>
<form style="display:none" id="addToWishlist" method="POST" action="{{ route('store')}}">{{csrf_field()}} <input type="hidden" name="bookid" value="{{$book->id}}" /> </form>
in your controller
public function store(Request $request){
$bookID= $request->input('bookid');
$wishlist=new Wishlist();
$wishlist->book_id= $bookID
$wishlist->user_id= Auth::user()->id;
$wishlist->save();
return redirect()->route('get_book_by_id', ['book_id' => $bookID]);
}
public function get_book_by_id(Request $request,$book_id){
// find book by ID;
$book=Book::find($book_id);
// book found
if($book){
return view('book')->with('book',$book);
}else{
// book not found , redirect to 404 page or home page
return redirect('/');
}
}
In blade, change the
<a class="btn btn-primary" href="{{ route('wishlistCRUD.store',$book->id) }}">wünschen</a>
to a <button class="btn btn-primary" type="submit">wünschen</button>
Delete the tag form, it's not necesary. You declared it with the {!! form:open... !!}
You are trying to access to route('wishlistCRUD.store',$book->id) by GET with the <a></a>
Try this:
the view.blade:
{!! Form::open(['url' => 'wishlistCRUD/store', 'method' => 'POST']) !!}
<input type="hidden" name="book_id" value="{{$book->id}}"/>
<button class="btn btn-primary" type="submit">wünschen</button>
{!! Form::close() !!}
the Route:
Route::post('wishlistCRUD/store', 'WishlistController#store');

Post Method request not found

I'm having a problem with my code now and it seems like that the route that I specify is not found whenever I try to access it.
Route:
Route::post('nniscaseassociates/pushreliever/{id}', 'NnisCaseAssociateController#pushreliever');
Route::get('nniscaseassociates/{id}/reliever', 'NnisCaseAssociateController#reliever');
View:
<form method="POST" action="/nniscaseassociates/pushreliever/{{ $caseassociate->nnis_case_id }}">
{{ method_field('PUT') }}
{{ csrf_field() }}
<...Fields...>
</form>
Controller:
public function reliever($id)
{
//this will be nniscase id then
$caseassociate = NnisCaseAssociate::findOrFail($id);
//return dd($caseassociate);
return view('nniscaseassociates.reliever', compact('caseassociate'));
}
public function pushreliever(Request $request, $id)
{
...Statements...
return redirect('nniscases/'.$caseassociates->nnis_case_id.'/edit');
}
By the end of submitting I want to redirect to my edit page and display the changes that I made from the previous form. And this is the error that I got when click submit.
You are making a PUT request and not POST.
Either remove this line from your form:
{{ method_field('PUT') }}
Or
Change your POST route to PUT:
Route::put
You have not defined PUT method in your web.php
Route::put('nniscaseassociates/pushreliever/{id}', 'NnisCaseAssociateController#pushreliever');
in your view :
<form method="POST" action="/nniscaseassociates/pushreliever/{{ $caseassociate->nnis_case_id }}">
{{ method_field('PUT') }}
{{ csrf_field() }}
<...Fields...>
</form>
in your controller :
public function pushreliever(Request $request, $id)
{
echo $id;
}
You should try this:
<form method="POST" action="{{ url('nniscaseassociates/pushreliever',[$caseassociate->nnis_case_id]) }}">

Categories