>
I want to use pagination in category view. I only want to display 5 posts of that specific category which is selected using a slug.
I want pagination link in below.
This is my view:
#extends('layouts.frontend')
#section('content')
<!-- Stunning Header -->
<div class="stunning-header stunning-header-bg-lightviolet">
<div class="stunning-header-content">
<h1 class="stunning-header-title">Category:{{$category->name}}</h1>
</div>
</div>
<!-- End Stunning Header -->
<div class="container">
<div class="row medium-padding120">
<main class="main">
<div class="row">
#php
$i = 0;
#endphp
#foreach($category->posts as $post)
<div class="case-item-wrap">
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
<div class="case-item" style="margin-top: 20px;">
<div class="case-item__thumb">
<img src="{{$post->featured}}" alt="our case">
</div>
<h6 class="case-item__title">{{$post->title}}</h6>
</div>
</div>
</div>
#php $i++; #endphp
#if($i % 3 == 0)
<div class="row"></div>
#endif
#endforeach
</div>
</div>
</main>
</div>
</div>
#endsection
And this is my frontendcontroller. Where I want to select Post with specific category Then display 5 number post in my view. Having problem in how to use pagination with realtionship.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Setting;
use App\Category;
use App\Post;
use App\User;
use App\Tag;
class FrontEndController extends Controller
{
public function index()
{
$title = Setting::first()->site_name;
$categories = Category::all();
$first_post = Post::latest()->first();
$second_post = Post::latest()->skip(1)->take(1)->get()->first();
$third_post = Post::latest()->skip(2)->take(1)->get()->first();
$english_11 = Category::find(1);
$blog = Category::find(3);
$settings = Setting::first();
$tags = Tag::all();
return view('index',compact('title','categories','first_post','second_post','third_post','english_11','blog','settings','tags'));
}
public function singlePost($slug)
{
$categories = Category::all();
$settings = Setting::first();
$post = Post::where('slug',$slug)->first();
$next_id = Post::where('id','>',$post->id)->min('id');
$prev_id = Post::where('id','<',$post->id)->max('id');
$next = Post::find($next_id);
$prev = Post::find($prev_id);
$tags = Tag::all();
$title = $post->title;
return view('single',compact('post','title','categories','settings','next','prev','tags'));
}
public function categories($slug)
{
$category = Category::where('slug',$slug)->first();
$categories = Category::all();
$title = $category->name;
$settings = Setting::first();
return view('category',compact('category','categories','title','settings'));
}
public function tags($slug)
{
$tag = Tag::where('slug',$slug)->first();
$categories = Category::all();
$title = $tag->tag;
$settings = Setting::first();
return view('tag',compact('tag','categories','title','settings'));
}
}
Related
I have dynamic laravel route. But i cannot translate this route when i use getTranslatedAttribute in the blade. I guess i got error because of the dynamic route.
i got this error.
Call to a member function getTranslatedAttribute() on null (View:/home//public_html/resources/views///)
my route
Route::get('/en/solutions/{seo_url}', function ($seo_url) {
App::setLocale('en');
$themes = App\Theme::all();
$services = App\Service::all();
$referances = App\Referance::all();
$blogs = App\Blog::all();
$solutions = App\Solution::all();
$announcements = App\Announcement::all();
$news = App\News::all();
$subsolutions = App\Subsolution::all();
$solution_details = App\Solution::where('seo_url', $seo_url)->first();
$pages = TCG\Voyager\Models\Page::all();
return view('tema.parallax.en.solution_detail', compact('services','solution_details','themes','pages' , 'referances' , 'blogs' , 'solutions', 'subsolutions' , 'news', 'announcements') );
});
my blade
<div class="container ms-content">
<div class="row justify-content-between">
<div class="row blog-sidebar grid-content col-lg-12">
<div class="grid-sizer col-12"></div>
#if ("en/solutions/" . $solution_details->getTranslatedAttribute('seo_url', 'en', 'fallbackLocale') == Request::path())
<article class="grid-item col-12 pb-lg-5">
<div class="work-card card--is-link"> <figure
class="card__img media-wrapper media-wrapper--21:9"><img
src="/public/storage/{{$solution_details->resim}}"
alt="{!!$solution_details->getTranslatedAttribute('baslik', 'en', 'fallbackLocale')!!}">
<div class="glow-wrap"><i class="glow"></i></div>
</figure>
<br>
<div class="card__content"><a class="card__title">
<h4>{!!$solution_details->getTranslatedAttribute('baslik', 'en', 'fallbackLocale')!!}
</h4>
</a>
<p class="text-sm post-excerpt">{!!$solution_details->getTranslatedAttribute('aciklama',
'en', 'fallbackLocale')!!}</p>
<div class="post-info__footer"><span class="post-info__divider"></span></div>
</div>
</div>
</article>
#endif
</div>
</div>
</div>
my Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use TCG\Voyager\Traits\Translatable;
class Solution extends Model
{
use Translatable;
protected $translatable = ['baslik', 'aciklama', 'aciklama_kisa', 'seo_url' , 'seo_baslik' ,'seo_aciklama'];
}
In short, I want to translate my dynamic url.
How can i do that ?
I have a project for food ordering. On my object(restaurant) show page I'm calling all product categories with relationship for foreach products that belongs to that category.
I'm also using laravel livewire and this is my Component:
<?php
namespace App\Http\Livewire\Stores;
use Livewire\Component;
use Illuminate\Http\Request;
use App\Models\Store;
use App\Models\CategoryProduct;
use App\Models\Product;
use App\Http\Livewire\Cart;
use App\Models\Favourite;
use Session;
use Auth;
class Show extends Component
{
public $store;
public $categories;
public $ratings;
public $message = '';
public $supplements = [];
public $supplementsArray = [];
public $features = [];
public $featuresArray = '';
public $quantity = 1;
public $deliveryPrice = 0;
public function render(Request $request)
{
if(!Session::has('cart')){
return view('livewire.stores.show');
}
$items = explode('|', $this->featuresArray);
$oldCart = Session::get('cart');
$store_id = $this->store->id;
$oldCart->deliveryPrice = $this->store->delivery_price;
if($oldCart->storeId != $store_id){
$request->session()->forget('cart');
}
$cart = new Cart($oldCart);
//dd(session()->get('cart'));
return view('livewire.stores.show',[
'products' => $cart->items,
'totalPrice' => $cart->totalPrice + $cart->deliveryPrice,
'totalQty' => $cart->totalQty,
'ordersPrice' => $cart->ordersPrice,
'storeId' => $cart->storeId,
]);
}
}
This public $categories; variable is from my Controller:
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
$store = Store::find($id);
$categories = CategoryProduct::where('store_id', $store->id)->get();
$ratings = $store->getAllRatings($store->id);
if($store->checkWorktime() == 'Zatvoreno'){
return view('website.stores.closed', compact('store', 'categories', 'ratings'));
}
return view('website.stores.show', compact('store', 'categories', 'ratings'));
}
And in my blade view this is how I'm viewing it:
#foreach($categories as $category)
<div class="row m-0" id="{{$category->id}}">
<h6 class="p-3 m-0 bg-light font-weight-bold w-100">{{$category->title}} <span class="float-right">
<small class="text-black-50">{{count($category->categoryProducts)}} proizvoda</small></span>
</h6>
<div class="col-md-12 px-0 border-top">
<div class="">
<ul class="list-group list-group-flush">
#foreach($category->categoryProducts as $product)
<a href="#0" class="list-group-item list-group-item-action" data-toggle="modal" data-target="#modal{{$product->id}}">
<div class="row">
<div class="col-9">
<b class="text-dark">{{$product->title}}</b><br>
<small class="text-muted">{{$product->description}}</small><br><br>
#if($product->features->isNotEmpty())
<small class="text-dark" style="font-size:14px !important;">od {{$product->price}}€</small>
#else
<small class="text-dark" style="font-size:14px !important;">{{$product->price}}€</small>
#endif
</div>
#if($product->thumbnail)
<div class="col-3">
<span class="float-right">
<img src="{{$product->thumbnail->getUrl('showPreview')}}" style="width:80px;height:80px;border-radius:50%;object-fit:cover;" alt="">
</span>
</div>
#endif
</div>
</a>
#include('website.stores.product-modal')
#endforeach
</ul>
</div>
</div>
</div>
#endforeach
And my page is loading crazy slow. I know I'm something doing wrong and I know that there is a way to speed this up. How can I do that?
I'm still learning so forgive me if this is a stupid question but I can't seem to find a fix for it myself.
I am having trouble getting the "title" property of the JSON and from the error that I got
"Trying to get property 'tag' of non-object pages/results.blade.php:28"
I understand that $tags is an array not an object.
I did a foreach with $tags with
foreach ($result->tags as $tag)
I also know that $tag contains the data I need because when I die and dump $tag I get this
My question is how do I access an unnamed JSON that is inside an array?
The title property is inside the "tags" array and the browser is giving me an error that I can't access it because it's a non-object.
Can you please help me with the code?
Here is my controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use GuzzleHttp\Client;
class PageController extends Controller
{
// public function __construct() {
// $this->middleware('auth');
// }
public function index() {
$user = Auth::user();
return view('pages/home', compact('user'));
}
public function result(Request $request) {
// https://api.unsplash.com/search/photos?query=philippines&client_id=hb-UQIJ2DMaPckaJOO5nxrC90uYnaVRGTMz3S8WHzJY
$input = $request->input('query');
$client = new Client();
// var_dump($input); die;
// dd($input);
// var_dump($request->input('query')); die;
$res = $client->request('GET', "https://api.unsplash.com/search/photos", [
"query" => [
"query" => $input,
"client_id" =>"hb-UQIJ2DMaPckaJOO5nxrC90uYnaVRGTMz3S8WHzJY",
"per_page" => 100
]
]);
$data = $res->getBody();
$data = json_decode($data);
$filteredData = [];
// return $data->results;
foreach($data->results as $result) {
$urls = $result->urls;
array_push($filteredData,$result);
foreach ($result->tags as $tag) {
$tags = $result->tags;
array_push($filteredData, $tag);
// dd($tag);
foreach($tag as $key => $value) {
if($key === 'title') {
array_push($filteredData,$value);
array_push($filteredData,$key);
// dd($filteredData);
}
}
}
$user = Auth::user();
return view('pages/results', compact('user', 'filteredData', 'input'));
}
}
Here is my blade
#extends('layouts/main')
#section('title')
Design Storm - Inspiration for Developers
#endsection
#section('content')
<div id="site-section">
<div class="container">
<div id="results">
<div>
<div class="search-container">
<form action="/results" method="POST">
#csrf
<input class="search" type="text" value="{{$input}}" placeholder="Search" name="query">
</form>
</div>
<div class="boxes">
<div class="row">
#foreach ($filteredData as $result)
<div class="col-md-3">
<div class="box">
<div style="position: relative; background: url('{{$result->urls->small}}') no-repeat center center;-webkit-background-size: cover;-moz-background-size: cover;-o-background-size: cover;background-size: cover; height: 200px;">
<div class="add-btn "><i class="fa fa-check" aria-hidden="true"></i></div>
</div>
#foreach ($filteredData as $tag)
<h4>
{{$filteredData->tag->title}}
</h4>
#endforeach
</div>
</div>
#endforeach
</div>
</div>
</div>
</div>
</div>
</div>
#endsection
"
I am using Laravel-nestedset and am having a hard time querying the ancestrosOf my category model.
This is what I am trying to do:
Works
http://carnival-experts.test/categories (this gives me a list of all categories)
http://carnival-experts.test/categories/carnival-games (this gives me a list of all Carnival Games)
Does Not Work
http://carnival-experts.test/categories/carnival-games/large-scale-games (this shows me a list of Carnival Games and not the Large Scale Games category)
I am sure this is just my lack of understanding--any help appreciated.
web.php
...
Route::get('/', 'CategoryController#index')->name('categories');
Route::get('/{category}/{slug?}', 'CategoryController#show')->name('show');
...
CategoryController.php
...
class CategoryController extends Controller
{
public function index()
{
$categories = Category::get()->toTree();
return view('categories.index', compact('categories'));
}
public function show(Category $category, $slug = null)
{
if (!is_null($slug)) {
$categories = Category::ancestorsOf($category);
return view('categories.show_subcategory', compact('categories', 'category', 'slug'));
} else {
return view('categories.show', compact('category'));
}
}
}
show_category.blade.php
...
#foreach($category['children']->chunk(3) as $chunk)
<div class="row">
#foreach($chunk as $category)
<div class="col-md-4" style="margin-bottom: 2rem">
<div class="feature-box center media-box fbox-bg">
<div class="fbox-media">
<a href="/categories/{{$category->slug}}">
<img class="image_fade"
src="https://*****.s3-us-west-1.amazonaws.com/{{ $category->photo }}"
alt="Featured Box Image"
style="opacity: 1;"></a>
</div>
<div class="fbox-desc">
<h3>{{$category->name}}</h3>
<span>Learn More</span>
</div>
</div>
</div>
Products
#if(count($category->products) > 0)
#foreach($category->products as $product)
{{$product->title}}
#endforeach
#endif
#endforeach
</div>
#endforeach
...
Okay i'm trying get "likes" and "users" in Posts by relationship hasOne.
here is my Post.php Model
class Posts extends Model
{
protected $table = 'posts';
public function User()
{
return $this->hasOne(User::class, 'id', 'user_id');
}
public function Like()
{
return $this->hasOne(Like::class, 'post_id', 'id');
}}
My Blade template
#foreach ($showdeals as $deal)
<div class="tab-pane active" id="home" role="tabpanel">
<div class="card-body">
<div class="profiletimeline">
{{$deal->like->status}}
<br>
{{$deal->user->email}}
<div class="sl-item">
<div class="sl-left"> <img src=" {{asset( '/assets/images/users/2.jpg')}}" alt="user" class="img-circle"> </div>
<div class="sl-right">
<div> {{$deal->user->username}} || {{$deal->subject}} <Br> <span class="sl-date">{{$deal->created_at}}</span>
<div class="m-t-20 row">
<div class="col-md-3 col-xs-12"><img src="{{$deal->image}}" alt="user" class="img-responsive radius"></div>
<div class="col-md-9 col-xs-12">
<p> {{$deal->body}} </p> עבור למוצר </div>
</div>
<div class="like-comm m-t-20"> 2 תגובות <i class="fa fa-heart text-danger"></i> 5 לייקים </div>
</div>
</div>
</div>
</div>
<hr></div>
</div>
#endforeach
And there is my Controller
class PostsController extends Controller
{
public function showdeals()
{
$showdeals = Posts::with( 'User', 'Like')->get();
return view('posts.show', compact('showdeals'));
}
public function helpnewview(){
return view('posts.anew');
}
public function helpnew(Request $request){
//User pick link
$userlink = $request['userlink'];
return \Redirect::route('newdeal', compact('userlink'));
}
public function new(Request $request)
{
//Emdeb user link
$link = Embed::create($request['userlink']);
$linke = $request['userlink'];
return view('posts.new', compact('link', 'userlink', 'linke'));
}
public function create(Request $request)
{
$posts = New Posts;
$posts->user_id = Auth::User()->id;
$posts->subject = $request['subject'];
$posts->body = $request['body'];
$posts->link = $request['link'];
$posts->price = $request['price'];
$posts->image = $request['image'];
$posts->tag = $request['tag'];
$posts->save();
return back();
}
}
Now if I do something like {{$deal->user->email}} its will work,
if I go to something like this {{$deal->like->status}} its does not work,
am I missing something ?
If you want multiple relationships to be eagerly loaded you need to use an array of relationships: Model::with(['rl1', 'rl2'])->get();
public function showdeals()
{
...
$showdeals = Posts::with(['User', 'Like'])->get();
...
}
EDIT:
From that json in the comments that I see, there is no attribute named status in your Like model so thats probably the root of the problem
Controller edit this code
public function showdeals()
{
$showdeals = Posts::all();
return view('posts.show', compact('showdeals'));
}
And blade file code
#foreach ($showdeals as $deal)
<div class="tab-pane active" id="home" role="tabpanel">
<div class="card-body">
<div class="profiletimeline">
{{ $deal->Like->status }}
<br>
{{ $deal->User->email }}
#endforeach
I think everything is good except
{{$deal->like->status}} {{$deal->user->email}}
Please try as
{{$deal->Like()->status}}
<br>
{{$deal->User()->email}}