Trouble getting $id from session in laravel - php

I am trying to pass on the value $id through my href to the next page
Route:
Route::get('/offer-me/{id}', 'OffersController#create')->name('project.detailed');
From View:
<button class="btn btn-success">View</button></p>
When I go to localhost/projectp0/public/offer-me/2 .... There is no $id value?
{"_token":"dSxgM8wTlpNxhDKsqj713KMy656bg8XAU5Q2sqe4","_previous":{"url":"http:\/\/localhost\/projectp0\/public\/auction"},"_flash":{"old":[],"new":[]},"login_web_59ba36addc2b2f9401580f014c7f58ea4e30989d":1}
To get that view i Run:
$data = session()->all();
return($data)

The {id} parameter lives in the request, and not in the session.
It will be available as request()->id.

The Returned Data set is your session data. Session data is not storing the data you send from a Get Method. Get method send Data with the URL,
So create method in Offer Controller should be like this.....
public function create($id){
dd($id);
}
Then You Can see What is the value passed for the $id,

You could use
public function create($id)
{
}
It is better to have a create route use a post route
in which case you should be able to access it through the request
public function create(Request $request)
{
$id = $request->id
}

as #drew010 suggested you in comments, try this way :
<button class="btn btn-success">View</button></p>

Related

url parameter - laravel

our users access our site with a unique parameter on the url. ie http://example.com/hire-agreement?u=unique_param
I've set up a route to a view -
Route::get('/hire-agreement', function () {
return view('hire-agreement');
});
I have 2 questions.
Do I need to add anything else to the Route to allow the parameter to be read in the view?
How do I read this parameter value in the View? Can I use $_GET["name"]) ?
thanks
Craig.
you don't need anything more in the url section. and to use or get url parameter use laravel request() helper.
$value = request('key');
in view you can print a key like
{{ request('name') }}
complete example for you using request helper
Route::get('/hire-agreement', function () {
$name = request('name'); //put the key in a variable
return view('hire-agreement', compact('name')); //send the variable to the view
});
and then in view you can use the variable as
{{ $name }}
if you don't want to use a variable you can use directly request helper in view
{{ request('name') }}
you can use Request class too.
Route::get('/hire-agreement', function (Request $request) {
$name = $request->name;
return view('hire-agreement', compact('name'));
});
however i would suggest you to use a controller. don't use closure in route file. u can't cache them when needed.
http://example.com/hire-agreement?u=unique_param
in laravel you can access both post and get can be access by Request class instance or request() helper so you can do
with helper request()
Route::get('/hire-agreement', function () {
dd(request('u')) // this getting from url ?u=unique_param this u param
return view('hire-agreement');
});
with Class Request
or
Route::get('/hire-agreement', function (Request $request) {
dd($request->u)) // this getting from url ?u=unique_param this u param
return view('hire-agreement');
});
here you can
You better pass the request to a controller and handle it there, it's easier and cleaner that way.however if you want to got straight from route to view, you better use the below method.
put this in your route file
Route::get('/hire-agreement/{param}', function ($param) {
return view('hire-agreement')->with($param);
});
in the view you can access the param like this
<p>{{$param}}</p>
now if user request "/hire-agreement/1234" your $param in the view will contain 1234, Also if you would like to access get parameters in the url you can do it like this
{{Request::input('q')}}

Redirect and pass data from controller

