#extends('layouts.app')
#section('title', 'Post')
#section('contents')
<div class="container">
<div class="row">
#foreach ($posts as $post)
<div class="col-md-4 mb-4">
<div class="row">
<div class="card mb-4">
<div class="card-header">
{{ $post->title }}
</div>
<div class="card-body">
{{ $post->body }}
</div>
<div class="card-footer">
{{ $post->created_at->diffForHumans() }}
</div>
</div>
</div>
</div>
#endforeach
</div>
<div class="d-felx justify-content-center">
{{ $posts->links() }}
</div>
</div>
#endsection
I had a problem while trying out the recently released laravel 8, I'm trying to find out what the changes are and how it works. When I did that I had a problem with the paginate laravel 8 UI getting messy and somehow it happened. Is there anyone who can help me? or have experienced the same thing?
By messy I think what you imply here is that the elements are out place.
Laravel 8 uses tailwind css.
If it's the problem and you have queried it correctly with paginate()
then what you can do is, use bootstrap.
Simply add the following code in AppServiceProvider file and it will use bootstrap for paginate styling.
class AppServiceProvider extends ServiceProvider {
/**
* Register any application services.
*
* #return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
Schema::defaultStringLength(191);
Paginator::useBootstrap(); //this line will be in boot method*
}
}
i want to create some CMS project with the laravel, when the user trying to click the categories, the app will show the post that only shows the category that user clicked. When the user click the categories, the app says error Bad Method Call. What should i do? Any help will be appreciated.
Here's the code from web.php
<?php
use App\Http\Controllers\Blog\PostsController;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', 'WelcomeController#index')->name('welcome');
Route::get('blog/posts/{post}', [PostsController::class, 'show'])->name('blog.show');
Route::get('blog/categories/{category}', [PostsController::class, 'category'])->name('blog.category');
Route::get('blog/tags/{tag}', [PostsController::class, 'tag'])->name('blog.tag');
Auth::routes();
Route::middleware(['auth'])->group(function () {
Route::get('/home', 'HomeController#index')->name('home');
Route::resource('categories', 'CategoriesController');
Route::resource('posts','PostsController');
Route::resource('tags','TagsController');
Route::get('trashed-posts','PostsController#trashed')->name('trashed-posts.index');
Route::put('restore-post/{post}', 'PostsController#restore')->name('restore-posts');
});
Route::middleware(['auth', 'admin'])->group(function () {
Route::get('users/profile', 'UserController#edit')->name('users.edit-profile');
Route::put('users/profile', 'UserController#update')->name('users.update-profile');
Route::get('users','UserController#index')->name('users.index');
Route::post('users/{user}/make-admin', 'UserController#makeAdmin')->name('users.make-admin');
});
Here's the code from PostsController.php
<?php
namespace App\Http\Controllers\Blog;
use App\Http\Controllers\Controller;
use App\Post;
use Illuminate\Http\Request;
use App\Tag;
use App\Category;
class PostsController extends Controller
{
public function show(Post $post) {
return view('blog.show')->with('post', $post);
}
public function category(Category $category) {
return view('blog.category')
->with('category', $category)
->with('posts', $category->posts()->simplePaginate())
->with('categories', Category::all())
->with('tags', Tag::all());
}
public function tag(Tag $tag) {
return view('blog.tag')
->with('tag', $tag)
->with('posts', $tag->posts()->simplePaginate(3));
}
}
Here's the code from App\Category.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
protected $fillable = ['name'];
public function post() {
return $this->hasMany(Post::class);
}
}
Here's the code from category.blade.php
#extends('layouts.blog')
#section('title')
Category {{ $category->name }}
#endsection
#section('header')
<header class="header text-center text-white" style="background-image: linear-gradient(-225deg, #5D9FFF 0%, #B8DCFF 48%, #6BBBFF 100%);">
<div class="container">
<div class="row">
<div class="col-md-8 mx-auto">
<h1>{{ $category->name }}</h1>
<p class="lead-2 opacity-90 mt-6">Read and get updated on how we progress</p>
</div>
</div>
</div>
</header><!-- /.header -->
#endsection
#section('content')
<main class="main-content">
<div class="section bg-gray">
<div class="container">
<div class="row">
<div class="col-md-8 col-xl-9">
<div class="row gap-y">
#forelse($posts as $post)
<div class="col-md-6">
<div class="card border hover-shadow-6 mb-6 d-block">
<img class="card-img-top" src="{{ asset($post->image) }}" alt="Card image cap">
<div class="p-6 text-center">
<p>
<a class="small-5 text-lighter text-uppercase ls-2 fw-400" href="#"></a>
{{ $post->category->name }}
</p>
<h5 class="mb-0">
<a class="text-dark" href="{{ route('blog.show',$post->id) }}">
{{ $post->title }}
</a>
</h5>
</div>
</div>
</div>
#empty
<p class="text-center">
No Results found for query <strong>{{ request()->query('search') }} </strong>
</p>
#endforelse
</div>
{{-- <nav class="flexbox mt-30">
<a class="btn btn-white disabled"><i class="ti-arrow-left fs-9 mr-4"></i> Newer</a>
<a class="btn btn-white" href="#">Older <i class="ti-arrow-right fs-9 ml-4"></i></a>
</nav> --}}
{{ $posts->appends(['search' => request()->query('search') ])->links() }}
</div>
#include('partials.sidebar')
</div>
</div>
</div>
</main>
#endsection
If you guys didn't see which file that i don't show up, just ask,.
If you guys see any problems that confusing, i can use teamviewer.
Any Help will be appreciated.
Thank you.
The error told that you don't have posts. Indeed it is. What you have is post() method on Category class, as evidently seen in this line:
public function post() {
You should change this ->with('posts', $category->posts()->simplePaginate()) to be:
public function category(Category $category) {
return view('blog.category')
->with('category', $category)
->with('posts', $category->post()->simplePaginate())
->with('categories', Category::all())
->with('tags', Tag::all());
}
In your model, you defined a post relationship whereas you use a posts relationship then, they're not the same one. You should replace post by posts because it's a to-many relationship.
Your relationship is post and you have multiple ->with('posts', $category->posts()->simplePaginate()). Can you change those to ->with('post') and try again?
I am trying to create charts on my application. I have the following error when I am trying to pass a variable to a view:
Undefined variable: chart (View:
/Users/ZK/Desktop/project/resources/views/home.blade.php)
I have pasted my controller code below;
<?php
namespace App\Http\Controllers\Admin;
use Charts;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class DashboardController extends Controller
{
public function index()
{
$chart = Charts::new('link', 'highcharts')
->setTitle("My Chart")
->setLabels("A", "B", "C")
->setValues([100, 50, 25])
->setElementLabel(["Total Users"]);
return view('home', [ 'chart' => $chart ]);
}
home.blade.php code;
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-12">
<div class="card">
<div class="card-header">Dashboard</div>
<div class="card-body">
{!! $chart->render() !!}
</div>
</div>
</div>
</div>
</div>
#endsection
Within my layouts.app I have also added the assets for my chart in the header;
{!! Charts::assets() !!}
Can anyone suggest what I might be doing wrong?
Thanks
I'm setting up a map, and want to display the coordinates. What do I need to do?
Blade
#foreach($myArgans as $argan)
<div class="col-lg-12">
<h1>{{ $argan->titre }}</h2>
<img class="card-img-top" src="{{ asset('storage/'.$argan->image) }}">
<div style="text-align: justify">
{{ $argan->body }}
</div>
<div>
{{ $argan->getCoordinates() }}
</div>
</div>
#endforeach
Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use TCG\Voyager\Traits\Spatial;
class Argan extends Model
{
use Spatial;
protected $spatial = ['point'];
}
i'm getting this error while passing data from controller to blade, every things looks good for me but i don't know what i'm missing
ErrorException in 3f66fc6a06d60b9d9b9d521f50ebd1074b04f3bc.php line 228:
Undefined variable: users (View: C:\wamp\www\transport_mithra\resources\views\agents.blade.php)
My controller is
<?php
namespace App\Http\Controllers;
use Response;
use Session;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
class UsersController extends ApiController
{
public function index()
{
$users = User::all();
return View('agents', compact($users));
//return view('agents')->with('users', $users); with this is also same error
}
}
and my agents.blade.php
#foreach ($users as $agent)
<div class="col s12 m6 l3">
<div class="card small card-agent">
<div class="card-image">
<img src="assets/images/profile-image-2.png" alt="">
</div>
<div class="card-content">
<div class="card-details">
<p>Name:</p><span>{{ $agent->name }}</span>
</div>
<div class="card-details">
<p>Phone:</p><span>{{ $agent->phone_number }}</span>
</div>
<div class="card-details">
<p>Email:</p><span>{{ $agent->email }}</span>
</div>
<div class="card-details">
<p>Address:</p><span>{{ $agent->address_city_village }}</span>
</div>
</div>
<div class="card-action">
<p>Drivers: 502</p>
View
</div>
</div>
</div>
#endforeach
looking forward for much needed help
Thank you
You should use correct syntax:
return view('agents', compact('users'));
compact('users') is the same as ['users' => $users]
Also the following will work:
return view('agents')->with(['users' => $users]);