How to pass path variable in thymeleaf to display data from connected table - spring

I have two tables: User and Wallet. This is how Wallet looks like:
As you can see I can assignee wallet to user.
Now, I want to create a controller that will show initial_balance and wallet_name in HTML.
This is how my controller now looks:
#GetMapping("/userWallet/balance/{user_id}")
public String getUserWallet(#PathVariable(value = "user_id") Long user_id, Model model) {
model.addAttribute("wallet", walletService.findByUserId(user_id));
return "userProfile";
}
And that works fine, when I go to that URL and pass user_id I get his initialBalance, but I need to go to that URL every time manually.
So, I tried to create this in HTML:
<div th:each="wallet : ${wallet}">
<a th:href="#{/api/wallet/userWallet/balance/{user_id}(user_id = ${wallet.id})}">
<span th:text="${wallet.initialBalance}"></span>
</a>
</div>
But it still wont to show me initialBalance of current user on page. Any help?

Related

how to display data based on specific id in laravel?

I am new to laravel. I want to send data as id of existing data. Id comes from products.blade I send via href tag as shown below to gallery page. I have tried to find a way through other sites but it still doesn't work
<a class="btn btn-success" href="/dashboard/galleries/{{ $product->id }}"><i class="ri-image-add-line text-white"></i></a>
then i create a route like this
Route::resource('/dashboard/galleries', DashboardGalleryController::class)->middleware('admin')->shallow();
in the controller gallery, I made like this
public function index($id)
{
$gallery = Gallery::where('products_id', $id)->get();
return view('dashboard.galleries.index', compact('gallery'));
}
then inside the gallery page, I want to display a table to add images based on that id.
dashboard/galleries/index.blade.php
<h1>{{ $gallery->id }}</h1>
should i add data inside foreach or outside?
A restful resource controller sets up some default routes for you and even names them.
and ur case the logic should be inside show() function not index()
check this issue it will help u same issue solved here

how to hide id from URL laravel 7?

i have this url :
http://127.0.0.1:8000/deliverer/4
i want it like this
http://127.0.0.1:8000/deliverer/
this is my function :
public function show($id)
{
// $userhash->hashids->encode($id);
$user=User::find($id);
return view('deliverer.profile')->with('user',$user);
}
and this is my route
Route::get('deliverer/{id}', 'deliverer\DelivererController#show')->name('profile');
and this in view
<a href="http://127.0.0.1:8000/deliverer/{{ Auth::user()->id }}" >
<i class="nc-icon nc-single-02"></i>
<p>Profile</p>
</a>
Assuming, that what you're trying to accomplish is to view the current authenticated user's profile, then you should follow the steps below:
First you have to modify the route and remove the {id} from the URL, like this:
Route::get('deliverer', 'deliverer\DelivererController#show')->name('profile');
Then, inside the controller you have to remove the $id param from the show() method and change the method to get the id from the authenticated user.
public function show($id)
{
// $userhash->hashids->encode($id);
$user = \Auth::user();
return view('deliverer.profile')->with('user',$user);
}
And of course, you have to remove the Auth::user()->id() from the view route, and perhaps use the named route instead of hardcoding it, like so:
<a href="{{ route('profile') }}">
<i class="nc-icon nc-single-02"></i>
<p>Profile</p>
</a>
I'm assuming you're just trying to hide id's so users can guess the next number or try to pull up records they shouldn't.
Have you looked into UUID's? Example here: (https://dev.to/wilburpowery/easily-use-uuids-in-laravel-45be) That might be a solution.
Also, if you are worried that someone might tamper with URL to pull records, you should look into securing up your models. Do a check to see if the user should have access to that particular record. Many ways to accomplish that.
You can use token or configure a middleware, or as you did you can hash or crypt the id and make the verification after the call
The url will be like that :
http://127.0.0.1:8000/deliverer/?id=aze45a8sd54q

I am using auth function in laravel 7

