Attempt to read property "ticket_title" on bool - php

I have this problem when I want to open specific page by id.
now I am trying to shown title on my page with specific id, which have post in db, but that error stopped me.
there are my code.
route
Route::get('/tickets/{id}', [TicketsController::class, 'show'])->name('tickets.show');
controller
public function show($id) {
$tickets = Tickets::with('companies')->get();
$ticketscomp = Companies::with('tickets')->get();
$severities = Severities::with('tickets')->get();
$ticketspage = Tickets::findOrFail($id);
return view('tickets.chat', compact('ticketspage'))->with(['tickets'=> $tickets])->with(['ticketscomp'=>$ticketscomp])->with(['severities'=>$severities])->with(['ticketspage'=>$ticketspage]);
//dd($ticketspage->toArray());
blade.php
#foreach ($ticketspage as $item)
<h6 class="mb-1; ticket-list-title;">{{ $item->ticket_title }}</h6>
#endforeach
When I dd post. post is opening by id with included information.

::findOrFail() returns a single model instance. You do not need a #foreach() loop.
<h6 class="mb-1; ticket-list-title;">{{ $ticketspage->ticket_title }}</h6>

I fix this now but if someone have problem like me just read my comment.
Just remove foreach and type like this
<h6 class="mb-1; ticket-list-title;">{{ $ticketspage->ticket_title }}</h6>

Related

Beginner in Laravel - Eager loading seems to not work?

I'm new to Laravel and I'm trying to make a post where the user's post can receive likes from other users. But I'm trouble at when you click the user, it was suppose to direct to the users page where you can see all its post and the likes the it received. This is the code that suppose to do that work:
This is the controller:
public function index(User $user)
{
$posts = $user->posts()->with('user', 'likes')->get();
return view('users.posts.index', [
'user' => $user,
'posts' => $posts,
]);
}
}
and this is the view/template:
<div class="flex justify-center">
<div class="w-8/12 bg-white p-6 rounded-lg">
{{$user->name}}
</div>
</div>
I don't know what's the problem though, or is it a bug with Laravel eager loading?
But unfortunately it only returns a blank page, there's no error though.
The thing is after putting this code inside dump and die, there's data showing from the database. I don't why it doesn't show. Can someone help me with this problem? Much appreciated
Following this tutorial: https://www.youtube.com/watch?v=MFh0Fd7BsjE
Tutorial Github: https://github.com/codecourse/posty-traversy-media
If you want to access the posts you can iterate through them:
#foreach($posts as $post)
{{ $post->title }}
{{ $post->likes->count() }}
#endforeach
Not sure what data or relationships you want to access.
Check your route whether it has a user id for model injection
eg:/index/{user}
also in your blade example, there is no posts are used.you are using the user object
public function index(User $user)
{
$posts = $user->posts()->with('user', 'likes')->get();
return view('users.posts.index')->with([
'user' => $user,
'posts' => $posts,
]);
}

Getting a value from another two models at the same time in Laravel

