i am beginner of laravel. i ran into the problem .Target class [App\Http\Controller\ContactController] does not exist.
i set the all the routes files currectly.i don't why this errors is displaying.
ContactController
public function index()
{
$contacts = Contact::all();
return view('contact.index')->with('contacts',$contacts);
}
View Page
#extends('layout')
#section('content')
<div class="card">
<div class="card-header">About</div>
<div class="card-body">
#foreach($contacts as $contact)
<p>{{ $contact->name }}</p>
<p>{{ $contact->address }}</p>
<p>{{ $contact->mobile }}</p>
#endforeach
</div>
</div>
#stop
i attached the screen shot image below.
Routes
use App\Http\Controller\ContactController;
Route::resource('/contact',ContactController::class);
The controllers live in App\Http\Controllers, so edit
use App\Http\Controller\ContactController;
to
use App\Http\Controllers\ContactController;
make sure the file namespace is correct too
Related
I'm trying to pass an array of objects. At the moment I'm passing it manually, but it will be able to come from the bank or insert it manually.
The problem is that it is giving an error, saying that it is an invalid argument to foreach. This is probably because of how I'm writing, can anyone give me some guidance?
<x-banner-introduction isCarousel="1" carouselItems="[{'titleBefore' : 'asds'}]" ...>
</x-banner-introduction>
<section {{ $attributes->merge(['class' => 'c-bannerIntroduction']) }}>
<div class="container">
{{-- Is carousel --}}
#if($isCarousel)
<div class="c-bannerIntroduction__carousel">
<div class="owl-carousel-oneItem owl-carousel owl-theme">
#foreach ($carouselItems as $item)
<div class="item">
<h1 class="nv-title nv-title--smile nv-title--big text-white mb-3 mb-md-4">
{{-- <span>{{$item->titleBefore}}</span> --}}
</h1>
</div>
#endforeach
</div>
</div>
...
<?php
namespace App\View\Components;
use Illuminate\View\Component;
class BannerIntroduction extends Component
{
public $isCarousel;
public $carouselItems;
...
public function __construct($isCarousel = 0, $carouselItems, ...)
{
$this->isCarousel = $isCarousel;
$this->carouselItems = $carouselItems;
...
}
...
}
So my front-end Lists of Businesses are not in paginated style. But I do not know how to do it. Can anyone please help? The code I posted is in my BusinessListController.php
BusinessListController.php
`<?php
namespace App\Http\Controllers;
use App\Models\Business;
use App\Models\Category;
use App\Models\Location;
use Illuminate\Http\Request;
class BusinessListController extends Controller
{
public function index(Request $request)
{
$businesses = Business::query()
->with('location')
->whereFilters($request->only(
['search', 'category', 'location']
))
->get();d
return view('pages.business-list', [
'businesses' => $businesses,
'locations' => Location::all(),
'categories' => Category::all()
]);
}
}`
And then here is the code for my view blade front-end
Business-List.blade.php
<div class="row business-list-row mx-auto">
#foreach ($businesses as $business)
<div class="col-md-4">
<div class="card shadow border-light mb-3">
<img
src="https://cdn1.clickthecity.com/images/articles/content/5d6eba1f4795e0.58378778.jpg"
class="card-img-top" alt="...">
<div class="card-body">
<div class="d-flex justify-content-between">
<div>
<h4 class="card-title h6" style="font-weight: bold;">
{{Str::limit($business->name, 20, $end='...')}}
</h4>
<div class="">
<p class="card-text">
{{ $business->location?->name }}
</p>
<p class="card-text" style="color: #32a852;">
{{ $business->category?->name}}
</p>
</div>
</div>
<div class="align-self-center">
<a href="{{ route('store', $business->id) }}" class="btn btn-info stretched-link">
Visit
</a>
</div>
</div>
</div>
</div>
</div>
#endforeach
</div>
So you need to do three things.
In Controller:
$businesses = Business::query()
->with('location')
->whereFilters($request->only(
['search', 'category', 'location']
))
->paginate(15);
put the number of items you need on a single page. here I put 15.
Put this under the </div> of your list.
{{ $business->links() }}
Put this inside the App\Providers\AppServiceProvider boot method.
use Illuminate\Pagination\Paginator;
public function boot()
{
Paginator::useBootstrapFive(); // or
Paginator::useBootstrapFour();
}
This depends upon which version of Bootstrap you are using.
Still confused? Checkout Laravel Pagination Documentation
Just remove ->get();d and add paginate
example
ModelName()->paginate();
I have made a simple project in Laravel. I am using Livewire v^2.2.
When I add the code as described in the documentation, the previous and next buttons are dead.
The code in php Component is:
// If I comment this out, the view is horrible but is working.
// use WithPagination;
// protected $paginationTheme = 'bootstrap';
public function render()
{
return view('livewire.users', [
'users' => User::paginate(1),
]);
}
In the template:
<div>
<h1>Users</h1>
<div class="list-group">
#foreach($users as $user)
<a href="" class="list-group-item list-group-item list-group-item-action">
<div class="d-flex w-100 justify-content-between">
<h5 class="mb-1">{{ $user->name }}</h5>
<small>{{ $user->created_at }}</small>
</div>
<p class="mb-1">{{ $user->email }}</p>
</a>
#endforeach
</div>
<br>
{{ $users->links() }}
</div>
try this
use Livewire\WithPagination;
class NameComponent extends Component
{
use WithPagination;
protected $paginationTheme = 'bootstrap';
add this in side Livewire view
<div>
.....
#push('scripts')
<script>
Livewire.restart();
</script>
#endpush
</div>
in layouts/app.blade.php after #livewireScripts add #stack('scripts')
<body>
...
#livewireScripts
#stack('scripts')
</body>
I found the reason why.
Livewire components MUST have a single root element.
I forgot to add the top line in the question i see.
I can currently show the index page along with a list of posts. The user can select a post to view post details. For this I have:
Routes:
Route::get('/', 'PostsController#showPosts');
Route::get('post/{slug}', 'PostsController#showPostDetails');
Controller:
<?php
namespace App\Http\Controllers;
use App\Post;
use Illuminate\Http\Request;
class PostsController extends Controller
{
public function showPosts()
{
$posts = Post::simplePaginate(2);
return view('index', ['posts' => $posts]);
}
public function showPostDetails($slug)
{
$post = Post::findBySlug($slug);
return view('post.show',['post'=>$post]);
}
}
Model:
public static function findBySlug($slug)
{
return static::where('slug', $slug)->first();
}
index
#extends('layouts.index')
#section('header')
<!-- Page Header -->
<header class="masthead" style="background-image: url('img/home-bg.jpg')">
<div class="overlay"></div>
<div class="container">
<div class="row">
<div class="col-lg-8 col-md-10 mx-auto">
<div class="site-heading">
<h1>Train Testing</h1>
<span class="subheading">Testing Times</span>
</div>
</div>
</div>
</div>
</header>
#stop
#section('content')
<div class="row">
<div class="col-lg-8 col-md-10 mx-auto">
#foreach ($posts as $post)
#include('partials.post', ['post' => $post])
#endforeach
{{ $posts->links() }}
</div>
</div>
#stop
partials/post.blade.php
<div class="post-preview">
<a href="/post/{{ $post->slug }}">
<h2 class="post-title">
{{ $post->title }}
</h2>
<h3 class="post-subtitle">
{{ $post->excerpt }}
</h3>
</a>
<p class="post-meta">Posted by
{{ $post->author->name }}
on {{ $post->created_at->format('l d F, Y') }}</p>
</div>
<hr>
I now want to show the posts on all other pages. My other pages currently have the route Route::get('{slug}', 'PagesController#show'); And it makes sense initially to refer back to existing code so use Route::get('{slug}', 'PostsController#showPosts'); like I did on the home page to display posts there.
However I am not sure of the best way to deal with this as I believe you can not have two controllers for one route.
For other pages I currently have:
Controller:
<?php
namespace App\Http\Controllers;
use App\Page;
use Illuminate\Http\Request;
class PagesController extends Controller
{
public function show($slug)
{
$page = Page::findBySlug($slug);
return view('page', ['page' => $page]);
}
}
page.blade:
#extends('layouts.index')
#section('header')
<!-- Page Header -->
<header class="masthead" style="background-image: url('/storage/{{ $page->image }}')">
<div class="overlay"></div>
<div class="container">
<div class="row">
<div class="col-lg-8 col-md-10 mx-auto">
<div class="page-heading"> <h1>{!! $page->title !!}</h1>
<span class="subheading"></span>
</div>
</div>
</div>
</div>
</header>
#stop
#section('content')
<!-- Main Content -->
<div class="container">
<div class="row">
<div class="col-lg-8 col-md-10 mx-auto">
{!! $page->body !!}
</div>
</div>
</div>
#stop
Pages Controller:
use App\Page;
use App\Post;
use Illuminate\Http\Request;
class PagesController extends Controller
{
public function show($slug)
{
$posts = Post::simplePaginate(2);
$page = Page::findBySlug($slug);
return view('page', ['page' => $page, 'posts' => $posts]);
}
Then in page.blade.php I can call $posts without any errors:
#foreach ($posts as $post)
#include('partials.post', ['post' => $post])
#endforeach
{{ $posts->links() }}
Not sure if this is the neatest way but it works.
I am using Laravel 5.2 (although solutions for later versions are also okay).
I have a page, which contains BOTH the login page and registration page.
These forms use the AuthController as usual.
I display the errors like so:
#if (count($errors) > 0)
<div class="callout alert">
<strong>Whoops! Something went wrong!</strong>
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
The issue is, the $errors variable does not make it clear which form the errors come from (i.e. Is it errors in the registration form or login form?).
How can I do this?
A way to handle this is to return flash messages. In your controller you can use something like:
For the login form
public function postLogin() {
// your code here
return redirect('/login')->with('login', 'Enter valid details');
}
For the sign up form
public function signUp() {
// your code here
return redirect('/login')->with('signup', 'SignUp has been successful');
}
And in order to display them in the view:
<div class="clearfix">
#if(Session::has('login'))
<div class="toast">
{{ Session::get('login') }}
</div>
#endif
</div>
<div class="clearfix">
#if(Session::has('signup'))
<div class="toast">
{{ Session::get('signup') }}
</div>
#endif
</div>