How can i translate dynamic seo_url in Laravel? - php

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 ?

Related

How to show summary data in Laravel Blade inside of ForEach Loop?

I am trying to display a ForEach loop on my view that shows each code type. That part works. I am having trouble figuring out how to display a count of how many pieces of code (stored in another table) exist for each code type. I don't understand how to use the id of the code_type to query the code_type_selected table and to return that count.
In my CodeType model
protected $fillable = [
'name',
'color',
'icon'
];
public function codeTypesSelected() {
return $this->hasMany(CodeTypesSelected::class, 'code_type_id', 'id');
}
In my CodeTypeSelected model
protected $fillable = [
'code_type_id',
'code_id',
];
public function codeTypes() {
return $this->belongsTo(CodeType::class, 'id', 'code_type_id');
}
In my controller
public function index(Request $request)
{
$codeTypes = CodeType::latest()->get();
$codeTypesSelected = CodeTypesSelected::latest()->get()->groupBy('code_type_id');
//dd($codeTypesSelected);
return view('code_layout.index', compact('codeTypes', 'codeTypesSelected'));
}
And in my view
<!-- /. ROW -->
<div class="row">
#foreach($codeTypes as $codeTypeItem)
<div class="col-md-3 col-sm-12 col-xs-12">
<div class="panel panel-primary text-center no-boder bg-color-{{ $codeTypeItem->color }}">
<div class="panel-left pull-left {{ $codeTypeItem->color }}">
<i class="fa fa-{{ $codeTypeItem->icon }} fa-5x"></i>
</div>
<div class="panel-right">
<h3>
{{ count($codeTypesSelected) }}
</h3>
<strong> {{ $codeTypeItem->name }} </strong>
</div>
</div>
</div>
#endforeach
</div>
<!-- /. ROW -->
Have you tried this
CodeType::latest()->withCount('codeTypesSelected')->get()

Variable doesn't seem to be passing from controller to to view

I'm trying to pass a variable from a controller route to a view so i can display data relating to new assoicate model. It's working for other contrllers, but i can't figure out why it's not here.
Controller
#Index - Not passing down
public function index(){
$Advert = new PropertyAdvert();
return view('pages/Advert/index', compact('Advert'));
}
#Show - Is working, this is just an example.
public function show($id){
$user = Auth::user();
$Advert = PropertyAdvert::where('id', $id)->first();
return view('pages/Advert/show', compact('Advert', 'user'));
}
This is the route
Route::get('/property', 'AdvertisementController#index');
Route::get('/advertise', 'AdvertisementController#create')->middleware('auth');
Route::post('/createadvert', 'AdvertisementController#store');
Route::get('/property/{id}', 'AdvertisementController#show');
This is the template i'm trying to pass down to show.blade.php
<div class="row text-center" style="display:flex; flex-wrap:wrap">
<div class="col-lg-12">
<h3>Our most recent properties</h3>
#foreach ($Advert as $property)
<div class="col-md-3 col-sm-6">
<div class="thumbnail">
<img src="data:image/gif;base64,{{ $property->photo }}">
<div class="caption">
<h4>{{$property->address .', '. $property->town .', '. $property->county}}</h4>
</div>
<p>
More Info!
</p>
</div>
</div>
#endforeach
</div>
</div>
</div>
Try this:
public function index(){
$Advert = PropertyAdvert::get();
return view('pages/Advert/index', compact('Advert'));
}
You need to actually access the data for that model.

How I use relationship in blade template (hasOne) || Laravel

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}}

pagination in laravel in frontend using relationship

>
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'));
}
}

Controller not passing variable correctly - Laravel

Could someone let me know why I get this error:
"Undefined variable: listings (View: \app\views\pages\listings.blade.php)"
Controller
class ListingsController extends BaseController {
public function showListings() {
$listings = Listing::paginate(10);
return View::make('pages.listings', $listings);
}
}
View
<div class="container">
<div id="main">
<div class="row">
<div class="span9">
<h1 class="page-header">Listings</h1>
#foreach($listings as $listing)
{{ $listing->address }}
#endforeach
</div>
</div>
</div>
</div>
#include('includes.footer')
Try doing like this:
return View::make('pages.listings')->with('listings', $listings);
or
return View::make('pages.listings', array('listings' => $listings));
or even
return View::make('pages.listings', compact('listings'));

Categories