Basically I have a function within a controller which is
public function get_all_clubs($city)
{
$city = ucwords($city);
$city_des = Town::where('town', $city)->value('town_desc');
$clubs = Club::get()->where('city', $city)->where('profile_enabled', '1');
$events = Event::where('club_id', $clubs->first()->id)->get();
$lowercase_city = Str::lower($city);
return view('frontend.pages.allclubs', compact('clubs', 'lowercase_city', 'city_des', 'events', 'flag'));
}
The events and the club tables are related using an id as you might have figured out. I have been able to get the events associated to a city based on the club_id. In my view I am echoing each event using the
#foreach Laravel syntax. The problem however is I need to create an hyperlink for each event on on my view. The routes are structred in the way below:
<a href="{{ route('event.show_event', [$club->slug, $event->slug]) }}">
Effectively, I need to pass the $event->slug which I already have and also the $club->slug which is what my question is, how do I get hold of this variable during my #foreach loop. Please note I am an intermediate Laravel/php developer.
My view is as follows
#foreach ($events as $event)
#if ( ( (strtotime((str_replace('/', '-', $event->date)))) >= (strtotime("today")) ) && ($event->event_private))
#php $flag='1'; #endphp
<li>
<div class="widget-posts-body">
<h6 class="widget-posts-title"> {{$event->title}} </h6>
</div>
</li>
#endif
#if ( ($loop->index) == '10' )
#break
#endif
#endforeach
#if($flag != '1')
<li>
<p> There are no events live at this time. Please check back later</p>
</li>
#endif
make a one to one relation in event model to club like this:
/**
* #return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function club()
{
return $this->hasOne(Club::class);
}
then call it in view like this:
<a href="{{ route('event.show_event', [$event->club->slug, $event->slug]) }}">

How Would I Send Data To a Controller

I'm building a forum, and I'm trying to implement a categories page. The page is working as of now, it's dynamic so it lists all categories stored in the database. However I want to be able to click on a category and be taken to a template page. From this template page I want to pass a category ID (defined in the database as a primary key). Then all posts with the matching category ID will be displayed. I'm having trouble passing this category ID to my category page template.
Any help would be extremely appreciated!
(How categories are displayed in a list:)
#foreach($categories as $row)
<div id="newscontainer" class="container">
<?php $categoryid = $row->id; ?>
<span id='categoryname'><?= $row->categoryname ?><br></span>
<span id="categorydescription"><?= $row->categorydescription?></span>
</div>
<br>
#endforeach
Thanks!
First of all define a route in web.php:
web.php:
Route::get('category/{category}','YouController#YourCatFunction')->name('categories.list');
in your controller:
public function YourCatFunction(Category $category)
{
// here you can return view for post with that category and then display them
return $category;
}
then in your view:
#foreach($categories as $row)
<div id="newscontainer" class="container">
<span id='categoryname'>{{ $row->categoryname }}<br></span>
<span id="categorydescription">{{ $row->categorydescription }}</span>
</div>
<br>
#endforeach
You can create a web route which will handle the category calls and return the view with that category and it's posts.
First you create a route like this:
Route::get('category/{category}', 'CategoriesController#show')->name('category.show');
Then you can access this Category inside the controller and load the posts and return them like this:
public function show(Request $request, Category $category) {
return view('category.show', compact('category'));
}
Then in your view you would have something like this where you loop over available posts:
#foreach($category->posts as $post)
// do something
#endforeach
To call the route you can simply create this link:
Show
As I can see, your base namespace is ULMG, so the correct way to your Category class would be ULMG\Category
You can try this one as well.
Route web.php
Route::get('category/{categoryid}', ['as'=>'category.show','uses'=>'CategoriesController#show');
Controller CategoriesController.php
public function show($categoryid){
// some of your code.
$categoryid = DB::table('category')->select('categoryId');
return view('category.show', compact('categoryId'));
// don't forget if you have some variables and you want to view it at blade just put it inside the compact
}
View blade category.show.blade.php
#foreach($categories as $row)
<div id="newscontainer" class="container">
<span id='categoryname'>{{ $row->categoryname }}<br></span>
<span id="categorydescription">{{ $row->categorydescription }}</span>
</div>
#endforeach
For anyone else who is having the same issue:
(I had a eureka moment...)
My forums.blade.php file (where the user selects the category they want to view:
Here, it's calling the category route (which is where the posts will be displayed) then it's setting the category ID in one url. It will look like this in the browser: www.example.test/category/(ID) (so when I query the database for posts under that category it will extract them).
<span id='categoryname'><?= $row->categoryname ?><br></span>
My web.php file:
I basically grabbed the id from the URL which was passed through when the user clicked on the link.
Route::get('/category/{id}', 'CategoriesController#getid');
My CategoriesController.php file:
Here I have defined a function, so in the web.php it knows to go to the function with the attribute of getid. Once it has found the correct function it sets the $catid the same value as $id. Then it returns to the categorytemplate view (which is the template requiring the ID in the first place to display the posts) with the $catid variable in a compact function.
public function getid($id){
$catid = $id;
return view('categorytemplate', compact('catid'));
}
I hope my explanation is clear enough to understand. And I hope this can help someone else with this issue in the future!
Thanks again to everyone else suggesting ideas.

Laravel displaying all posts with WHERE clause and pagenation in laravel

so i want to display post in my index page but only the posts that are reviewed by the admin. so i added another column in my database table post which is called "review" which is integer. So this is what i have in hand.
in my controller i have this
public function index()
{
$this->layout->content = View::make('partials.index',
array(
'posts' => Post::paginate(9)
));
}
and in my index page i have this
#section('content')
<div class="dashboard">
#foreach (array_chunk($posts->getCollection()->all(),2) as $row)
<div class="row">
#foreach($row as $post)
<article class="col-md-4 effect4" id="dash-box">
<p></p>
<c>{{$post->content}}</c><br>
<b>{{$post->title}}</b><br>
<d>posted..{{$post->created_at->diffForHumans()}}</d>
<hr>
</article>
#endforeach
</div>
#endforeach
<div class="page">
{{$posts->appends(Request::only('difficulty'))->links()}}
</div>
</div>
#stop
Help im newbie here i hope someone can help me out with this one hoping for a reply thanks
Just call:
Post::whereReview(1)->paginate(9);
Please read the documentation (and also the one for the query builder since these pages apply to Eloquent as well)
You can just add a where() call to the query:
$posts = Post::where('review', '=', 1)->paginate(9);
Or a shorter version (= is the default operator)
$posts = Post::where('review', 1)->paginate(9);
Or even with a dynamic method name:
$posts = Post::whereReview(1)->paginate(9);
Also you can use true instead of 1. Laravel will convert it:
$posts = Post::whereReview(true)->paginate(9);
There is also no need to do chunking that complicated:
#foreach (array_chunk($posts->getCollection()->all(),2) as $row)
You can just use the chunk method on any collection:
#foreach ($posts->chunk(2) as $row)

Laravel 4: how to display a preview of a post using eloquent ORM/blade

I built a simple cms in Laravel 4. At the moment the main site index shows a list of all posts including the body of the post.
I want to only show, for example, the first 100 letters of the body of my post as a preview before viewing the whole article on my show.blade.php page.
The relevant part of the index.blade looks like this:
#foreach($posts as $post)
...
<p>{{ $post->body }}</p>
And the relevant part of my DisplayController looks like this:
public function index()
{
$posts = $this->post->orderBy('id', 'DESC')->get();
return View::make('index', compact('posts'));
}
How would I display only the first 100 characters of each post on my index page (as an example)?
#foreach($posts as $post)
<p>{{ substr($post->body, 0, 100) }}</p>
#endforeach

Categories