Method Illuminate\Database\Eloquent\Collection::paginate does not exist using relations tales - laravel

I created a page using the pagination feature, when I opened the blog page everything went well, but when I opened the blog page based on the username from the user table there was an error Method Illuminate\Database\Eloquent\Collection::paginate does not exist. BTW it uses the same view.
blog.blade.php
#extends('layouts.main')
#section('container')
<main class="mt-20 mb-5 w-[800px] mx-auto">
#foreach ($blogs as $blog)
<article class="mb-5">
{{-- judul --}}
<h2 class="text-blue-600 text-2xl mb-2 font-semibold">{{ $blog->title }}</h2>
{{-- penulis category --}}
<p>Author : {{ $blog->user->name }} | Category : {{ $blog->category->name }}</p>
<p class="mt-2 pb-2 border-b border-grey-500">{{ Str::limit($blog->body, 100) }}</p>
</article>
#endforeach
{{ $blogs->links() }}
</main>
#endsection
BlogController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Blog;
class BlogController extends Controller
{
public function index() {
return view('blog', [
'title' => 'All Blog',
'blogs' => Blog::latest()->paginate(5)
]);
}
public function show(Blog $blog) {
return view('show_blog.index', [
'title' => $blog->title,
'blog' => $blog
]);
}
}
UserController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
class UserController extends Controller
{
public function blogFromUser(User $user) {
return view('blog', [
'title' => $user->name,
'blogs' => $user->blog->paginate(5)
]);
}
}
Blog.php *relate to user (belongsTo)
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Blog extends Model
{
use HasFactory;
public function category() {
return $this->belongsTo(Category::class);
}
public function user() {
return $this->belongsTo(User::class);
}
}
)*
User.php *related to Blog (hasMany)
<?php
namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
public function Blog() {
return $this->hasMany(Blog::class);
}
}
)*

When you do :
$user->blog
it will execute the query and retrieve the results, so $user->blog will be a collection.
If you want to paginate it, you need to call paginate on the query/relation, not the result, so you need to do something like :
$user->blog()->paginate(5)
then to access other pages, you will have to append ?page=xy to the page url
be aware that if you have multiple paginations within the same page you will need to change the name of the parameter to avoid conflict like so :
$user->blog()->paginate(5, '*', 'blog')

Related

Laravel Import Excel into DB: ErrorException Undefined array key "name"