I have two models users and user_profiles
user
id name email
user_prfiles
id user_id fname lname address
this is a one to one relation concept, my doubt is, I add user_profiles details in my table and user table is also good but again the same person login he can view the link of create details which will create user_profiles details of particular user. and my point is when a user fill all of his user profile
the link should not display how can I do that? how to hide the link once the data created? if anyone knows please share the answer..
Can you do something like this?
Code
class User extends Model
{
public function userProfile()
{
$this->hasOne('App\UserProfile');
}
}
class UserProfile extends Model
{
isComplete()
{
// check if all fields is what you expect
}
}
In your view
<h1>User dashboard</h1>
#if(!$user->userProfile || !$user->userProfile->isComplete())
Fill in your profile
#endif
finally I found the answer, in my create page
#if(!Auth::user()->userprofile)
<form>
</form>
#else
<div>already created</div>
#endif
Trying to get property 'userprofile' of non-object (View: E:\Xampp\htdocs\Projects\laravel\Interview\resources\views\layouts\app.blade.php)
I get this error when I did as you say..
#if(!Auth::user()->userprofile || !Auth::user()->userprofile->isComplete())
<a class="nav-link active text-dark" href="/users/create">Add Details</a>
#endif
this is my app.blade.php
my UserProfile.php
public function isComplete()
{
return $this->hasOne(UserProfile::class);
}

Most efficient way to get a specific model from a collection in BLADE in Laravel

Hi im developing a site using Laravel and have some site data id like to keep track of such as facebook url, instagram url, address, phone number, etc. This information for this model is stored as a name and value in the DB, so example name:facebook and value:[facebook url here]. I know that i can retrieve all the models and get a collection, pass it to my view and loop through them, but id like a little more control to get specific models to use it where i need them on specific parts of the page. This is what I currently have it:
I have a model SiteMeta which i pass an instance to my view:
$site_meta = new \App\SiteMeta();
return view($page_string)->with('site_meta',$site_meta);
And in my SiteMeta Model:
public static function get($name)
{
return SiteMeta::where('name', '=', $name)->firstOrFail();
}
And then in my view im getting my specific model by its name:
<a target="_blank" href="{{$site_meta::get('Facebook')->value}}">
<i class="fa fa-facebook-official data-icon" aria-hidden="true"></i>
</a>
So my question is, is this the most efficient way to do it? Im doing this in various places on the contact page to display information like phone number and address and other site information which i feel like is a bit overload as it make a request to the database each time im doing sitemeta::get('name');
Is there a better way to get a specific one from the collection in BLADE? Thanks
I would pass it as an array from your controller method:
$site_meta = \App\SiteMeta::lists('value', 'name');
return view($page_string, compact('site_meta');
On your blade template you could do this:
$site_meta['Facebook'];

Laravel - get id from the right database record

I have laravel project. When I click my view button, I want to see full description of my record. But I don't know how to pass the right id. My database table is called - csstable.
I have model:
<?php
class CssTable extends Eloquent
{
protected $table = 'csstable';
}
View button on each post (I get all of my posts from database, so each of them have id):
<div class="view">
<a href="{{ action('CssController#show') }}" ></a>
</div>
CssController with this show function:
public function show()
{
$csstable = CssTable::all();
return View::make('cssposts/right_post', compact('csstable'));
}
My Route:
Route::get('/css/id_of_right_post', 'CssController#show' );
Right_post, where I want information from description column from row, with id, that i clicked (In this field, I see just last record's description:
<h1 style="color:#fff">{{ $css->description }}</h1>
I have tried to put something like this
public function show($id)
{
$csstable = CssTable::find($id);
return View::make('cssposts/right_post', compact('csstable'));
}
But then there is an error - missing 1 argument in show function. So I want to know, how to pass correct id!
The way to do this involves three steps. First let's go for the route:
Route::get('css/{id}', 'CssController#show');
The {id} there means it's a matching parameter - it'll match a full URI segment (basically anything between slashes) and use that to pass into he method passed. So on to the controller:
class CssController
{
public function show($id)
{
$csstable = CssTable::findOrFail($id);
return View::make('cssposts/view', compact('csstable));
}
}
That controller method accepts a (required) single parameter. You can call it whatever you want, but here I'm going for id as it's an ID for a model. Finally, the last part of the puzzle is how to link to such a route:
// view button for each csstable
<div class="view">
{{ link_to_action('CssController#show', $item->title, array($item->getKey())) }}
</div>
As you can see, I'm using the link_to_action helper, but your method with <a href="{{{ action('CssController#show', array($item->getKey())) }}}"> will work too. After the controller action name, you pass an array that contains all of the parameters in the URI to fill in (in order). In our case we have one parameter, to it's an array with one item. I think in these cases you could also use a string and Laravel will turn it into an array with one element for you. I prefer to be explicit.
Hopefully that's helped you work out how to use the parameter-based routing system in Laravel.

Resources