Support the ongoing development of Laravel.io →
posted 10 years ago
Database
Last updated 2 years ago.
0

Hi,

Whats the relationship between tech's and orders?

You should set-up the route to the tech controller something like this.

route::get('tech/{id}/order',techController@order);

then in your tech controller.

   public function order($id){
    $tech = Tech::find($id)->orders;
     return View::make('tech',compact('tech'));    
    }
Last updated 2 years ago.
0

Thank you for your response. So you are saying I should make a new controller for this purpose? No problem, I can do that.

I am slightly lost as to what the actual orders have to do with it and why I'd want it in the URL. The system should simply be listing existing orders for the technician. I am not trying to retrieve specific information per order because the system already takes care of that.

I simply want to go to tech/123 (for example) and call up my page that lists all the work orders that the tech is assigned to. I have already designed and coded this page just like I have customer.blade.php that comes up with url customer/123 (for example) Hope that makes sense :)

Thank you so much for the help I really appreciate you taking the time to assist me.

Edit: Here is a screenshot of our customer page for a test customer. Similar concept to how my tech page is laid out. Does that clarify somewhat I am trying to accomplish?

http://imgur.com/wfVv74s

Last updated 2 years ago.
0

Ok, I see. the order is in the URL is just there make it more readable. You can remove it if you wish.

Are you currently using only the homeController for everything?

You should have a tech controller.

Last updated 2 years ago.
0

We have a lot of controllers right now: ApiController BaseController HomeController InventoryController OrderController SetupController UserController

I would like to just have the URL for this page of the system to be example.com/tech/NUMBER because I can imagine this is less work, and it's consistent with the other pages. like customer/NUMBER and workorder/NUMBER

I can create the TechController if that is good, is there anything else I need to do to "enable" the controller so to speak except by editing routes.php?

Thanks a bunch, Michael

Last updated 2 years ago.
0

Yep setup a TechController.

Set-up a route in your router filer

route::get('tech/{id}',techController@order);

Set-up the relationships.

     // Tech Model
       public function orders(){
			return $this->hasMany('order');
		}

//Order Model
  public function tech(){
 return $this->belongsTo('tech');
   }

In the techController file

 public function order($id){
   //find the tech
    $tech = Tech::find($id);
   //find the order
    $order = $tech->orders()->orderBy('id','desc')->paginate('30');
     return View::make('tech',compact('tech','order'));    
    }

You also have to setup a migration to add a tech_id field to orders table. This is a simple OneToMany relationship. 1 tech have many order's.
I think this will get you going for now.

http://laravel.com/docs/eloquent

Last updated 2 years ago.
0

Thanks you for that detail. I haven't done the last step yet, but I entered the code like you set and I'm getting the following error:

Fatal!! Error 500

syntax error, unexpected '@' Line: 681

Edit: is there a way to do the migration without the CLI artisan thing? I don't have that on my computer, just have this web app on a shared hosting account. Thanks, Michael

Last updated 2 years ago.
0

You can add the tech_Id field manually usually phpmyadmin.

Which file is throwing that error and what's on line 681?

Actually the Route should be

route::get('tech/{id}','techController@order');
Last updated 2 years ago.
0

Now that I put the ' ' that error is gone. Now I have error: syntax error, unexpected 'public' (T_PUBLIC) Line: 701

Last updated 2 years ago.
0

You have to post the filename and the line thats throwing the error..

Last updated 2 years ago.
0

Sorry, I don't know which file it's in. I simply get the error no matter which page of the system we go to. If I'm logged out of the system (that's the error I get when logged in) I get the following error: Error in exception handler: Trying to get property of non-object in /home/renew/public_html/app/storage/views/3ba2de8dceb2afd8975a6822bd910d1e:36

Last updated 2 years ago.
0

Post a screenshot let me see..

Last updated 2 years ago.
0

Thanks not a laravel error. You may have forgotten to put a semicolon at the end of a line.. Post your route and techController file, let me that a look.

Last updated 2 years ago.
0

Here you go, sorry about that. I went back into global.php and set the exception handler back to default (the previous developer had customized it)

http://imgur.com/Pzwdfy4

Last updated 2 years ago.
0
Last updated 2 years ago.
0

Remove this from the route file.

 // Tech Model
       public function orders(){
            return $this->hasMany('order');
        }
 
//Order Model
  public function tech(){
 return $this->belongsTo('tech');
   }

Those are the relationship for the models.

The first goes to tech model(you have to create it.) and the other goes to the order model

Last updated 2 years ago.
0

Thanks, it works now, but now the feature I want to add does not work either :/ I'm probably missing the point of the relationships but why do I need that if I already have the orders and techs created? Wouldn't I do some sort of "for each" pull from the database and display it in a list? I could do it in plain PHP/MySQL I am just stumped as to how to do it in Laraval - but I want to learn because it's a really powerful system that can speed up my work a LOT!

So if we could get this working it would be awesome. Thanks, Michael

Last updated 2 years ago.
0

Right to the get the tech orders. do something like this

@foreach($order as $t)
   {{$t->ordername}}
@endforeach
Last updated 2 years ago.
0

Ok, that makes sense. Do I do that on the page itself or in routers or in the techcontroller? But I need to get that page to at least load first, when I click on the technician's name.

Last updated 2 years ago.
0

That goes in the tech display view. The view you want to display the tech and their orders.

Last updated 2 years ago.
0

Thanks. But as of right now when I go to tech/123 or whatever I get an error, because we removed the routes.

Last updated 2 years ago.
0

Do have a tech with Id 123 in Ur database?

You should still have this route

route::get('tech/{id}','techController@order');

Put it back if you removed it.

Last updated 2 years ago.
0

Nope, we only have a few techs. 123 is just an example number. Even if I go to like 4, which we have, it gives the error.

Last updated 2 years ago.
0

Hey, I'm back, trying to tackle this one again.

I added this to my routes: route::get('tech/{id}','TechController@order');

And now I get error: rgument 1 passed to Illuminate\Database\Eloquent\Model::__construct() must be of the type array, string given, called in /home/renew/public_html/app/storage/views/999ecfb79fac1d6d277dfa72a9dca1f9 on line 3 and defined

Last updated 2 years ago.
0

Sorry guys here is the code I'm working with at this point. I've included my controller, routes, and also the page that it should display. I can edit the page once we even get it to display but right now when I go to /tech/4 for example I get a big error (last pastebin)

TechController.php http://pastebin.com/j0bVCn16

routes.php http://pastebin.com/byFDnwBh

tech.blade.php http://pastebin.com/NgrNjvMx

Error: http://pastebin.com/VmqRKG82

Last updated 2 years ago.
0

I would guess the error is related to this line that is in the template.

<?php $user = new User($id); ?>

You can't call a eloquent model with just a number. You should really move this to the controller and pass the data over to the template. It looks like the $user is only used to pull the tech's name.

Quick and dirty you could try,

<?php $user = new User->find($id); ?>

I'm not sure it it will work as I don't know about the $user->detail part. Is there a function in the User's model class called detail? or a relationship?

Last updated 2 years ago.
0

Thank you SO MUCH for your reply!

It looks like the only problems are on tech.blade.php since I get this error now. This is great news because it measn the rest is being routed correctly.

syntax error, unexpected '->' (T_OBJECT_OPERATOR)

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

shovenose shovenose Joined 19 Jun 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.