How to put a field in other table?

Published 1 week ago by irankhosravi

Look at my codes.

RequisitionController.php

$requisitions = Requisition::with('schools')->where('city_id', auth()->user()->city_id)->latest()->paginate(25);

Requisition.php

public function schools ()
{
    return $this->belongsTo(SchoolsList::class, 'school_id');
}

I have two tables schools and requisitions in my project.

schools table

id, province_id, city_id, gender,......

requisitions table

id, school_id, type, ...

How to put schools in RequisitionController

Cronix
Cronix
1 week ago (794,690 XP)

How to put city_id in RequisitionController

I don't understand the question. The code that you show for "RequistionController" clearly shows that you are passing the city_id to it. You're getting it off the user object, so I'm not sure what your question is.

$requisitions = Requisition::with('schools')
    ->where('city_id', auth()->user()->city_id) // city_id is already available in RequisitionController??
    ->latest()
    ->paginate(25);
irankhosravi

My question is here, How to call two tables in a controller???

Cronix
Cronix
1 week ago (794,690 XP)

How to call two tables in a controller???

class SomeController extends Controller {

    public function call2tables() {
        // get data from one table
        $model1 = Model1::where(something)->get();

        // get data from a 2nd table
        $model2 = Model2::where(something)->get();
    }
}

Care to be more specific?

irankhosravi

I edited my post and city_id changed to schools.

Cronix
Cronix
1 week ago (794,690 XP)

Ok but that doesn't change anything at all. You are not giving an adequate description of the problem, or asking a real question that makes sense.

You say this is in your controller

RequisitionController.php

$requisitions = Requisition::with('schools')->where('city_id', auth()->user()->city_id)->latest()->paginate(25);

You are already getting the schools for that requisition by eager loading it with('schools'). So it's already "in the controller."

Again, I don't understand what you are asking, or what the problem is. Schools is already loaded in $requisitions, so

How to put schools in RequisitionController

doesn't make sense.

jlrdw
jlrdw
1 week ago (252,600 XP)

I may be way way off, but I think OP is asking how store related data to achieve that query in the first place:

$requisitions = Requisition::with('schools')->where('city_id', auth()->user()->city_id)->latest()->paginate(25);

I can't think of any other reason for the question. Everything OP shows here is a list view, nothing involving an add or update operation.

A real head scratcher.

irankhosravi

Oh sorry, My question is here, While city_id field is in the school's table, but city_id field does not exist in the requisitions table. So how to call city_id in RequisitionController.php?

irankhosravi

How should I do?

Cronix
Cronix
1 week ago (794,690 XP)

In php, how do you normally pass a variable/parameter to a function/method? You send it to it, like public function myfunction($variable) and then to use it $class->myfunction('foo').

You're only giving a very tiny portion of the problem, and not enough code to tell what you're actually trying to do. I have no idea what you're doing or how you'd pass the variable because you aren't explaining what you're doing. You're asking a very generic question that has a lot of answers and to narrow it down we need to know WHAT your are doing and HOW you're doing it (including views, the route definitions, the controllers/methods involved).

Are you submitting a form to this controller? If so, then pass the city_id to it like you would with any other form and access it via the $request object. Is this from a route? If so, declare the variable in the route, and accept it in the controller method. It's really difficult/impossible to answer without knowing all of the details of what you're doing.

Please sign in or create an account to participate in this conversation.