Call to a member function get() on null for a function in a model - laravel-5

I am trying to have a button of an employee show the text “Already booked” if the variable
count($bookingRequests) returns something and if nothing then show “Book Me’
I get the error above.
I have the following models and their relationships:
User.php and Bookings.php
User:
public function bookings() {
return $this->hasMany('App\Models\Bookings');
}
public function BookingRequest()
{
$this->bookings()->where('booking_status','=','confirmed')->get();
}
Bookings:
public function user(){
return $this->belongsTo('App\Models\User');
}
The view mentioned above is Employeeblock.blade.php:
<div class="row no-gutters mb-5 mb-lg-0" style="padding:10px">
#if(count($bookingRequests))
<a href="#" class="btn btn-primary" data-
toggle="modal" data-target=“#bookingModal">Already Booked</a>
#else
<a href="#" class="btn btn-primary" data-
toggle="modal" data-target="#bookingModal">Book Me</a>
#endif
</div>
Which has data passed onto it by a controller SearchController:
In the following way:
<?php
namespace App\Http\Controllers;
use DB;
use Auth;
use App\Models\Employee;
use App\Models\User;
use App\Models\Bookings;
use Illuminate\Http\Request;
use GuzzleHttp\Client;
use Session;
.
.
.
$bookingRequests=Auth::user()->BookingRequest()->get();
return view(‘search.results’)
->with('bookingRequests',$bookingRequests)
results.blade.php above has the following html:
<div class="panel panel-default">
<div class="panel-heading"><h3></h3></div>
<div class="panel-body">
<div class="row">
<div class="col-lg-12">
#foreach($Employees as $Employee)
#include('user/partials/employeeblock')
#endforeach
</div>
</div>
</div>
</div>
</div>

Replace:
$bookingRequests=Auth::user()->BookingRequest()->get();
with
$bookingRequests=Auth::user()->BookingRequest();
Because your BookingRequest() method already use ->get() method to obtain data from db.

Related

How do I do pagination on my code in Laravel?

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 update the picture with the spatie media library and my picture does not appear

my controller:
public function update(SettingUpdateRequest $request, Setting $id){
$setting = Setting::first();
$setting->update($request->post());
if ($request->hasFile('site_logo')) {
$setting->addMedia($request->site_logo)->toMediaCollection('document');
}
return redirect()
->route('back.setting.index')->withMessage('Ayarlar başarıyla güncellendi!');
}
my model:
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
class Setting extends Model implements HasMedia
{
use HasFactory , InteractsWithMedia;
protected $table='settings';
protected $fillable=['site_title','site_desc','site_email'];
protected $appends = ['site_logo'];
public function getSiteLogoAttribute()
{
return $this->getMedia('document')->last();
}
}
my picture way in stroge:
https://prnt.sc/11q22mc
my blade:
<div class="form-group">
<label for="exampleInputFile">Site Logo</label>
<input type="file" id="exampleInputFile" name="site_logo" class="form-control" accept="image/*">
</div>
<label><strong>Resimler:</strong></label>
#if (!$setting->site_logo)
<span class="form-text text-muted">Bu Rehber ile ilgili resim bulunamadı. Yeni bir resim ekleyin!
</span>
#endif
<div class="col-lg-3">
<div class="image-input image-input-empty image-input-outline" id="kt_image_5" style="background-image: url({{$setting->getFirstMediaUrl('document')}})">
<div class="image-input-wrapper"></div>
<span class="btn btn-xs btn-icon btn-circle btn-white btn-hover-text-primary btn-shadow" data-action="cancel" data-toggle="tooltip" title="" data-original-title="Cancel avatar">
<i class="ki ki-bold-close icon-xs text-muted"></i>
</span>
<span class="btn btn-xs btn-icon btn-circle btn-white btn-hover-text-primary btn-shadow" data-action="remove" data-toggle="tooltip" title="" data-original-title="Remove avatar">
</span>
</div>
</div>
my database:
https://prnt.sc/11q2b2l
I update the picture with the spatie media library and my picture does not appear.
https://prnt.sc/11q2fvi
my console fault:
https://prnt.sc/11q2k8q
You should retrieve the last media in your blade. As you've already defined a getter in your model, you can use below code.
{{ $setting->site_logo->getUrl() }}
you should delete last image
$setting->media()->delete();
or you can use
$setting->clearMediaCollection('document');

Show counter result in dashboard