I am trying to use the Maatwebsite\Excel package to let users import a csv or excel file and it get imported into the DB.
I am new to laravel, so I am not quite sure how to troubleshoot this issue.
I keep getting the error:
ErrorException Undefined array key "FIRST"
http://127.0.0.1:8000/import-form
CSV Sample Data
FIRST,LAST,EMAIL,PHONE,DEPARTMENT,LOCATION
test name 1,teast last 1,test#mail.com,123-123-1231,test department,test location
Routes:
Route::post('/import-form', [ImportPatientController::class, 'importForm'])->name('import.file');
ImportPatientController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\ImportPatientModel;
use Excel;
use App\Imports\PatientImport;
use App\Http\Controllers\Controller;
class ImportPatientController extends Controller
{
public function importUploadForm()
{
return view('import-form');
}
public function importForm(Request $request)
{
Excel::import(new PatientImport,$request->file2);
return "Record are imported successfully!";
}
}
PatientImport.php (Imports Folder)
<?php
namespace App\Imports;
use App\Models\ImportPatientModel;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
class PatientImport implements ToModel, WithHeadingRow
{
/**
* #param array $row
*
* #return \Illuminate\Database\Eloquent\Model|null
*/
public function model(array $row)
{
return new ImportPatientModel([
'firstName'=>$row['FIRST'],
'lastName' => $row['LAST'],
'email' => $row['EMAIL'],
'phone' => $row['PHONE'],
'department' => $row['DEPARTMENT'],
'location' => $row['LOCATION'],
]);
}
}
ImportPatientModel.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class ImportPatientModel extends Model
{
use HasFactory;
protected $table = "imported_patients";
protected $fillable = ['firstName', 'lastName', 'email', 'phone', 'department', 'location'];
}
import-form.blade.php
<form action="" method="post" enctype="multipart/form-data" action="{{route('import.file')}}">
<img class="flowhealthlogoform" src="{{ url('images/flowhealthlogo.png')}}" />
<h1> BACKUP LIS </h1>
<!-- CROSS Site Request Forgery Protection -->
#csrf
<div class="form-group">
<label>Upload Excel Sheet</label>
<input type="file" class="form-control {{ $errors->has('file') ? 'error' : '' }}" name="file2" id="file">
<!-- Error -->
#if ($errors->has('file'))
<div class="error">
{{ $errors->first('file') }}
</div>
#endif
</div>
<input type="submit" name="send" value="Submit" class="btn btn-dark btn-block">
</form>```
Array keys in PHP are case sensitive.
I think if you change $row['FIRST'] to $row['first'] the issue will be solved!

Eloquent: relationships between blog tables

someone would have a practical example of using relationships in Eloquent as follows: I have a blog with several categories, in these categories I will have several Posts, as I do to display a category with several Post in Views. Any practical examples for me to study my logic? I've seen several here, but none fit into what I want above.
Model Post:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
public function category()
{
return $this->belongsTo('App\Category');
}
public function tags()
{
return $this->belongsToMany('App\Tag');
}
public function comments()
{
return $this->hasMany('App\Comment');
}
}
Controller:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Post;
use Mail;
use Session;
use App\Category;
class PagesController extends Controller {
public function getIndex() {
$posts = category::find(1)->posts()->orderBy('created_at', 'desc');
return view('v1.index')->withPosts($posts);
// $posts = Post::orderBy('created_at', 'desc')->limit(3)->get();
// $categorias = Category::find(1);
// return view('v1.index')->withPosts($posts)->withCategorias($categorias);
}
public function getContact() {
return view('v1.contato');
}
public function postContact(Request $request) {
$this->validate($request, [
'email' => 'required|email',
'subject' => 'min:3',
'message' => 'min:10']);
$data = array(
'email' => $request->email,
'subject' => $request->subject,
'bodyMessage' => $request->message
);
Mail::send('emails.contact', $data, function($message) use ($data){
$message->from($data['email']);
$message->to('hello#devmarketer.io');
$message->subject($data['subject']);
});
Session::flash('success', 'Your Email was Sent!');
return redirect('/');
}
}
Model Category:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
protected $table = 'categories';
public function posts()
{
return $this->hasMany('App\Post');
}
}
View index
<div class="col-sm-6">
<div id="home-slider">
#foreach($posts as $post)
<div class="post feature-post">
<div class="entry-header">
<div class="entry-thumbnail">
<img class="img-responsive" src="{{ asset('imgs/'.$post->image) }}" width="572" height="350" alt="" />
<div class="catagory world">{{ $post->category->name }}</div>
</div>
<div class="post-content">
<h2 class="entry-title">
{{ $post->title }}
</h2>
</div>
</div><!--/post-->
#endforeach
</div>
</div>
First get the category you want to display
$category = Category::find(1);
Then Get all the post related to that category by skipping the first 3
$posts = Post::where('category_id', $category->id)
->skip(3)
->take(6)
->get();
Pass them to your view
return view('v1.index', compact('posts'));
In your view, wherever you want, loop them with blade exactly the way you are doing already.
#foreach($posts as $post)
#endforeach

Laravel simple form not setting errors and session variables

I am new to Laravel and I am trying to create a simple form that adds a record into a database table (that has 2 fields: ID and name).
Here is the code I have so far:
routes.php
Route::group(['middleware' => ['web']], function () {
Route::get('/banks/add', 'BanksController#add');
Route::post('/banks/add', 'BanksController#store');
});
BanksController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Requests\BankFormRequest;
use App\Bank;
class BanksController extends Controller
{
public function add() {
return view('banks.add');
}
public function store(BankFormRequest $request) {
$bank = new Bank(array(
'name' => $request->get('name'),
));
$bank->save();
return redirect('/banks/add')->with('status', 'Your bank has been created! Its name is: '.$request->get('name'));
}
}
BankFormRequest.php
<?php
namespace App\Http\Requests;
use App\Http\Requests\Request;
class BankFormRequest extends Request
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'name' => 'required|unique:banks|max:255',
];
}
}
Bank.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Bank extends Model
{
public $timestamps = false;
protected $guarded = ['id'];
}
banks/add.php
<form id="registerForm" role="form" method="post">
<pre><?=var_dump($errors)?></pre>
<pre>
<?php if (session()->has('status')): ?>
<?=session('status')?>
<?php endif; ?>
</pre>
<?php if (isset($errors) && $errors->any()): ?>
<?php foreach ($errors->all() as $error): ?>
<p class="alert alert-danger"><?=$erorr?></p>
<?php endforeach; ?>
<?php endif; ?>
<input type="hidden" name="_token" value="<?=csrf_token()?>">
<div class="form-group">
<label>Name</label>
<input type="text" class="form-control" placeholder="Bank name" name="name">
</div>
<button type="submit" class="btn btn-primary">Add</button>
</form>
banks table
CREATE TABLE `banks` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8
In the banks/add view I have the form and 2 var_dumps of the $errors and session('status') variables, but they are always NULL. Otherwise, the form validator works well and it inserts my input into the database if it passes the rules I defined.
Anyone knows what causes my errors to not be shown?
in your banks/add.php
<p class="alert alert-danger"><?=$erorr?></p>
$erorr is wrong
If you are using laravel 5.2 try to remove web middleware on the route group this will fix your problem. why? because the RouteServiceProvider class will add it for you.
line 53 - 60 as of this writing.
protected function mapWebRoutes(Router $router)
{
$router->group([
'namespace' => $this->namespace, 'middleware' => 'web',
], function ($router) {
require app_path('Http/routes.php');
});
}

Individual profile pages and searches Laravel 5.2

Having a real confusing time with this project, my issue is I'm trying to get my search working but for some reason its not pulling results from my query when there is that information in the database, also when I click on the username in the top corner of my page, it should redirect to the user page but instead I get this error "NotFoundHttpException in Application.php line 879:" with the URl looking like this "http://localhost/WorldLink/users/firstName%20=%3E%20Auth::user%28%29-%3EfirstName" and I have exhausted all other means of trying to fix it so I'm back for some help! my code is below Im using laravel 5.2:
Users.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Users extends Model
{
protected $table = 'users';
protected $fillable = [
'id', 'firstName', 'lastName', 'bio', 'homeLocation', 'currentLocation', 'email', 'password',
];
public function getName()
{
if ($this->firstName && $this->lastName) {
return "{$this->firstName} {$this->lastName}";
}
if ($this->firstName) {
return $this->firstName;
}
return null;
}
public function getNameOrLocation()
{
return $this->getName() ?: $this->currentLocation;
}
public function getFirstNameOrLocation()
{
return $this->firstName ?: $this->currentLocation;
}
public function getAllAvatarsUrl()
{
return "https://www.gravatar.com/avatar/{{ md5($this->email) }}?d=mm&s=40";
}
}
SearchController.php:
<?php
namespace App\Http\Controllers;
use DB;
use App\Users;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Http\Request;
class SearchController extends BaseController
{
public function getResults(Request $request)
{
$query = $request->input('query');
if (!$query) {
return back();
}
$users = Users::where(DB::raw("CONCAT(firstName, ' ', lastName)"), '
LIKE', "%{$query}%")
->orWhere('currentLocation', 'LIKE', "%{$query}%")
->get();
return view('search/results')->with('users', $users);
}
ProfileController.php
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Http\Request;
class ProfileController extends BaseController
{
public function getProfile($firstName)
{
$users = User::where('firstName', $firstName)->first();
if (!$users) {
abort(404);
}
return view('profile.index')
->with('users', $users);
}
}
userblock.blade.php
<div class="media">
<a class="pull-left" href="{{ route('profile/index', ['firstName' => $users->firstName]) }}">
<img class="media-object" alt="{{ $users-getNameOrLocation() }}" src="{{ $users->getAllAvatarsUrl() }}">
</a>
<div class="media-body">
<h4 class="media-heading">{{ $users->getNameOrLocation() }}</h4>
</div>
#if ($users->currentLocation)
<p>{{ $users->currentLocation }}</p>
#endif
results.blade.php
#extends('layouts.app')
#section('content')
<h3>Search Results for "{{ Request::input('query') }}"</h3>
#if (!$users->count())
<p>No Results Found</p>
#else
<div class="row">
<div class="col-lg-12">
#foreach ($users as $user)
#include('users/partials/userblock')
#endforeach
</div>
</div>
#endif
#endsection
And finally my two routes, the problem is connected in here somewhere I just cant find where its going wrong.
Route::get('/search', [
'uses' => '\App\Http\Controllers\SearchController#getResults',
'as' => 'search/results',
]);
Route::get('/users/{firstName}', [
'uses' => '\App\Http\Controllers\ProfileController#getProfile',
'as' => 'profile/index',
]);
The Link:
#if (Auth::guest())
<li>Login</li>
<li>Register</li>
#else
<ul class="nav navbar-nav">
<form class="navbar-form navbar-left" role="search" action="{{ route('search/results') }}">
<input type="text" class="form-control" placeholder="Search" name="query">
</form>
<li>{{ Auth::user()->firstName }}</li>
<li>Timeline</li>
<li>Link</li>
<li>Journeys <span class="journey-num">{{ Auth::user()->journeys }}</span></li>
<li>Forum</li>
</ul>
Defo quoting incorrectly
....
<li>{{ Auth::user()->firstName }}</li>
....
Note closing ' moved to after firstName array key.
That should at least fix the link

How to check if item exists in pivot table then show a message in the view depending on result?

I want to be able to check if the current user that is signed in has any jobs in their watchlist table. If they have I want to display a button that says "Currently watching" else have a button that says "Add to watchlist" in my view called viewListings which displays all of the listings listed in the job_listings table.
I have the logic working in the ListingController. But I need to know how I can apply this logic in my view: viewListings. How do I do this?
I have the following database setup:
ListingController:
<?php namespace App\Http\Controllers;
use App\JobListing;
use App\User;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\CreateListingRequest;
use App\Watchlist;
// use Illuminate\Http\Request;
use Request;
use Session;
use Auth;
use Carbon\Carbon;
use Input;
use Redirect;
use URL;
use File;
use Hugeform;
// use Symfony\Component\HttpFoundation\File;
class ListingController extends Controller {
public function __construct()
{
// This will send the user to the login page if they are not
// logged in except for the index page
$this->middleware('auth', ['except' => ['index', 'show']]);
}
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
$listings = JobListing::all();
$joblisting_ids = JobListing::lists('job_id');
// This is the logic that is working, but need to get this to the view?
foreach ($joblisting_ids as $id) {
if (Watchlist::where('user_id', '=', Auth::user()->id)->where('job_id', '=', $id)->get()->first()) {
echo 'Currently watching this'.'<p>';
} else {
echo 'Add to watchlist'.'<p>';
}
}
// Need to pass something to the view here so I can print out if the job is in the watchlist table or not
// return view('listings.viewListings', compact('listings'));
}
viewListings.blade.php
<div class="container">
<div class="row">
<div class="col-md-10 col-md-offset-1 inner-content">
#if(Session::has('flash_message'))
<div class="alert alert-success listing-success" role="alert">{!! Session::get('flash_message') !!}</div>
#endif
<h2>Listings page</h2>
{!! HTML::link('/listings/create', 'Create a Job Listing', ['class' => 'btn btn-success']) !!}
{!! HTML::link('/listings/myjobs', 'My Jobs', ['class' => 'btn btn-info']) !!}
{!! HTML::link('watchlist', 'My Watchlist', ['class' => 'btn btn-primary']) !!}
<hr>
#if (Auth::check())
<h4> Welcome {{ Auth::user()->username }} </h4>
<h3> Welcome ID: {{ Auth::user()->id }} </h3>
<h3> Another Version ID: {{ Auth::id() }} </h3>
#endif
#foreach ($listings as $listing)
<div class="job-full-wrap">
<div class="col-md-10">
<div class="job-left-wrapper">
<h3>{{ $listing->position_title }} </h3>
<h5>{{ $listing->company_name }} | {{ $listing->city }} | {{ $listing->job_type }} </h5>
<!-- Need to add logic in here to show either a link or button that will say 'Add to watchlist' or 'currently watching' -->
<!-- <a class="add-watchlist" href="watchlist/add/{{ $listing->job_id }}" id="{{ $listing->job_id }}">Add to watchlist</a> -->
</div>
</div>
<div class="col-md-2">
<div class="job-right-wrapper">
<img class="img-responsive" src="{{ '../uploaded_images/'.$listing->logo_path }}" alt="">
</div>
</div>
<div class="clearfix"></div>
</div>
#endforeach
</div>
</div> <!-- End of row -->
</div>
Watchlist model:
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Watchlist extends Model {
protected $primaryKey = 'job_id';
protected $table = 'watchlist';
protected $fillable = ['user_id', 'job_id'];
}
User model:
<?php namespace App;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Model implements AuthenticatableContract, CanResetPasswordContract {
use Authenticatable, CanResetPassword;
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'users';
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = ['username','firstname', 'lastname', 'email', 'password', 'role'];
/**
* The attributes excluded from the model's JSON form.
*
* #var array
*/
protected $hidden = ['password', 'remember_token'];
public function watchlist() {
return $this->belongsToMany('\App\JobListing', 'watchlist', 'user_id', 'job_id');
}
}
JobListing model:
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class JobListing extends Model {
protected $primaryKey = 'job_id';
protected $table = 'job_listings';
protected $fillable = ['user_id', 'position_title', 'description', 'category', 'job_type',
'city','company_name','company_url','logo_path','how_to_apply', 'expiry_date', 'status'];
}
For your controller function, you can set a dynamic property on each listing item as you discover whether the listing is currently watched or not.
public function index()
{
$listings = JobListing::all();
foreach ($listings as $listing) {
if (Watchlist::where('user_id', '=', Auth::user()->id)->where('job_id', '=', $listing->job_id)->get()->first()) {
$listing->watching = true;
} else {
$listing->watching = false;
}
}
// Need to pass something to the view here so I can print out if the job is in the watchlist table or not
// return View::make('listings.viewListings')->with(['listings' => $listings]);
}
Inside your foreach loop in your view, you now have access to the property which you can check like so:
#foreach ($listings as $listing)
#if ($listing->watching)
//"Currently watching" button
#else
//"Add to watchlist" button
#endif
#endforeach

Resources