I want to redirect from a controller and pass data;
public function fortest(Request $request)
{
$user = DB::table('user2s')->where('name', $request->name)->first();
if (isset($user))
{
return redirect('/fortest2', ['user'=>$user]);//compact('user'));
//return $this->fortest2($request);
}
}
public function fortest2(Request $request)
{
return $request->name;
}
Route::get('/fortest', 'UserController#fortest');
Route::get('/fortest2/', 'UserController#fortest2');
The code works when calling the controller directly from within the controller. The data type has a model. How can I accomplish this?
If you want to pass data in a redirect, you can use the with() method.
You have to append it to the redirect like so:
redirect('/fortest2')->with('data', 'value');
It will be saved in your current session, so it will be only persistent until you refresh the page again. If you want to store it for longer you have to go with a database/textfile etc. You can then check for it using
if (session()->has('data')) { // check if it exists
$value = session('data'); // to retrieve value
}
You can also send errors with the redirect (from validation i.e.) using withErrors(), sending the current input with it using withInput()
For what you want to achieve, try using this in your controller. This will just send the users name with the redirect:
$user = DB::table('user2s')->where('name', $request->name)->first();
redirect('/fortest2')->with('username', $user->name);
You can then access is via session('username')
You need to use sessions to pass data when using redirect:
return redirect('/fortest2')->with('data', 'some data');
Then get data from session:
$data = session('data');
Or you can persist data in DB and then get it from there.
Try to do like this
public function fortest(Request $request)
{
$user = DB::table('user2s')->where('name', $request->name)->first();
if(isset($user))
{
return redirect('/fortest2/$user->name');
}
}
public function fortest2($name)
{
return $name;
}
Your route
Route::get('/fortest', 'UserController#fortest');
Route::get('/fortest2/{$name}', 'UserController#fortest2');

Laravel 5.2.32: Session flash doesn't work with redirect

I have a controller which has a redirect function:
public function myControllerMethod()
{
$data = $this->blabla();
return Redirect::to('previousroute')->with('data', $data);
}
This previousroute is handled by otherControllerMethod() like so:
public function otherControllerMethod()
{
$data = Session::get('data');
return $this->makeView($data);
}
Unfortunately, Laravel forgets this session data. I've done this many times before and I have never seen if forget the session flash data after a single redirect. What is going on here? I have tried both adding and removing "web" middleware but nothing works. If anyone knows why this happens let me know.
use Session;
public function myControllerMethod()
{
$data = $this->blabla();
Session::set('data', $data);
return Redirect::to('previousroute')->with('data', $data);
}
public function otherControllerMethod()
{
$data = Session::get('data');
return $this->makeView($data);
}
Try like this. Use the session and set the data in session and get it from where you want.
I have had the same Issue before. Basically I needed to call send function when redirecting using Redirect facade. So you need to change your myControllerMethod to:
public function myControllerMethod()
{
$data = $this->blabla();
return Redirect::to('previousroute')->with('data', $data)->send();
}
As send() function of class Symfony\Component\HttpFoundation\Response calls the function sendContent() which sends the data when redirecting.
Hope this helps.
In myControllerMethod you are passing the data obj/var as a Request.
In otherControllerMethod you are requesting the Session data which is not set.
In order to put data to the session you should do:
Session::put('data','value')
and then it will be available with:
Session::get('data');

Sending data along with a redirect function to a another function in controller with CodeIgniter

I want to send data with a redirect function to another function within a controller here are my codes
1.
function index($id) {
redirect("/names/particular/", $id, 'refresh');
}
2.
function particular($id){
$fno=$this->uri->segment(3);
$this->load->model('names_rank');
$this->data["names"]=$this->names_rank->get_particular($id);
$this->load->view("view/details", $this->data);
}
I want to redirect with $id from function index in 1 to function particular within controller with function shown in 2..
I need help please
Why don't you better pass it through session or flash data.
//to set a flash data
$this->session->set_flashdata('id',$id);
redirect('/names/particular/') ;
//to access this flash data
$data = $this->session->flashdata('id');
But if you wish to send using redirect method then you can give this a try.
$id=1;
redirect(base_url()."/names/particular/".$id);
you can excess by any method as :
$id = $this->uri->segment(3);
You should put dot to concatinate with $id like this: redirect("names/particular/".$id, 'refresh');
And i'm not sure about 'refresh',if it belongs there at all!

Laravel pass url parameter to controller

My current url I'm on is localhost/test/1
When I click on save button, it's going to call a rest service test/{id}/createNewTest. How do I get the {id} in the controller? Right now it's getting nothing.
Routes
Route::post('test/{id}/createNewTest', 'TestController#createNewTest');
Controller
public function createNewTest() {
$id = Request::input('id');
}
the $id is suppose to be 1 here, but it's getting nothing
Route parameters are passed into the controller via method parameters, not as request input:
public function createNewTest($id) {
var_dump($id); // from method parameter, not Request::input()
}

Categories