I would like to create a dasboard with Laravel 8. I want to count all tickets in the database and display the number in the dashboard. Unfortunately it does not work do you have an idea?
Controller Code
namespace App\Http\Controllers;
use App\Models\Ticket;
use Illuminate\Http\Request;
class DashboardController extends Controller
{
public function index()
{
//
$ticketsCount = Ticket::count();
return view('dashboard.index', compact('ticketsCount'));
}
}
View Code
<div class="col-lg-3 col-6">
<!-- small box -->
<div class="small-box bg-info">
<div class="inner">
<h3>{{ $ticketsCount->count() }}</h3>
<p>Open Tickets</p>
</div>
<div class="icon">
<i class="ion ion-bag"></i>
</div>
More info <i class="fas fa-arrow-circle-right"></i>
</div>
</div>
As I can see, your router returns view file, and not getting into controller.
Route::get('/dashboard', function () {
return view('dashboard.index');
});
change your router to (Laravel version before 8)
Route::get('/dashboard', 'DashboardController#index');
After Laravel version 8
use App\Http\Controllers\DashboardController;
Route::get('/dashboard', [DashboardController::class, 'index']);
Docs
Remove ->count() from your view, you already counted it in controller
$ticketsCount = Ticket::count();
The view code:
<div class="col-lg-3 col-6">
<!-- small box -->
<div class="small-box bg-info">
<div class="inner">
<h3>{{ $ticketsCount }}</h3>
<p>Open Tickets</p>
</div>
<div class="icon">
<i class="ion ion-bag"></i>
</div>
More info <i class="fas fa-arrow-circle-right"></i>
</div>
</div>
I have tried to get the number of users displayed on the view. Unfortunately I always get an error message...
Error:
ErrorException
Undefined variable: counter (View: /laravel/resources/views/dashboard/index.blade.php)
Route (web):
Route::get('/dashboard', function () {
return view('dashboard.index');
});
Model (Dashboard)
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Dashboard extends Model
{
use HasFactory;
}
Controller (DashboardController)
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
class DashboardController extends Controller
{
public function index()
{
$counter = DB::table('users')->count();
return view('dashboard.index', compact('counter'));
}
}
View (dashboard/index.blade.php):
Users: {{ $counter }}

Cant Display my Reply under the Comment Laravel 5.7

I can't display my reply under each comment. Here is my code...
Comment model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
public function commentable()
{
return $this->morphTo();
}
public function user()
{
return $this->belongsTo('App\User');
}
public function comments()
{
return $this->morphMany('App\Comment', 'commentable');
}
}
Post model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
protected $fillable = [
'user_id', 'topic', 'body', 'category',
];
public function user()
{
return $this->belongsTo('App\User');
}
public function comments()
{
return $this->morphMany('App\Comment', 'commentable');
}
}
Comment controller
<?php
namespace App\Http\Controllers;
use App\Comment;
use App\Post;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class CommentController extends Controller
{
public function store(Request $request, $post)
{
$comment = new Comment;
$comment->body = $request->body;
$comment->user_id = Auth::user()->id;
$post = Post::find($post);
$post->comments()->save($comment);
return back();
}
public function replyStore(Request $request, $comment)
{
$comment = new Comment;
$comment->body = $request->body;
$comment->user_id = Auth::user()->id;
$comment = Comment::find($comment);
$comment->comments()->save($comment);
return back();
}
}
Routes
Route::post('/comment/store/{post}', 'CommentController#store')->name('comment.add');
Route::post('/reply/store/{commentid}', 'CommentController#replyStore')->name('reply.add');
View
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-12">
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<div class="card">
<div class="card-header">{{$post->topic}}
Create New Post
</div>
<div class="card-body">
#if (session('status'))
<div class="alert alert-success" role="alert">
{{ session('status') }}
</div>
#endif
<h3>{{$post->topic}}</h3>
<p>{{$post->body}}</p>
</div>
</div>
<form action="/comment/store/{{$post->id}}" method="post" class="mt-3">
#csrf
<div class="form-group">
<label for="">Comment :</label>
<textarea class="form-control" name="body" id="" rows="3"></textarea>
<br>
<input type="submit" value="Comment" class="btn btn-secondary">
</div>
</form>
<div class="row mt-5">
<div class="col-md-10 mx-auto">
<h6 style="border-bottom:1px solid #ccc;">Recent Comments</h6>
#foreach($post->comments as $comment)
<div class="col-md-12 bg-white shadow mt-3" style="padding:10px; border-radius:5px;">
<h4>{{$comment->user->name}}</h4>
<p>{{$comment->body}}</p>
<button type="submit" class="btn btn-link" onclick="toggleReply({{$comment->id}})">
Reply
</button>
<div class="row">
<div class="col-md-11 ml-auto">
{{-- #forelse ($replies as $repl)
<p>{{$repl->body}}</p>
#empty
#endforelse --}}
</div>
</div>
</div>
<form action="/reply/store/{{$comment->id}}" method="post"
class="mt-3 reply-form-{{$comment->id}} reply d-none">
#csrf
<div class="form-group">
<textarea class="form-control" name="body" id="" rows="3"></textarea>
<br>
<input type="submit" value="Reply" class="btn btn-secondary">
</div>
</form>
#endforeach
</div>
</div>
</div>
</div>
</div>
#endsection
#section('js')
<script>
function toggleReply(commentId) {
$('.reply-form-' + commentId).toggleClass('d-none');
}
</script>
#endsection
I have created the normal table with parent_id but I don't know how to display the replies for each comment. Please, anyone who can help me with this - I am stranded here and the error coming from the second controller function which is replystore() saying it doesn't recognize the comments() method. Please help me out to display the reply.

Two controllers one route laravel

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.

Resources