How to allow $user variable in a blade view page in Laravel? - php

I have this page support.blade.php in views folder and I'm using Laravel.
I can't echo out $user->id or $user[id] in this page and I get undefined variable error. It's possible to echo it out in other pages, like in tickets.blade.php but in this specific page it's not working.
How can I fix this?

Within your Service Provider you can use
View::share('key', 'value')
You can consult the docs for a more indepth explanation.

You need to pass the data first from controller method:
return view('support', ['user' => $user]);
If you want to display ID of an authenticated user, you can do this from any part of the app without passing data from controller method:
{{ auth()->user()->id }}

Did you pass data in view file??
return view('support', ['user' => $user]);

Related

laravel if request is username in blade

I have simple URL /players/USERNAME to view user profile. I want to insert some thing only in user profile page. Cuz user profile got another links.
Example:
#if (Request::is('players/{{ $user->slug }}'))
#include('users::stats')
#endif
But dont get anything. Any ideas what's wrong? Thanks!
I don't think Request::is is necessary in your blade view. You should have a route defined in your web.php file to capture that pattern meaning you already know the end user is on players/{username}.
web.php
Route::get('/players/{slug}', [
PlayerController::class, 'show'
])->name('players.show');
PlayerController.php
public function show($slug)
{
$user = User::where('username', $slug')->firstOrFail();
return view('players.show', compact('user'));
}
Blade view (players/show.blade.php)
// some stuff
#include('users.stats')
// some other stuff
Note that I've named the methods and files based on what you've provided, you'll need to change them to be whatever they are in your project.

Laravel. conflict with routes

I have a problemwith my routes. When I call 'editPolicy' I dont know what execute but is not method editPolicy. I think I have got problem beteweeb this two routes:
My web.php ##
Route::get('admin/edit/{user_id}', 'PolicyController#listPolicy')->name('listPolicy');
Route::put('/admin/edit/{policy_id}','PolicyController#editPolicy')->name('editPolicy');
I call listPolicy route in all.blade.php view like this:
{{ $user->name }}
And call editPolicy route in edit.blade.php view like this:
Remove</td>
My PolicyController.php is:
public function listPolicy($user_id)
{
$policies = Policy::where('user_id', $user_id)->get();
return view('admin/edit',compact('policies'));
}
public function editPolicy($policy_id)
{
dd($policy_id);
}
But I dont know what happend when I call editPolicy route but editPolicy method not executing.
Any help please?
Best regards
Clicking an anchor will always trigger a GET request.
route('listPolicy', $user->id) and route('editPolicy', $policy->id) will both return admin/edit/{an_id} so when you click your anchor, listPolicy will be executed. If you want to call editPolicy, you have to send a PUT request via a form, as defined when you declared your route with Route::put.
Quick note, your two routes have the same URL but seem to do very different things, you should differentiate them to avoid disarray. It's ok to have multiple routes with the same url if they have an impact on the same resource and different methods. For example for showing, deleting or updating the same resource.
Have a look at the documentation.

How to access cookie from view in PHP Laravel 5

I can access cookie in controller then pass it to view
//HomeController.php
public function index(Request $request)
{
$name = Cookie::get('name');
return view('index', ['name'=> $name]);
}
But I want to write a small control (widget) that can fetch data from cookie without concern of parent controller. For example, header, footer widgets could fetch its own data without main page controller knowing which data is needed.
I can query the data from database by using View Composer. But, how can I access data from view in the request cookie ?
Using static function with defining namespace and etc is a not safe.
Cuz maybe in next versions of framework this namespace can change.
It's better to use helper functions.
{{ request()->cookie('laravel_session') }}
or
{{ cookie('laravel_session') }}
Tested on working app with Laravel 5.2
You can use {{ Cookie::get('laravel_session') }} to print out the cookie inside your view.
You can use
{{\Illuminate\Support\Facades\Cookie::get('laravel_session')}}
in a blade template
You can use:
$response = new \Illuminate\Http\Response(view('your_view'));
$response->withCookie(cookie('cookieName' , 'cookieValue' , expire));
return $response;
or
\Cookie::queue('cookieName', 'cookieValue', expire);
return view('your_view');
I've used both of them.

return back() in Laravel without session params

I have this code in a project with Laravel 5:
return back()->with('msg_ok','successfully sent');
The param msg_ok is pushed in Session, but I´m not want use session params, I want pass the msg_ok parameter as variable.
For example I want print this en my blade file:
{{ $msg_ok }}
Laravel back() function will always return data in a Session. Normally you can't return variable with back() function. You have to use view() function for that.
Alternate Solution
You can use keep() function for storing data in session like variable. Which will not be flushed after refresh.
e.g
$request->session()->keep(['username', 'email']);
and then get data with key.
Laravel back() will redirect you back from the form where it is submitted,
and back()->with('msg_ok', 'successfully..');
if you place an item inside back()->with(); it will automatically place the value
inside session.
You can use return redirect()->to('url/path?msg_ok=successfully') add the query string parameter to url to pass it as variable.
and then on the controller where you have been redirected inject the Reqest class to your method
e.g
public function index(Request $request) {
$msg_ok = $request->get('msg_ok', '');
return view('view.blade', compact('msg_ok'));
}
Place the variable string inside the compact to make it accessible to your view.
You can just use Session::get('msg_ok') in your blade file. Or pass the variable to the view explicitly, in controller, like
return view()->make('view', ['msg_ok' => session()->get('msg_ok')]);

Laravel3 how to send variable into controller from view

I'm learning laravel 3 at the moment, and I have a technical question. I have a list of authors and I'm trying to make some links to set a filter to order those authors.
I tried setting a default parameters to order by Name, it works, but I can't pass any other filters.
This is my route :
Route::get('authors', array('as'=>'authors', 'uses'=>'authors#index'));
And this is my controller function :
public function get_index($filter="name"){
return View::make('authors.index')
->with('title', 'Authors list')
->with('authors', Author::order_by($filter)->get());
}
And this is the links in my view trying to send the filter I want
{{ HTML::link_to_route('authors', 'Id', array('id')) }}
{{ HTML::link_to_route('authors', 'Name', array('name')) }}
The parameters I try to send (id and name) from the view never reach the controller so it always use default parameter.
Thank you !
You're not setting any route parameters, you must do something like:
Route::get('authors/(:any?)', array('as'=>'authors', 'uses'=>'authors#index'));
Take a look at the docs: http://three.laravel.com/docs/routing#wildcards

Categories