routes/web route me to another route - php

My web doesn't seem to be directing to the correct page.
Here is my blade
<div class="container">
<div class="col-lg-12 d-flex justify-content-between align-items-center">
<h2>Informations</h2>
<a href="{{ route('add-new-information') }}" class="btn text-success">
<i class="fas fa-plus-circle fa-2x"></i>
</a>
</div>
<hr>
<div class="row d-flex justify-content-center align-items-center mt-5" style="max-height: 500px !important; overflow-y: scroll">
#foreach ($informations as $info)
<div class="card col-sm-11 p-0 mb-4 clickable-item" onclick='window.location = "{{ route('admin-informations', ['id' => $info->id]) }}"'>
...
</div>
#endforeach
</div>
</div>
Here is my routes/web
Auth::routes();
Route::group(['middleware' => ['auth'], 'prefix' => '/',], function () {
Route::get('/', function () {
return redirect('/home');
});
Route::group(['prefix' => 'admin'], function() {
Route::get('/informations', [App\Http\Controllers\InformationController::class, 'index'])->name('informations');
Route::get('/informations/{id}', [App\Http\Controllers\InformationController::class, 'indexAdminInfo'])->name('admin-informations');
Route::get('/informations/add-new-information', [App\Http\Controllers\InformationController::class, 'add'])->name('add-new-information');
});
});
and here is my controller
public function indexAdminInfo($id){
$information = Information::find($id);
// $comments = Comment::where('information_id', $id)->orderByDesc('created_at')->get();
$ack_count = Acknowledge::where('information_id', $id)->count();
$user_ack = Acknowledge::where([
['information_id', '=', $id],
['user_id', '=', Auth::user()->id],
])->first();
$ack = 'FALSE';
if($user_ack != null){
$ack = 'TRUE';
}
return view('adminviews/infoadmin', compact('information', 'ack_count', 'ack', 'user_ack'));
}
public function add(){
return view('adminviews/addinfo');
}
For some reason, when I click the a tag with the href {{ route('add-new-information') }} to go to the add page 'adminviews/addinfo',
instead the page will go to the 'adminviews/infoadmin' page, which will cause an error, because no parameters are being sent.
I tried checking the code, but it looks correct to me. Can anybody find an error on this?

the problem is with your routes:
these two routes are ambiguous:
Route::get('/informations/{id}');
Route::get('/informations/add-new-information');
just think of below scenario:
router wants to route, this url : /information/add-new-information
router will hit the first defined route, because it is compatible with the definition ('/informations/{id}')
Note :{id} is a variable and can be any string
so it will go with this.
Solution
write the more restricted route first,
and more general route later:
Route::get('/informations/add-new-information');
Route::get('/informations/{id}');

Related

Form refuses to add first entry to cart and remove first entry from cart in Laravel

