Passing Parameter into Create in Laravel - php

I tried to passsing Parameter to 'App\Http\Controllers\StoreController#create', but Laravel return error 404
i tried this on view:
<div class="text-end">
<?php $id = $i->id; ?>
<a href="{{ url('/store/create/' . $id) }}"
class="btn btn-primary text-end">Checkout</a>
</div>
web.php:
Route::get('/store/create/{$id}','App\Http\Controllers\StoreController#create');
What did i do wrong?

Just Removing the dollar sign from the route. Route::get('/store/create/{id}'. Here we cann't use the dolla sign in routes w

Related

How to pass a parameter to a middleware?

I have this route in my routes/web.php:
Route::get('/checkout', [CheckoutController::class, 'index'])->name('checkoutIndex')->middleware('auth');
My CheckoutController:
function index()
{
if (Cart::instance('default')->count() == 0) {
return redirect()->route('cartIndex', App::getLocale())->withErrors('Your shopping cart is empty! Please select an item to checkout.');
}
$discount = session()->has('coupon') ? session()->get('coupon')['discount'] : 0;
return view('checkout_index')->with(['lang' => App::getLocale(), 'discount' => $discount]);
}
When I'm not logged in and go to the URL it takes me to a login page as it should. but the login page gives a Missing required parameter for [Route: login] [URI: {lang}/login] [Missing parameter: lang]. error.
The login page works well in its own route anywhere else in the app, I only get the error by clicking on this one specific link:
<a class="btn btn-primary text-light p-3 rounded-0" href="{{ route('checkoutIndex', App::getLocale()) }}">{{__('Proceed to Checkout')}}</a>
I believe having middleware is causing the issue.
I'm using the laravel built-in auth. the route to login is Auth::routes();
Any ideas?
could you try this
in your App\Http\Middleware\Authenticate middleware
protected function redirectTo($request)
{
if (! $request->expectsJson()) {
return route('login');
}
}
You need to pass the required parameter to the route:
return route('login', ['lang' => App::getLocale()])
change this line:
<a class="btn btn-primary text-light p-3 rounded-0" href="{{ route('checkoutIndex', App::getLocale()) }}">{{__('Proceed to Checkout')}}</a>
to
<a class="btn btn-primary text-light p-3 rounded-0" href="{{ route('checkoutIndex', ['lang' => App::getLocale()) ] }}">{{__('Proceed to Checkout')}}</a>

Fetching particular data from <table> (using Id) in Laravel

View:
<td>
<div class="template-demo">
<button type="button" onclick="location.href=' {{ route ('SupAd.View_PL_Accnt/{$id}') }}'" class="btn btn-outline-info btn-icon-text">
<i class="ti-search btn-icon-append"></i>View
</button>
</td>
Route:
`Route::get('View_PL_Accnt', [SupAdController::class, 'View_PL_Accnt'])->name('SupAd.View_PL_Accnt/{$id}');`
Controller:
public function View_PL_Accnt(Request $request){
$id = $request->id;
$data = User::find($id);
return view('dashboards.SupAd.pages.index.View_PL_Accnt', compact (['data', 'id']));
}
View_PL_Accnt.blade.php :
<h3 class="SupAd_name">{{ data['name'] }}</h3>
Error:
Use of undefined constant data - assumed 'data' (this will throw an Error in a future version of PHP) (View: C:\Users\CuatrosMarias\Documents\GitHub\IPS\resources\views\dashboards\SupAd\pages\index\View_PL_Accnt.blade.php)
Error
You need to send variables using with() in your controller
return view('dashboards.SupAd.pages.index.View_PL_Accnt')->with('data',$data)->with('id',$id);
Your View Accnt.blade.php you have used data as constant you need to use it as variable
Eloquent result gives object so you can access the name property of your result object like below
{{ $data->name }}
typo error {{ $data['name'] }} $ is missing at the view
Try this one works fine in my side always..
->with(compact('data', 'id'));
View:
<button type="button" onclick="location.href=' {{ route ('SupAd.View_PL_Accnt', [$user->id]) }}'" class="btn btn-outline-info btn-icon-text">
<i class="ti-search btn-icon-append"></i>View
</button>
Route:
Route::get('View_PL_Accnt/{id}', [SupAdController::class, 'View_PL_Accnt'])->name('SupAd.View_PL_Accnt');
Controller:
public function View_PL_Accnt($id){
$data = User::find($id);
return view('dashboards.SupAd.pages.index.View_PL_Accnt', compact (['data', 'id']));
View_PL_Accnt.blade.php :
<h3 class="SupAd_name">{{ $data->name }}</h3>

Trying to use Laravel policies into components

I'm trying to use Policies inside a Post Component, using Laravel. This is how I'm iterating through the posts in my page.
#foreach($posts as $post)
<x-post
:id="$post->id"
:title="$post->title"
:description="$post->description"
:userId="$post->user_id"
:img="$post->img"
:author="$post->user->name"/>
#endforeach
In the post.blade.php I'm trying to use a 'update' policy method to define which users can see the post:
#can('update', Auth::user(), /*What shoud I pass here?*/)
<a class="btn btn-success"href="{{route('posts.edit', $id)}}">
<i class="bi bi-pencil"></i>
</a>
#endcan
What should I pass as the second parameter of the policy? Normally, it would be a post variable. Since I'm already inside a post, I don't know to proceed.
You could check outside the component. Something like
#foreach ($posts as $post)
<x-post
:id="$post->id"
:title="$post->title"
:description="$post->description"
:userId="$post->user_id"
:img="$post->img"
:author="$post->user->name"
:canUpdate="Auth::user()->can('update', $post)"/>
#endforeach
#if ($canUpdate)
<a class="btn btn-success"href="{{ route('posts.edit', $id) }}">
<i class="bi bi-pencil"></i>
</a>
#endif

How can I redirect one blade to another blade without using route file in laravel 5

How to redirect index.blade to register.blade
index.blade
<a href="{{ url('register') }}">
Click to Channel
</a>
register.blade
{!!Form::text('name')!!}
In the example below you have to use route but the url does not change.
You just change the blade
Use this in index.blade:
<button type="button">Edit/Add Values</button>
Use this in route file
Route::get('/toregister', 'YourController#redirectToRegister');
And this to YourController:
public function redirectToRegister() {
return view('register');
}

Laravel Undifined Variable in View

I cant't figure out what's going wrong with a link from my "welcome" page to a "profile" page.
It says "Undefined variable: restaurants (View: /Users/beyerdynamic/Documents/Developer/dev1/resources/views/welcome.blade.php)"
The strange thong is that I pass the Variable to my view.
My PagesController is:
public function welcome() {
$restaurants = User::orderByRaw('RAND()')->take(3)->get();
return view('welcome')->withRestaurants($restaurants);
}
My View is:
#foreach($restaurants as $key => $restaurant)
<div class="col-md-4">
<div class="card">
<div class="image">
<img src="{{asset('images/frontend/profile/dummy/profilehero/hero4.png')}}" alt="..." />
<div class="filter filter-white">
<a href="{{ URL::to('profile/'.$restaurant->id)}}" type="button" class="btn btn-info btn-round btn-fill">
<i class="fa fa-heart"></i> View Restaurant
</a>
</div>
</div>
</div>
</div>
#endforeach
The Restaurants Variable is passed properly to my welcome view, but when I click the link the error occurs on /profile/{id} url.
Change return view('welcome')->withRestaurants($restaurants);
to
return view('welcome')->with('restaurants', $restaurants);
OR
return view('welcome', compact('restaurants');
Try with this,
return view('welcome')->with("restaurants",$restaurants);
OR
return view('welcome',["restaurants" => $restaurants]);
Check laravel docs : https://laravel.com/docs/master/views#passing-data-to-views
Change the following line:
return view('welcome')->withRestaurants($restaurants); // there is nothing like withRestaurants in laravel
to
return view('welcome', array('restaurants' => $restaurants));
and try again.

Categories