Route Model binding in Laravel - php

I have a Laravel app and a model Category. It has a relation to itself called parent
Category.php
public function parent(): BelongsTo
{
return $this->belongsTo(self::class, 'parent_uuid', 'uuid');
}
I'm trying to make this kind of URL,
ex.: domain.com/mobile-phones/smartphones
('mobile-phones' - category's parent, 'smartphones' - category itself)
In the routes file it should be:
Route::get('/{parent}/{category}', ...)->name('category');
Is there a way to code so i use just one Route Parameter?
For example:
Route::get('/{category:parent}/{category}', ...)->name('category');
Also a way so that in blade files when i generate the url to use just the category.
route('category', ['parent' => $category->parent, 'category' => $category]); // No
route('category', $category); // Yes
I have tried making the route binding as follows:
/{category:parent}/{category}
But it gives the error that the parameter category can be used just once.

I would define the route like this
Route::get('/categories/{parent}/{category}')->name('category');
Because it is easy to read
and on blade run a loop on the devices and just prepare the anchor tag for the route.
#foreach ($smartPhone as $phones)
<a href="/categories/{{ $smartPhone->parent->name }}/{{ $smartPhone->categories }}">
Categories
</a>
#endforeach
or if wanna display the category names
#foreach ($smartPhone as $phones)
<a href="/categories/{{ $smartPhone->parent->name }}/{{ $smartPhone->categories }}">
{{ $smartPhone->category }}
</a>
#endforeach

Related

create counter for blog categories in laravel

I´m traying to create one counter for my blog´s categories. This should appear to the right name side of my category . i´m using my model with variable appends, that after i will use in my blade for show my result in one span. But i don´t know very well how i can use count in my Model. I´m doing this in my model Blog.
my variable appends contain:
protected $appends = [
'custom_fields',
'has_media',
'restaurant',
'blog_category',
'viewer',
'postCounter',
];
i´m traying this:
return $this->blogs()->count();
i have a relation between blog and blog_category with:
public function blogCategory()
{
return $this->belongsTo(\App\Models\BlogCategory::class, 'blog_category_id', 'id');
}
i want to do in my view appear for example:
innovation (2)
in my view i´m doing this:
#foreach($categories as $category)
<li>{{ trans('web.blog_category_'.$category->name) }}<span>{{$category->postCounter}}</span></li>
#endforeach
but always returned me 0, and i have post with categories
updated
With laravel relationship withCount you can do this easily. If you want to count the number of results from a relationship without actually loading them you may use the withCount method, which will place a {relation}_count column on your resulting models.
add withCount method to your query
$categories = Category::withCount('blogCategory')->get();
You can access the count in your foreach loop
// $category->blogCategory_count
#foreach($categories as $category)
<li>
<a href="{{ url('blogs/'.$category->name) }}">
{{trans('web.blog_category_'.$category->name) }}
</a>
<span>{{$category->blogCategory_count}}</span>
</li>
#endforeach

how to show multiple category name from checkbox value in eloquent relationship laravel

firstly, i'm a newbie in laravel.
i insert a array data as a string in server.
column name "category_id" & value like "1,2,3,4".
Now i just want to show these id's category name with eloquent relationship.
Using laravel latest version.
view page
{{$category_var = explode(',',$post->category_id)}}
#foreach($category_var as $category)
#if($category)
<span class="badge mb-3">{{$post->category->category}} </span>
#endif
#endforeach
class page
public function category(){
return $this-> belongsTo(Category::class);
}
Controller page
public function index()
{
$posts = post::paginate(9);
return view('pages/home',compact('posts'));
}
anything else need just ask me.
Thanks
just use the benefits of models and relation like below:
#foreach($posts as $post)
#foreach($post->category as $category)
<span class="badge mb-3">
{{ $category->(what ever you want from category model) }}
</span>
#endforeach
#endforeach

laravel eager loading for self relation

I have the model that has relation to itself like this
public function children() {
return $this->hasMany(AppointmentPart::class,'parent_id');
}
I want to eager loading for queries for this model but because every row can have child with() method doesn't work for me
$parts = AppointmentPart::whereParentId(0)->with('children')->get();
my blade:
<ul id="treeData">
#foreach($parts as $part)
<li id="{{ $part->id }}"
#if(!empty($part->children)) class="folder" #endif>
{{ $part->title }}
#if(!empty($part->children))
#include('appointment::admin.appointment.layout._part_child', array('parts' => $part->children))
#endif
</li>
#endforeach
</ul>
content of _part_child :
<ul>
#foreach($parts as $part)
<li id="{{ $part->id }}" #if(!empty($part->children)) class="folder" #endif>
{{ $part->title }}
#include('appointment::admin.appointment.layout._part_child',['parts'=>$part->children])
</li>
#endforeach
</ul>
how can I do this?
You should use a whereHas() function. Have a look at the official documentation.
If you need even more power, you may use the whereHas and orWhereHas methods to put "where" conditions on your has queries. These methods allow you to add customized constraints to a relationship constraint [...]
So in your case you have to change your method as follows:
$parts = AppointmentPart::with('children')->whereHas('children', function($query) use ($parent_id) {
$query->where('parent_id', '=', $parent_id);
})->get();
you can also create a local scope in order to create a reusable query statement.
This is an example for you. This might help to solve your problem. I have Structure Model which contain organizational structure of a company.
In my StructureController.php
public function index()
{
$parentStructures = Structure::where('parent_id', 0)->get();
return view('structure.structureTreeview', compact('parentStructures'));
}
In my Structure.php, I add with() at the relationship for eager loading.
public function children()
{
return $this->hasMany(Structure::class, 'parent_id')->with('children');
}
And, if you you check with this package here , it won't show any warning.
Reference : check this website here

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 data from 2 tables

Let me explain situation first.
I am on the page with list of skills available such as "Plumber","Carpenter" and "Painter", when I click on one of those skills I want to get a list of handymans that have that skill and once clicked on a handyman I get full details about him.
Skills are displayed however when I click on one of the skills it doesn't want to retrieve any data. Both tables "handymen" and "skills" have many to many relationship and also there is a junction table. What am I doing wrong here?
Route::group(['middleware' => ['web']], function () {
Route::get('home', 'HandymanController#home');
Route::get('search', 'HandymanController#search');
Route::get('details/{handyman}', 'HandymanController#details');
Route::post('assignjob', 'HandymanController#assignJob');
Route::get('addjob', 'HandymanController#addJob');
Route::post('addjform', 'HandymanController#addjForm');
Route::get('jobs', 'HandymanController#jobs');
Route::get('jobsdetails/{jobId}', 'HandymanController#jobsdetails');
Route::get('deletejob', 'HandymanController#deleteJob');
Route::post('deletejform', 'HandymanController#deletejForm');
Add Job View:
#extends('layouts.master')
#section('title', 'Add Job')
#section('header2')
<ul>
<li>Assign job</li>
</ul>
#show
#section('content')
<h1>Handyman details</h1>
<ul>
#foreach ($handymen as $handyman)
<a href= "{{ url("HandymanController#details", $handyman->id) }}">
{{$handyman->name}}
</a>
#endforeach
</ul>
Controller:
function search()
{
$skills = Skill::all();
return view('layouts/search',['skills' => $skills]);
}
function details($skillId)
{
$skill = Skill::find($skillId);
$handymen = $skill->handymen;
return view('layouts/details', ['handymen' => $handymen]);
}
According to your edited question, first you list all the skills in your search view. So, in your search view, you would have something like this:
#foreach ($skills as $skill)
<a href= "{{ action("HandymanController#addjForm", $skill->id) }}">
{{ $skill->name }}
</a>
#endforeach
So you have to define in your routes file this route:
Route::post('addjform/{skill}', 'HandymanController#addjForm');
This will point to Handyman Controller, where you will list all handymen that have that skill:
public function addjForm(Request $request, Skill $skill)
{
$handymen = $skill->handymen;
return view('layouts/skilledHandymen', ['skill' => $skill,'handymen' => $handymen]);
}
For this to work, you have to define in Skill Model, the association:
public function handymen()
{
return $this->belongsToMany(Handyman::class,
'handyman_skill',
'skill_id',
'handyman_id');
}
The controller will point to a view where you will list all handymen that have such skill:
In your case, it would be easier if you define an association in Skill model that links to Handyman:
#foreach ($handymen as $handyman)
<a href= "{{ action("HandymanController#details", $handyman->id) }}">
{{ $handyman->name }}
</a>
#endforeach
When you choose a handyman, it will take you to Handyman controller details:
function details(Request $request, Handyman $handyman)
{
return view('layouts/details', ['handymen' => $handymen]);
}
For this you will define this route:
Route::get('/handyman/{handyman}', 'Handyman#details');
And this will point you finally to the details of chosen handyman, and you can show his details:
<p>{{ $handyman->id }}<p>
<p>{{ $handyman->name }}</p>
The thing that is important to understand here is that you will first have a Collection of skills that will lead you to a Collection of Handymen, and not just a single one. After you choose a Handyman from the second list you will be able to show his details. If you try to jump over this step you will be trying to show details of a list.
Hope this helps...

Categories