Use Larave route to vuejs component - laravel

I have an a tag in one of my VueJS component like below.
<a href="" class="btn btn-sm btn-white">
<i class="tio-edit"></i>
</a>
I would like to place route('admin.shop.time.edit',1) inside href with ID parameter.
How can I do it ?

I use this package : https://github.com/tighten/ziggy
that provide a route() helper to use in javascript :
route('user.profile')
you can define which routes are accessible from the js in the config/ziggy.php file
<?php
return [
'whitelist' => [
'user.*',
'home',
'login',
'logout',
'password.*',
],
];
You just have to put a #routes helper in your blade layout and it will work
it's iso with the laravel route helper, and can handle parameters and so on, like for example you can do route('user.show', {id: '15'})

Related

Error: Ziggy error: 'id' parameter is required for route

I am working on single SPA using laravel and inertia js. I am using multi language as well. So I am passing language as prefix parameter like this.
Route::group(['prefix' => '{language}'],function(){
Route::get('events',[HomeController::class,'event'])->name('events');
Route::get('events-details/{id}',[HomeController::class,'eventDetail'])
->name('events.reservations.index');
});
my controller
public function eventDetail($land,$id)
{
$events = $this->repository->getAllEvents();
$event_details = $this->repository->findById($id);
$time_interval = ReservationHelper::getTimeInterval();
return Inertia::render('Frontend/Pages/Reservation/Index', compact('events','event_details','time_interval'));
}
Calling in vue component like this
<Link
:href="
route('events.reservations.index', [$page.props.current_locale, id])
"
>
<div class="sao-box1 text-center">
<img :src="imgSrc" alt="" class="img-fluid rounded_img" />
<h3>{{ title }}</h3>
<div class="event-description" v-html="description"></div>
<a class="sl-btnBook">Book</a>
</div>
</Link>
when I click on that link I am getting error as
Error: Ziggy error: 'id' parameter is required for route 'events.reservations.index'
I don't know where I am going wrong.
thank you
change this line only from :-
:href="route('events.reservations.index', [$page.props.current_locale, id])"
to :
:href="route('events.reservations.index', {language : $page.props.current_locale, id: id})"
thanks

How to set up a beautiful url in laravel 8?

Now my url looks like this
localhost/3
But I want the path to look like this
localhost/some-text
To do this, I have a special url field in the Ad model, where the desired address is located. But going through it, I get 404
I have a route
Route::get('/{url}', [App\Http\Controllers\IndexController::class, 'show'])->name('show');
Method
public function show($url) {
$ad = Ad::where('url', $url)->first();
return view('ad', compact('ad'));
}
And btn
<a href="{{ route('show', ['url' => $ad->url]) }}">
<button type="button" class="btn btn-sm btn-outline-secondary">View</button>
</a>
To create better links just use slug
I use laravel slugable

Route [/download] not defined. (View:

hi I am downloading data directly from URL, but when I try to make a button that when I click on it then it becomes downloadable then it said:
Route [/download] not defined.
D:\xampp\htdocs\laravel\webpro5\resources\views\showrecord.blade.php
code of button:
<div align="center">
Export to Excel
</div>
route:
Route::get('/download', function(){
return Excel::download(new ExcelsExport, 'importpdfs.xlsx');
});
The route() function expects a single parameter that matches a named route. Currently, you don't have a named one. Either use the url() function:
...
or name your route:
Route::get("/download", ...)->name("download");
<div align="center">
Export to Excel
</div>
or
<div align="center">
Export to Excel
</div>
route helper is used for named routes.
Additionally you can do
Route::get('/download', 'Controller#Method')->name('download');
then you can use the route helper like this:
{{ route('download') }}

How to pass vue data to laravel #include

I want to pass the Vue data to Laravel include
Let say for example:
#include('sample-form', then pass vue data here)
as usual it looks similar to this:
#include('sample-form', ['id' => $data]);
But when includes Vue data like the first statement above it will returns error on my blade.
Here's my code:
<button type="button" data-toggle="modal" :data-target="'#edit_' + attribute.id" class="btn btn-info btn-xs radius"><i class="fa fa-pencil"></i> Edit</button>
And here's what I'm trying to accomplish:
#include('modals.edit-modal', :data-target="'#edit_' + attribute.id")
How should we do that exactly?
Put before your Vue app script in blade template:
<script>
Vue.prototype._data = {!! json_encode(['id' => $id] !!};
</script>
Now, you can access data from Vue like:
methods: {
test() {
alert(this._data.id)
}
}

Laravel NotFoundHttpException although route exits

I added a new route as:
Route::post('friendSend/{uid}','FriendController#sendFriendRequest')->name('friends.add');
and called it as a hyperlink to submit form:
<a href="{{route('friends.add',$user->uid)}}"
onclick="event.preventDefault();
document.getElementById('addfriend-form).submit();">
<i class="glyphicon glyphicon-facetime-video" style="color:#F44336;"></i> Add Friend
</a>
<form action="{{route('friends.add',$user->uid)}}" method="post" id="addfriend-form">
{{ csrf_field() }}
</form>
However when I click on the said link, I get redirected to /friendSend with the said error.
the route is visible in:
php artisan route:list
which makes sense since I called it via it's name 'friends.add'. It doesn't even go the controller.
I've already tried the following:
Laravel NotFoundHttpException although route exists
Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Friend;
use Auth;
class FriendController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
return redirect()->route('home');
}
public function sendFriendRequest($id)
{
echo "hello world";
}
}
Update:
Manually entering the url as /friendSend/2 (or any number for that matter) works.
You are missing a ' in the onclick javascript.
<a href="{{route('friends.add',$user->uid)}}"
onclick="event.preventDefault();
document.getElementById('addfriend-form').submit();">
<i class="glyphicon glyphicon-facetime-video" style="color:#F44336;"></i> Add Friend
</a>
<form action="{{route('friends.add',$user->uid)}}" method="post" id="addfriend-form">
{{ csrf_field() }}
</form>
If this doesn't work, what is the href on the generated page? Do you have multiple of these forms on a single page?
I thing you are doing wrong on set the url in form submit.You are adding route like
href="{{route('friends.add',$user->uid)}}"
Just modified it as
href="{{route('friends.add',['uid' => $user->uid])}}"
You can refer Laravel Named Routes

Resources