How to set up a beautiful url in laravel 8? - laravel

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

Related

Can only instantiate this object with a string(Laravel)?

I get this error when I call route.
I give a route Like:
<a href="{{ route('billing', ['plan' => $plan[0]->id]) }}">
<button style="padding:15px 25px;border:none;color:#007F5F;font-weight:700;font-size:20px;border- radius:10px;">Choose Plan</button>
</a>
but When I click on link I get this error : Laravel Error Picture
This is my Route:
if (Util::registerPackageRoute('billing.process', $manualRoutes)) {
Route::get(
'/billing/process/{plan?}',
BillingController::class.'#process'
)
->middleware(['verify.shopify'])
->where('plan', '^([0-9]+|)$')
->name(Util::getShopifyConfig('route_names.billing.process'));
}
I can't understand this error please help me to solve this error.
I solved my error by adding shop parameter on the route :
My route Now :
<a href="{{ route('billing', ['plan' => 2, 'shop' => Auth::user()->name]) }}">
<button style="padding:15px 25px;border:none;color:#007F5F;font-
weight:700;font-size:20px;border-radius:10px;">Choose Plan</button>
</a>
This route works fine: 😊😊😊
Check out relevant issue: https://github.com/gnikyt/laravel-shopify/issues/872

Getting id value from url in controller and displaying values associated laravel

I'm trying to create a ticket management system with laravel jetstream and livewire. In the user page there's a table with all tickets that the user created. When the user clicks on the button to open one specific ticket, it should pass the id of the ticket and redirect to another page where it receives the data of that ticket he clicked, like title, message, etc..
The id is passed through the url, but my main problem is that whenever I try to display that data in the view, nothing shows, no errors either. I think that something might be wrong with my controller.
Here's my route:
Route::get('tickets.answers/{id}', [TicketsController::class, 'answers']);
The button to redirect to that specific ticket:
<a href="{{ url('tickets.answers' . '/'. $ticket->id ) }}" > <x-jet-secondary-button >
See Answer
</x-jet-secondary-button></a>
AnswersController:
public function render(Request $request)
{
$tickets = Ticket::where('id', $request->url('id'));
return view('livewire.tickets.answers', [
'tickets' => $tickets,
]);
}
And how I'm trying to display in my blade:
#foreach($tickets as $key => $ticket)
<!-- This example requires Tailwind CSS v2.0+ -->
<div class="bg-white shadow overflow-hidden sm:rounded-lg">
<div class="px-4 py-5 sm:px-6">
<h3 class="text-lg leading-6 font-medium text-gray-900">
Ticket nΒΊ {{$ticket->id}} - {{$ticket->title}}
</h3>
</div>
</div>
#endforeach
In your TicketsController you can fetch the id like this
public function answer(Request $request, int $id)
{
// Use the find() method, instead of where(), when searching for the primary key
$tickets = Ticket::find($id);
// .. more
}
In your routes files you specify an answer method, so use this in your TicketsController.
// See how to name a route
Route::get('tickets.answers/{id}', [TicketsController::class, 'answers'])->name('tickets.answers');
Then use the named route in your view like this:
<a href="{{ route('tickets.answers', ['id' => $ticket->id]) }}">
You can see a similar example in the Laravel docs.

multiple variables in routes. A route isn't leading to the controller defined blade

I have "show" and "edit" two routes. It's showing two different url. But "edit" route is using the 'show' blade, which is the SAME as the "show" route. How to lead the "edit" route to the 'edit' blade?
here is the web.php:
Route::get('/{user}/{course}', 'CoursesController#show')->name('course.show');
Route::get('/{user}/edit_{course}', 'CoursesController#edit')->name('course.edit');
here is the controller:
public function edit(Course $course) {
return view('courses.edit', compact('course'));
}
public function show(Course $course) {
return view('courses.show', compact('course'));
}
here is the index blade:
<a class="btn btn-xs btn-primary" href="{{ route("course.show", [auth()->user()->username, $course->title. $course->id] ) }}"> VIEW </a>
<a class="btn btn-xs btn-info" href="{{ route("course.edit", [auth()->user()->username, $course->title. $course->id]) }}"> EDIT </a>
Change the order or your routes in your routes files web.php
Route::get('/{user}/edit_{course}', 'CoursesController#edit')->name('course.edit'); //This one goes first.
Route::get('/{user}/{course}', 'CoursesController#show')->name('course.show');
Your show route is working as a wildcard if you invert the order as shown the edit route will catch it first when the second variable begins with edit_.

Laravel returns 404 error when passing a parameter

Hello when I send a data press a link to show another view laravel returns a 404 error, what am I failing?, I have created other crud and I have had no problems so far
Expansion -> index.blade.php:
<a href="{{url('/cards/'.$expansion->exp_id.'/vcards')}}"class="btn btn-primary form-control" >Go!</a>
URL:
http://localhost/CardSystem/cartas/1/vcards
Route:
Route::resource('cards', 'cardControllerr');
cardController:
public function vcards($id){
$data['cards']= DB::table('cards')
->join('expansion','expansion.exp_id','=','cards.exp_id')
->select('cards.card_id','cards.card_nom','expansion.exp_nom')
->where('cards.exp_id','=',$id)
->orderBy('cards.card_num')
->paginate(5);
return view('cards.vcards',$data);
vcards.blade.php:
#extends('layouts.app')
#section('content')
<div class="container">
#foreach($cards as $card)
<div class="form-group">
<h5 class="card-title">{{$card->card_nom}}</h5>
</div>
#endforeach
</div>
#endsection
I think you misunderstood routing, views etc.
For your controller to be hit, you will need to make the following route.
Route::get('cartas/{card}/vcards', 'cardControllerr#vcards')->name('cartas.vcards');
If you wanna link to this route use route().
route('cartas.vcards', ['card' => $card]);

Is it possible to delete record without using forms in laravel 5.4

I want to delete a record but I haven't been successful, apparently my code is wrong. Solutions i came across say i have to use a post in my form method and add the method_field helper. This would mean my view having a form in it, i want to avoid this if possible. Is it then possible to do my delete another way. Below is my code
snippet of my view
<div class="backbtn">
<a class="btn btn-savvy-delete" href="/tasks/{{$task->id}}" data-toggle="tooltip" title="Delete"><i class="fa fa-trash-o" aria-hidden="true"> Delete</i></a>
</div>
<div class="panel-body">
<p><strong>Owner:</strong> {{ ucfirst($task->employee->firstname) }} {{" "}} {{ ucfirst($task->employee->lastname) }}</p>
<p><strong>Task:</strong> {{ $task->title }}</p>
<p><strong>Description:</strong> {{ $task->description }}</p>
</div>
TaskController
public function destroy($id)
{
Task::destroy($id);
Session::flash('status', "Task was successfully deleted.");
return redirect('/tasks');
}
web.php
Route::delete('/tasks/{id}', 'TaskController#delete');
Im not sure what error you are getting, but i can point out a few things. For one use Route::get instead of ::delete, you are calling it via a link not a form method.
Secondly to delete follow what the laravel doc says here eg.
$task = App\Task::find(1);
$task->delete();

Resources