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...
Related
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
I want to show my product in a single page but I have "Property [name] does not exist on this collection instance." error
blade:
<div class="title">
<h2>{{ $singleproduct->name }}</h2>
</div>
<div class="single-product-price">
<h3>{{ $singleproduct->price }}</h3>
</div>
<div class="single-product-desc">
<p>{!! $singleproduct->explain !!} </p>
</div>
controller:
public function show()
{
$singleproduct = Singleproduct::get();
return view('UI.store.SingleProduct' , compact('singleproduct' ));
}
route:
Route::get('/singleproduct/{product}' , 'admin\SingleproductController#show');
You have a Collection of potentially many or no Singleproducts. If you only want 1 you would use first.
Most likely because this is a show route you want a specific Singleproduct, which I will guess you are passing an 'id' via the URL.
public function show($product)
{
$singleproduct = Singleproduct::findOrFail($product);
return view('UI.store.SingleProduct', compact('singleproduct'));
}
Now in the view you know that singleproduct is definitely an instance of Singleproduct and is accessible.
You need to select only one product not all of them,
so you can update your show method like this
using the route model binding you can read more about this awesome feature in laravel docs
public function show(Singleproduct $product)
{
return view('UI.store.SingleProduct' , compact('product' ));
}
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]) }}">
AdminsController
public function index(){
$someMessages=$this->blog->paginate(5);
$users=User::all();
// return 'Welcome Admin';
return View::make('admin.admin',['someMessages'=>$someMessages,'users'=>$users]);
}
admin.blade.php
Here need to display username from 'users' variable based on uid stored in 'someMessages' object.
#extends('layout.default')
#section('title')
<title>Welcome Admin</title>
#stop
#section('content')
#foreach($someMessages as $message)
<blockquote>{{$message['blog']}}
<small>
<cite>
</cite>
</small>
</blockquote>
#endforeach
{{ $someMessages->links()}}
#stop
If you have a Eloquent relation between blog messages and users defined correctly you don't need to query the users separately. What you want to do is just go with the eager loading:
//in case you call it author in your "relation definition"
$someMessages = $this->blog->with('author')->paginate(5);
And then in your Blade template:
#foreach($someMessages as $message)
<div>
<div>{{ $message->blog }}</div>
<div>{{ $message->author->name }}</div>
</div>
#endforeach
If you haven't declared the relationship yet - it's easy. Open up your user eloquent model class and add a method:
public function blogs()
{
return $this->hasMany('Blog', 'uid');
}
In the blog eloquent model class you'd add:
public function author()
{
return $this->belongsTo('User', 'uid');
}
More about eloquent and relationships can be found here.
If that's not the case you can still filter your users collection to get the one you want:
#foreach($someMessages as $message)
<?php
$user = $users->filter(function($user) use ($message)
{
return $user->id == $message->uid;
})->first();
?>
<div>
<div>{{ $message->blog }}</div>
<div>{{ $user->name }}</div>
</div>
#foreach
What happens here is you filter your users collection based on $message->uid value and take the first one from it (it should always be just one or none since user IDs are unique).
Although you need to understand that in this case you will get all the users from database and filter through them for every blog message you're outputting. Eager loading is a much better idea here and I'd stick to it if possible.
I am using laravel 4. I have a table which displays data from "cars" table. I want to click at one of car's name and display all information about this vehicle in a new page. I have done this:
index.blade.php
#foreach ($cars as $car)
<tr>
<td>
{{ HTML::link('/desc', $car->Description, array('id' => 'linkid'), true) }}</td>
{{ Form::open(array('action' => 'CarController#showDesc', $car->id)) }}
{{ Form::hidden('id', $car->id) }}
{{ Form::close() }}
<td>
{{ $car->License }}</td>
<td>{{ $car->Make }}</td>
<td>{{ $car->status }}</td>
</tr>
#endforeach
CarController.php
public function index() {
$cars = Car::all();
return View::make('pages.index', compact('cars'));
}
public function showDesc() {
$description = //here want to get name, year, color from "cars" where id = POST['id']
return View::make('pages.description', compact('description'));
}
//pages description is the new page which will display all information about that specific vehicle ,
routes.php
Route::resource('cars', 'CarController');
Route::post('desc', array('uses' => 'CarController#showDesc'));
The problem is that browser shows: This webpage not available
you are using Route::resource('cars', 'CarController'); and this will create restful routes to access your controller and in the car controller you should have functions like index(), show($id), destroy($id).
To see what are the valid routes, run php artisan routes in your project directory.
if you want to follow the restful pattern which is what you want to do I think by using a resource route. be sure you have the function show($id) in your CarController. the route to that function is cars/{$id} so in your view make a link to this route:
{{link_to_action('CarsController#show', $car->Description, $car->id)}}
I hope this will fix the problem
in your view you have a link to "/description" and in the routes.php file you don't have a route to that link