After redownloading my laravel project to my new pc from github, I got a new bug that didn't have before. The first entry of my shopping cart and of my products page can not go throught the assigned route given, but the rest of the entries work perfectly fine. I couldn't really find anything that resembled my problem in other questions.
This is the error message:
I have tried to substitute the Route::post with Route::match(['get', 'post'], which solved the error, but had as a result that I couldn't add the first entry to the cart.
These are my routes before and after exchanging:
Route::get('/login', [LoginController::class, 'index'])->name('login');
Route::post('/login/authenticate', [LoginController::class, 'authenticate'])->name('auth');
Route::get('/home', [PageController::class, 'home'])->name('home')->middleware('checklogin');
Route::get('/products', [PageController::class, 'products'])->name('product')->middleware('checklogin');
Route::get('/logout', [LoginController::class, 'logout'])->name('logout');
Route::post('/products/addproduct', [OrderController::class, 'addToCart'])->name('addtocart')->middleware('checklogin');
Route::post('/products/removeproduct', [OrderController::class, 'removeFromCart'])->name('removefromcart')->middleware('checklogin');
Route::get('/order', [PageController::class, 'order'])->name('order')->middleware('checklogin');
Route::post('/order/buy', [OrderController::class, 'payment'])->name('pay')->middleware('checklogin');
Route::get('/orderhistory', function(){return view('orderHistory');})->name('orderhistory')->middleware('checklogin');
Route::get('/products/details/{product}', [ProductDetailController::class, 'getProductPage'])->name('productdetail')->middleware('checklogin');
Route::get('/login', [LoginController::class, 'index'])->name('login');
Route::match(['get', 'post'], '/login/authenticate', [LoginController::class, 'authenticate'])->name('auth');
Route::get('/home', [PageController::class, 'home'])->name('home')->middleware('checklogin');
Route::match(['get', 'post'], '/products', [PageController::class, 'products'])->name('product')->middleware('checklogin');
Route::get('/logout', [LoginController::class, 'logout'])->name('logout');
Route::match(['get', 'post'], '/products/addproduct', [OrderController::class, 'addToCart'])->name('addtocart')->middleware('checklogin');
Route::match(['get', 'post'], '/products/removeproduct', [OrderController::class, 'removeFromCart'])->name('removefromcart')->middleware('checklogin');
Route::match(['get', 'post'], '/order', [PageController::class, 'order'])->name('order')->middleware('checklogin');
Route::match(['get', 'post'], '/order/buy', [OrderController::class, 'payment'])->name('pay')->middleware('checklogin');
Route::get('/orderhistory', function(){return view('orderHistory');})->name('orderhistory')->middleware('checklogin');
Route::get('/products/details/{product}', [ProductDetailController::class, 'getProductPage'])->name('productdetail')->middleware('checklogin');
This is the form I use to send the data to the route.
<div class="card-deck row">
#foreach (ProductController::getProduct() as $product)
#if($product->quantity > 0)
<div class="col-md-4 col-sm-12 my-3">
<div class="card h-100 mx-2" style="width: 18rem;margin-bottom: -1em">
<form action="{{ route('addtocart') }}" method="post">
#csrf
<a href="products/details/{{$product->id}}" class="" style="text-decoration: none; color: inherit;">
<div class="card-body d-flex flex-column">
<input type="hidden" name="productId" value="{{$product->id}}">
<h5 class="card-title" name="product">{{$product->name}}</h5>
<p class="card-text" name="description">{{$product->description}}</p>
</div>
</a>
<ul class="list-group list-group-flush">
<li class="list-group-item">
#if(count($product->categories) < 1)
No relevant categories found.
#else
#foreach ($product->categories as $category)
{{$category->name}}
#endforeach
#endif
</li>
</ul>
<div class="card-footer mt-auto">
<div class="input-group mb-3">
<input class="form-control" aria-describedby="basic-addon1" type="number" name="quantity" value="1" min="1" max="{{$product->quantity}}">
<div class="input-group-append">
<input class="btn btn-outline-secondary" type="submit" value="Buy">
</div>
</div>
</div>
</form>
</div>
</div>
#endif
#endforeach
</div>
I have also no idea if this has anything to do with the function i use to add to or remove from the cart.
This is the function used to add the product to the cart and remove from the cart:
public function addToCart(Request $request){
$orderInfo = $this->getOrderInfo();
if($orderInfo === null){
$orderInfo = Order::create([
'customer_id' => Auth::user()->id,
'order_status' => 0
]);
}
if(Product::find($request->post('productId'))->quantity >= $request->post('quantity')){
if($orderInfo->products->contains($request->post('productId')) === true){
$orderInfo->products()->increment('order_quantity', $request->post('quantity'));
}else{
$orderInfo->products()->attach($request->post('productId'), ['order_quantity' => $request->post('quantity')]);
}
Product::where('id', $request->post('productId'))->decrement('quantity', $request->post('quantity'));
return redirect(route('product'));
}else{
return "Product not in Stock";
}
}
public function removeFromCart(Request $request){
$orderInfo = $this->getOrderInfo();
$orderQuantity = 0;
foreach($orderInfo->products as $order){
$orderQuantity = $order->pivot->order_quantity;
}
$orderInfo->products()->detach($request->post('delete'));
Product::where('id', $request->post('delete'))->increment('quantity', $orderQuantity);
return redirect(route('order'));
}
After leaving this problem for what it is for a while, I came back to it to find out that the error code I had no longer appears and that everything works fine and as it should. I don't know what the problem was, nor do I know how it fixed itself.
EDIT
The error was caused by an unclosed <form> tag in the navbar of the project. After closing it off it resolved any route related error messages.

laravel view doesn't show data after adding new route

It works really good, but when I've created new page something goes wrong and now my blog view doesn't show data, but blogs view still correctly show data . I am trying to show detailed data of each blog when user click on button "Details"
MainController:
public function blog(Blogs $blog)
{
return view('blog', compact('blog'));
}
public function blogs()
{
return view('blogs',['blogs' => Blogs::all(),]);
}
blogs.blade.php:
#extends('layouts.master')
#section('title', __('main.blogs'))
#section('content')
<div class="row">
#foreach($blogs as $blog)
#include('layouts.cardBlog', compact('blog'))
#endforeach
</div>
#endsection
and cardBlog.blade.php:
<div class="col-sm-6 col-md-4">
<div class="thumbnail">
<img src="{{($blog->image) }}">
<div class="caption">
<h3>{{ $blog->title }}</h3>
<p>{{ $blog->body }}</p>
<p>
<a href="{{route('blog', $blog->id) }}"
class="btn btn-default"
role="button">#lang('main.more')</a>
#csrf
</p>
</div>
</div>
</div>
blog.blade.php
#extends('layouts.master')
#section('title', __('main.blogs'))
#section('content')
<h1>{{ $blog->title}}</h1>
<img src="{{$blog->image }}">
<p>{{ $blog->body }}</p>
#endsection
web.php:
Route::get('/', 'MainController#index')->name('index');
Route::get('/categories', 'MainController#categories')->name('categories');
Route::get('/about', 'MainController#aboutus')->name('about');
Route::get('/contact-us', 'ContactUSController#contactUS')->name('contact-us');
Route::post('contactus', ['as'=>'contactus.store','uses'=>'ContactUSController#contactSaveData']);
Route::get('/contacts', 'MainController#contacts')->name('contacts');
Route::get('/blogs', 'MainController#blogs')->name('blogs');
Route::get('/blog/{id}', 'MainController#blog')->name('blog');
Route::get('/intership', 'MainController#intership')->name('intership');
Route::get('/{category}', 'MainController#category')->name('category');
Route::get('/{category}/{product}/{skus}', 'MainController#sku')->name('sku');
Route::post('subscription/{skus}', 'MainController#subscribe')->name('subscription');
What's is the error that it shown?
I suggest use "compact" in this line code
return view('blogs',['blogs' => Blogs::all(),]);
Something like this
public function blogs()
{
$blogs = Blogs::all();
return view('blogs',compact('blogs'));
}
Try this
public function blog($id)
{
$blog = Blogs::findOrFail($id)
return view('blog', compact('blog'));
}

What can cause a laravel blade template to not be processed?

I'm working with Laravel 5.1
I have a template for a page that was previously working fine. I could hit my route and the page would be rendered as expected. At some point the page started just displaying the text contained in the blade template rather than rendering my page. To my knowledge nothing had changed in my routes file, controller or blade. Is there anything that is known to cause this to happen? I've tried updating permissions, using a different method to call the view and creating a new view file all together.
The route:
Route::group(['prefix' => '/admin'], function() {
Route::group(['prefix' => '/reactor-rules'], function() {
Route::get('/visual-rules/{track_id?}', [
'as' => 'broker_visual_reactor_rules',
'uses' => 'ReactorRulesController#visualRules'
]);
});
});
ReactorRulesController#visualRules:
public function visualRules(IReactorTrackDAO $reactorTrackDAO, $id = 0)
{
$export = new ReactorExport();
$tracks = $export->getReactorTracks();
if ($id == 0) {
$eventsArray = [];
}
else {
$eventsArray = $export->getArrayForTrack($id);
}
return $this->renderView('enterpriseBroker::reactor-rules.visual-rules', compact('eventsArray','tracks','reactorTrackDAO'));
}
My blade:
#extends($layout)
{{ SEO::setPageTitle(trans('enterpriseBroker::reactorRules.title'), false) }}
#section('content')
<br>
<br>
#if(empty($eventsArray))
<p>Please select a track below to view/export rules.</p>
#foreach($tracks as $track)
{{$track->name}}<br>
#endforeach
#else
#foreach($eventsArray as $track => $events)
<h4>{{$track}}</h4>
<a href="{{url().'/admin/visual-rules/'.$reactorTrackDAO->findByName($track)->id.'/download'}}" class="btn btn-primary" download>Export</a><br>
#foreach($events as $id => $event)
#if (count($event['rules']) > 0)
<div id="{{$track.'-'.str_replace(' ','_',$event['name'])}}">
<div class="col-lg-12">
<hr/>
<div class="col-lg-2">
<h4>{{$event['name']}}</h4>
</div>
<div class="col-lg-10 border border-primary">
#foreach($event['rules'] as $ruleId => $rule)
<h5>{{$rule['name']}}</h5>
<div class="col-lg-6">
<h6>Conditions to be met:</h6>
#foreach($rule['conditions'] as $condition)
<p>{{$condition}}</p>
#endforeach
</div>
<div class="col-lg-6">
<h6>Actions to run:</h6>
#foreach($rule['actions'] as $action)
<p>{!!$action!!}</p>
#endforeach
</div>
#endforeach
</div>
</div>
</div>
#endif
#endforeach
#endforeach
#endif
#endsection
#section('scripts')
#endsection
This is how it originally appeared:
This is how it currently appears:
It seems to be error with $layout
Try to test it using a hard coded value as below:
#extends('layouts.app')
and run below command
php artisan view:clear
I resolved the issue by creating a new blade file, pasting the contents of the old one in and pointing my controller to the new view. I'm not sure how this resolved the issue.

question mark in the url with laravel

i'm learning laravel for now , i'm trying to build a crud application how i got the url with a question mark how i can remove it from the url
the url that i got is like ..../blogs?1
here is the view
#extends ('layouts.app')
#section('content')
<div class="row">
#foreach($blogs as $blog)
<div class="col-md-6">
<div class="card">
<div class="card-header">
{{$blog -> title}}
</div>
<div class="card-body">
{{$blog->content}}
</div>
</div>
</div>
</div>
#endforeach
#endsection
<?php
Route::get('/', function () {
return view('welcome');
});
Route::name('blogs_path')->get('/blogs','BlogController#index');
Route::name('create_blog_path')->get('/blogs/create','BlogController#create');
Route::name('store_blog_path')->post('/blogs','BlogController#store');
Route::name('blogs_path1')->get('/blogs/{id}','BlogController#show');
Route::name('edit_blog_path')->get('/blogs/{id}/edit','BlogController#edit');
how can i fix this , thank you in advance
Because the second argument in route('blogs_path', $blog->id) is parameter.
try this:
Routes:
Route::name('blogs_path')->get('/blogs/{id}/','BlogController#index');
Controller:
public function index(Request $request, $id)
{
...
}
You made a mistake in the routing of the template Blade.
{{ route('blogs_path1', ['id' => $blog->id]) }}

Slug not work properly

I am try create slug with laravel on admin its work but on front-end view its work too except one problem, i have four article and work nice on admin but when on view front end its always open same article even-tough slug is different URL.
Controller code
public function singleArticle($slug){
Article::where('slug', '=', $slug)->increment('viewed');
$navi['country'] = Country::get();
$navi['genre'] = Genre::get();
$articles = Article::orderBy('created_at','desc')->limit(3)->get();
$latest_movies = Movie::where('type', '=', 'movie')->orderBy('created_at','desc')->limit(4)->get();
$latest_tv = Movie::where('type', '=', 'tv')->orderBy('created_at','desc')->limit(4)->get();
$most_viewed_movies = Movie::where('type', '=', 'movie')->orderBy('viewed','desc')->limit(3)->get();
$most_viewed_tv = Movie::where('type', '=', 'tv')->orderBy('viewed','desc')->limit(4)->get();
$most_viewed_article = Article::orderBy('created_at','desc')->orderBy('viewed','desc')->limit(4)->get();
$article = Article::where('slug', '=', $slug)->first();
and for view front end
#if(count($articles) > 0)
<div class="articles_list">
#foreach($articles as $article)
<div class="col-md-6">
<div class="article_item row">
<div class="article_info">
#if($article->thumb)
<a href="{{route('articles.single',$article->slug)}}"><img class="img-responsive" src="{{ url(Image::url($article->thumb,350,200,array('crop'))) }}" alt="{{$article->title}}">
#else
{{-- <img class="img-responsive" src="{{ url(Image::url($article->thumb,250,250,array('crop'))) }}" alt="{{$article->title}}"> --}}
#endif
<div class="artice_title"><h3>{{$article->title}}</h3></div>
<div class="article_time">
<span class="author">By Admin</span>
<span class="cateogry">Category</span>
<span class="time">{{date('F d, Y', strtotime($article->created_at))}} </a></span></div>
<!--<div class="article_descr">
<?php echo mb_substr($article->content, 0, 200) . ' ...'; ?>
</div>
<a class="btn btn-default read_more" href="{{route('articles.single',$article->id)}}">Read more</a> -->
</div>
</div>
</div>
#endforeach
</div>
<div class="clearfix"></div>
<div class="text-center col-xs-12">
{{$articles->links()}}
</div>
#else
<div class="col-xs-12">
Nothing found in there
</div>
#endif
For routing below code :
// Category
Route::get('category/{slug}', ['as' => 'category.index', 'uses' => 'CommonController#categoryIndex']);
// Articles
Route::get('articles', ['as' => 'articles.index', 'uses' => 'CommonController#articleIndex']);
// Specific article
Route::get('articles/{slug}', ['as' => 'articles.single', 'uses' => 'CommonController#singleArticle']);
thanks before.
It is just issue of naming convention.Please choose different names for variables.
#foreach($articles as **$article**). It is in your view which have $article variable.
**$article** = Article::where('slug', '=', $slug)->first(); It is in your controller. so it is just because of conflict.

Categories