I'm trying to find the slugs complete and incomplete model TaskboardColumn, then I only want the values of the user and the project.
Here is what I've tried:
Model
public static function projectOpenTasks($projectId, $userID=null)
{
$taskBoardColumn = \App\TaskboardColumn::where('slug', 'incomplete')->first();
$taskBoardColumn2 = \App\TaskboardColumn::where('slug', 'inprogress')->first();
$projectTask = \App\Task::where('tasks.board_column_id', $taskBoardColumn->id)->orWhere('tasks.board_column_id', $taskBoardColumn2->id);
if($userID)
{
$projectIssue = $projectTask->where('user_id', '=', $userID);
}
$projectIssue = $projectTask->where('project_id', $projectId)
->get();
return $projectIssue;
}
Controller
$this->openTasks = Task::projectOpenTasks($this->project->id);
View
<ul class="list-task list-group" data-role="tasklist">
<li class="list-group-item" data-role="task">
<strong>#lang('app.title')</strong>
<span class="pull-right"><strong>#lang('app.dueDate')</strong></span>
</li>
#forelse($openTasks as $key=>$task)
<li class="list-group-item row" data-role="task">
<div class="col-xs-8">
{{ ($key+1).'. '.ucfirst($task->heading) }}
</div>
<label class="label label-danger pull-right col-xs-4">{{ $task->due_date->format($global->date_format) }}</label>
</li>
#empty
<li class="list-group-item" data-role="task">
#lang('messages.noOpenTasks')
</li>
#endforelse
</ul>
With this code I'm getting all the tasks, and I need just this project's tasks.
You can use grouping to achieve this
$projectTask = \App\Task::where(function($q) {
$q->where('tasks.board_column_id', $taskBoardColumn->id);
$q->orWhere('tasks.board_column_id', $taskBoardColumn2->id)
})->get();
Thanks
orwhere is used in places where only one of the query has to run. just like and keyword in programming orwhere is used in sense of query.
following is the code snippet -
$projectTask = $query->where('tasks.board_column_id', $taskBoardColumn->id)
->orWhere('tasks.board_column_id', $taskBoardColumn2->id)
->get();
Related
I am working on a blogging application in Laravel 8.
The ArticlesController controller I have this method to display the paginated articles:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
use App\Models\ArticleCategory;
use App\Models\Article;
use App\Models\Comment;
class ArticlesController extends FrontendController {
// Articles per page
protected $per_page = 12;
public function index(Request $request) {
// Search query
$qry = $request->input('search');
$articlesQuery = Article::where('title', 'like', '%' . $qry . '%')
->orWhere('short_description', 'like', '%' . $qry . '%')
->orWhere('content', 'like', '%' . $qry . '%');
// Search results count
if ($qry) {
$article_count = $articlesQuery->count();
}
$articles = $articlesQuery->orderBy('id', 'desc')->paginate($this->per_page);
$featured_articles = Article::where('featured', 1)->orderBy('id', 'desc')->get();
return view('themes/' . $this->theme_directory . '/templates/index',
array_merge($this->data, [
'search_query' => $qry,
'articles' => $articles,
'featured_articles' => $featured_articles,
'article_count' => $article_count ?? null
])
);
}
}
The pagination, in the plain HTML template, looks like this:
<nav class="pgn">
<ul>
<li>
<a class="pgn__prev" href="#0">Prev</a>
</li>
<li><a class="pgn__num" href="#0">1</a></li>
<li><span class="pgn__num current">2</span></li>
<li><a class="pgn__num" href="#0">3</a></li>
<li><span class="pgn__num dots">…</span></li>
<li><a class="pgn__num" href="#0">4</a></li>
<li>
<a class="pgn__next" href="#0">Next</a>
</li>
</ul>
</nav>
The goal
The goal is to keep the pagination structure above, in Laravel's Blade.
The problem
The code below works for the "Next" and "Prev" buttons, but not the links in between.
<nav class="pgn">
<ul>
<li>
<a class="pgn__prev" href="{{ $articles->withQueryString()->previousPageUrl() }}">Prev</a>
</li>
{!! $articles->withQueryString()->links() !!}
<li>
<a class="pgn__next" href="{{ $articles->withQueryString()->nextPageUrl() }}">Next</a>
</li>
</ul>
</nav>
Questions
What causes this bug?
What is the easiest fix?
To create a custom pagination, I'd recommend to make use of a custom "view".
Basically, what would you have to do (which you have already done), is define a limit, and then basically do the following:
you create your view file (which will be the custom paginator) - name it however you want to name it. I'll name it custom.blade.php
This view has to be created after running the command: php artisan vendor:publish --tag=laravel-pagination
here you can find a bit more in the documentation about it: https://laravel.com/docs/9.x/pagination#customizing-the-pagination-view
#if ($paginator->onFirstPage())
<li class="disabled"><span>← Previous</span></li>
#else
<li>
<a class="pgn__prev" href="{{ $articles->withQueryString()->previousPageUrl() }}">Prev</a>
</li>
#endif
#foreach ($elements as $element)
#if (is_string($element))
<li class="disabled"><span>{{ $element }}</span></li>
#endif
#if (is_array($element))
#foreach ($element as $page => $url)
#if ($page == $paginator->currentPage())
<li class="active my-active"><span>{{ $page }}</span></li>
#else
<li>{{ $page }}</li>
#endif
#endforeach
#endif
#endforeach
#if ($paginator->hasMorePages())
<li>
<a class="pgn__next" href="{{ $articles->withQueryString()->nextPageUrl() }}">Next</a>
</li>
#else
<li class="disabled"><span>Next</span></li>
#endif
And finally, on the view where you want to make use of the custom pagination:
{{ $articles->links(‘path.pagination.custom') }}
(just make sure to have the correct path)
edit: It's a bit hard for me to give a definitive answer without looking at the project itself, but I hope this at least helps somehow.
I want to filter the datas according to colleges and universities and my query is
$exams = Exam::where('status', 1);
if ($request->filled('universities')) {
$exams = $exams->whereIn('university_id', $request->universities); //data 1st
}
if ($request->filled('colleges')) {
$exams = $exams->whereIn('college_id', $request->colleges); //data 2nd
}
$exams = $exams->get();
And my form is
<form action="">
<ul class="list-group list-group-flush">
#foreach($universities as $university)
<li class="list-group-item">
<a href="#">
<label>
<input name='universities[]' onchange="searchExam();"
class="mr-2" type="checkbox"
value='{{$university->id}}'/>
{{$university->name}}
</label>
</a>
</li>
#endforeach
</ul>
<ul class="list-group list-group-flush">
#foreach($colleges as $college)
<li class="list-group-item">
<a href="#">
<label>
<input onchange="searchExam();" name='colleges[]'
class="mr-2" type="checkbox"
value='{{$college->id}}'/>
{{$college->name}}
</label>
</a>
</li>
#endforeach
</ul>
</form>
My problem is I am getting data for only either universities or colleges but what I want is if colleges and universities options are checked, I want to get data for both colleges and universities. How can I merge these two datas into single array?
You would want to group the wheres for the college or university. You can try something like this:
$exams = Exam::where('status', 1);
if ($request->has('universities', 'colleges')) {
$exams->where(function ($q) use ($request) {
if ($request->input('universities')) {
$q->orWhereIn('university_id', (array) $request->input('universities'));
}
if ($request->input('colleges')) {
$q->orWhereIn('college_id', (array) $request->input('colleges'));
}
});
}
You should be able use orWhereIn eloquent function for this: https://github.com/laravel/framework/blob/f802e8134bc898871cd02398621745cee45739ef/src/Illuminate/Database/Query/Builder.php#L974
Exam::where('status', 1)
->orWhereIn('university_id', $request->universities ?? false)
->orWhereIn('college_id', $request->colleges ?? false)
->get();
This should produce (assuming that table name is exams) something like:
"select * from `exams` where `status` = ? and (`university_id` IN ? or `college_id` IN ?)"
If that doesn't work with your PHP version, you could try adding conditional clauses ( https://laravel.com/docs/master/queries#conditional-clauses)
Exam::where('status', 1)
->when($request->universities, function ($query) use ($request) {
return $query->whereIn('university_id', $request->universities);
})
->when($request->colleges, function ($query) use ($request) {
return $query->whereIn('college_id', $request->colleges);
})
->get();
After I click in the menu navigation (for exemple href='home') when i am in the view .../public/single/1 they send me to .../public/single/home and not .../public/home
Here is my code:
MENU LIST
<div class="header_content d-flex flex-row align-items-center justify-content-start">
<div class="logo"><span>m</span>a <span> B</span>ibli<span>o</span></div>
<nav class="main_nav">
<ul class="d-flex flex-row align-items-start justify-content-start">
<li class="{{$home}}">Acceuil</li>
<li class="{{$about}}">About us</li>
<li class="{{$listing}}">Produits</li>
<li class="{{$blog}}">Nouveauté</li>
<li class="{{$contact}}">Contactez nous!</li>
</ul>
</nav>
<div class="submit ml-auto">Recherche >></div>
<div class="hamburger ml-auto"><i class="fa fa-bars" aria-hidden="true"></i></div>
</div>
</div>
WEB.PHP
Route::get('/single/{id}','AnnonceController#annonce');
Route::get('/home','PagesController#home');
Route::resource('home','AnnonceController');
And this is my controller:
public function annonce($id) {
$annonce = DB::table('annonce')
->join('article', 'annonce.ID_ANNONCE', '=', 'article.ID_ANNONCE')
->join('photo_articles', 'photo_articles.ID_ARTICLE', '=', 'article.ID_ARTICLE')
->select('annonce.*', 'article.NOM_ARTICLE','PHOTO_ARTICLE')
->where('annonce.ID_ANNONCE',$id)
->get();
return view('single')->with('annonce',$annonce);
}
It's happening because you giving href='home' which will add to your current URL , which means you are on single/1(route parameter) and when you click it will become single/home(parameter).
so give the full path.
I suggest you should use the route function of laravel and also give the name to your route like below
Route::get('/home','PagesController#home')->name('myhome');
see Documentation for naming route.
then call this in your blade like this
{{route('myhome')}}
You return statement should be like this return view('single',['id' => $id])->with('annonce',$annonce);
Note: Please use relationship in Laravel instead of DB::table
public function annonce($id) {
$annonce = DB::table('annonce')
->join('article', 'annonce.ID_ANNONCE', '=', 'article.ID_ANNONCE')
->join('photo_articles', 'photo_articles.ID_ARTICLE', '=', 'article.ID_ARTICLE')
->select('annonce.*', 'article.NOM_ARTICLE','PHOTO_ARTICLE')
->where('annonce.ID_ANNONCE',$id)
->get();
return view('single',['id' => $id])->with('annonce',$annonce);
}
I have 2 models 'Trips.php'
public function region()
{
return $this->belongsTo('App\Region');
}
Region.php
public function trips()
{
return $this->hasMany('App\Trip');
}
I'm trying to show the trips that are in one one specific region. For example: I have a region named Lake Side and I want to show all trips that are in Lake Side region in list.
I tried the following:
In controller:
$trips = Trip::all();
In view:
#foreach($trips as $trip)
<li><a href="#">
{{$trip->region->name}}</a>
<ul class="dropdown">
<li><a href="#">
{{$trip->title}}</a>
</li>
</ul>
</li>
#endforeach
This gives me region name and trip name but repeats region name if more than one trip is made in same region.
And tried another way around (inverse):
<ul class="dropdown">
#foreach($regions as $region)
<li><a href="#">
{{$region->tour->title}}</a>
</li>
#endforeach
</ul>
And getting error Trying to get property of non-object
One way to do that is to use whereHas():
$trips = Trip::whereHas('region', function($q) use ($regionName) {
$q->where('name', $regionName);
})->get();
Pass $trips and $regionName to the view:
#foreach ($trips as $trip)
<li>{{ $regionName }}
<ul class="dropdown">
<li><a href="#">
{{ $trip->title }}</a>
</li>
</ul>
</li>
#endforeach
Alternatively, you can use eager loading:
$region = Region::where('name', $regionName)->with('trips')->first();
And in the view:
#foreach ($region->trips as $trip)
<li>{{ $region->name }}
<ul class="dropdown">
<li><a href="#">
{{ $trip->title }}</a>
</li>
</ul>
</li>
#endforeach
By getting trips with regions, what you're essentially getting is every trip with a region property.
Instead do the reverse Region::with('trips')->where('id', $regionId)->get() and you'll get regions with their trips. So now every region result has a trips property which contains many trips.
Alternatively don't use eager load. So Region::firstOrFail($regionId) then just use $region->trips.
And you can loop through them like
// Can print region here
#foreach($region->trips as $trip)
// Can print region's trips here
#endforeach
My model code
how we can call this function in blade.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class BasicModel extends Model
{
public static function get_product_count($id){
$query = "select COUNT(sub_id) AS count FROM products WHERE products.sub_id = $id";
print_r($query);
return $query->row_array();
}
}
My view.blade.php code
count in foreach loop or show in all category
#foreach ($r as $row)
<li class="grid-item type-rent">
<div class="property-block">
<img src="{{ URL::to('/template/images/background-images/sub-category-images/' .$row->sub_cat_images. '')}}" alt=""> <!-- <span class="images-count"><i class="fa fa-picture-o"></i> 2</span> <span class="badges">Rent</span> -->
<div class="property-info">
<h4>{{ ucwords(substr($row->sub_cat_name, 0, 22)) }}</h4>
<span class="location">NYC</span>
<div class="price"><strong>Items</strong><span>
<!-- start count code from here -->
$data = $this->BasicModel->count {{ ($row->sub_id) }}
echo $data['count'];
</span></div>
</div>
<!-- <div class="property-amenities clearfix"> <span class="area"><strong>5000</strong>Area</span> <span class="baths"><strong>3</strong>Baths</span> <span class="beds"><strong>3</strong>Beds</span> <span class="parking"><strong>1</strong>Parking</span> </div> -->
</div>
</li>
#endforeach
My BasicController Code
public function grid(Request $request, $id)
{
if ($id == 1) {
$r = DB::table('sub_category')->select('*')->where('cat_id', $id)
->where('sub_status', '1')->orderBy('sub_id', 'asc')->get();
$name = DB::table('category')->where('cat_id', $id)->get();
return view('buy-and-sell/grid', compact('r','name','count'));
}
image for your help
image for your help
problem in this image please solve the problem
Although its no good Practice Accessing the DB in Blade (better do this in the controller and pass the data) you can do:
<div class="price"><strong>Products</strong>
<span>
{{ BasicModel::where('sub_id', $row->sub_id)->count() }}
</span>
</div>
Its not tested, but have a look at the Eloquent docs, the count() method is explained there.
Update: I am not shure if laravel will find the class BasicModel (I never would access Models directly in blade, as stated do this in the controller and pass the data.) So maybe you need to write it with the full Namespace most likely {{ \App\BasicModel::where() }}.