Laravel - Views - router() doesn't work or I just dumb - php

Here is my web.php
Route::get('read/{book_id}/{part_id}', 'BookPartController#show')->name('read');
And this is the Views
<a href="{{ route('read', [$part->book_id, $part->part_id]) }}">
I wanna expect for the result is mysite.com/read/808/121, though the result is this mysite.com/read/808

try this, adding the keys to the array:
<a href="{{ route('read',
['book_id' => $part->book_id, 'part_id' => $part->part_id]) }}">

Correct way to do it:
<a href="{{ route('read', ['book_id' => $part->book_id, 'part_id' => $part->part_id]) }}">

Related

Is it possible to remove variable from symfony url?

/category/{id}/{categoryName}
localhost:8000/category/2/tours-and-travels
localhost:8000/category/tours-and-travels //want to have this url.
Is it possible to remove id as shown above url?
Twig
<a class="nav-link text-white" href="{{ path('category_with_id',{'id': category.id, 'categoryName': category.name|slug('-') }) }}">
{{ category.name }}
</a>
I know we can do this with the help of unique slug, but is it possible with id?

Getting data from database in web routes - Laravel

I have this Laravel application, in which I want to display the name of the logged in user in the pages.. Thing is, I'm getting the data in the web.php file, like this:
Route::get('/', function () {
$welcomeNames = DB::table('users')->get();
return view('welcome', ['welcomeNames' => $welcomeNames]);
});
And then, in my welcome.blade.php, I return the data like this:
#if (!Auth::guest())
#foreach($welcomeNames as $key => $data)
#if ($data->name == Auth::user()->name )
<tr>
<th>{{$data->name}}</th>
</tr>
#endif
#endforeach
<a href="{{ route('logout') }}" onclick="event.preventDefault(); document.getElementById('frm-logout').submit();" class="text-sm text-gray-700 dark:text-gray-500 underline">
Logout
</a>
<form id="frm-logout" action="{{ route('logout') }}" method="POST" style="display: none;">
{{ csrf_field() }}
</form>
#else
#endif
My question is: is this a safe practice? I don't know if getting the data from the DB in my route is safe. Thanks in advance!
It is safe as long as you do not show it somewhere where it doesn't belong. But its bad practice and shouldn't be done if not really needed. AND controllers are way more organised in any way.

Is there a way of getting authenticated user ID and use it on navigation.blade?

I'm building a new Laravel app, using Breeze.
What i'm trying to do is get the authenticated user id to redirect it to the profile route, which is:
Route::group([
'prefix' => 'profile',
'as' => 'profile.',
'middleware' => ['auth']
], function () {
Route::get('/{id}', [ProfileController::class, 'show'])->name('profile');
}
);
But, on the layout of navigation, as shown below, i can't manage to get it on the :href.
I already tried some approaches, like:
:href="{{ route('profile', [Auth::user()->id]) }}"
:href="{{ route('profile',/([Auth::user()->id])) }}"
But none of them seems to work.
The navigation.blade.php dropdown part:
<x-slot name="content">
<x-dropdown-link :href="route('admin.aprovar')">
{{ __('Aprovações') }}
</x-dropdown-link>
<x-dropdown-link>
{{ __('Perfil') }}
</x-dropdown-link>
<!-- Authentication -->
<form method="POST" action="{{ route('logout') }}">
#csrf
<x-dropdown-link :href="route('logout')"
onclick="event.preventDefault();
this.closest('form').submit();">
{{ __('Log Out') }}
</x-dropdown-link>
</form>
</x-slot>
Any help or hint would be appreciated.
Thanks for your time!
Credits to #aynber to answering, what i needed to do was:
{{ route('profile', ['id' => Auth::user()->id]) }}

String replace in Laravel Blade

I have project in Laravel 5.8.
I have this code:
<a href="{{ route('user', ['url_address' => $user->url_address, 'id' => $user->id]) }}"><img
src="{{ $user->images->first()->path ?? $placeholder }}"
class="img-responsive center" alt="{{ $user->name }}"></a>
This return me:
<img src="upload/images/UserImage/c561b1742783ed038521c488d1cd59a1.jpg" class="img-responsive center" alt="angel">
I need replace this:
upload/images/UserImage/c561b1742783ed038521c488d1cd59a1.jpg
to:
upload/images/UserImage/thumbs3/c561b1742783ed038521c488d1cd59a1.jpg
How can I make it?
You have a few options here. You could update your DB data, add a new column to store the thumb path, or modify the existing path in place.
Here's one way to do it using https://laravel.com/docs/5.8/helpers#method-str-replace-first
<?php
use Illuminate\Support\Str;
...
$search = 'upload/images/UserImage/';
$replace = 'upload/images/UserImage/thumbs3/';
$original = $user->images->first()->path;
$replaced = Str::replaceFirst($search, $replace, $original );
// Now, use `$replaced` as your image src.
<a href="{{ route('user', ['url_address' => $user->url_address, 'id' => $user->id]) }}">
<img src="{{ $replaced ?? $placeholder }}"
class="img-responsive center" alt="{{ $user->name }}">
</a>
I should also mention, that in my experience it is easier to maintain a system where all you store in the DB is the file name of the uploaded document. In this case, c561b1742783ed038521c488d1cd59a1.jpg.
Your views then control which directory to look in: ...public_path('upload/images/thumbs/' . $user->profile_image)

Laravel modal binding gives route not found error

I'm developing simple crude application using laravel 4.2. this is my controller method for edit/update.
class ProductsController extends BaseController{
public function getEdit($id){
$product=Products::find($id);
$this->layout->content=View::make('products.edit',compact('product'));
}
}
this is the part of edit.blade.php file
{{ Form::model($product, ['route' => ['products/update', $product->id], 'method' => 'patch']) }}
I define route for the ProductsController as follows in route.php file
Route::controller ( 'products', 'ProductsController');
when i try to edit product(http://localhost:8000/products/5/edit)
it says Route [products/update] not defined.
this is my edit link
<a class="btn btn-small btn-info" href="{{ URL::to('products/' . $product->id . '/edit') }}">Edit </a>
what is the reason for this error? i have define patchUpdate() function on product contraller.
You are using a route controller, not a resourceful controller - so there are no 'named' routes.
You could do this
{{ Form::model($product, ['action' => 'ProductsController#putEdit', $product->id], 'method' => 'patch']) }}
Add following line your routes.php file
Route::model('products', 'Product');
Route::resource('products', 'ProductsController');
and also change what #The Shift Exchange has suggested
products.update not products/update
change also
<a class="btn btn-small btn-info" href="{{ URL::to('products/getEdit/'. $product->id) }}">Edit </a>

Categories