0

I have two functions in my controller, I would like to call the model Tbl_Perimetro and Tbl_Holding, I've achieved to call Tbl_Perimetro.. but I don't know how to call Tbl_Holding to work in the same way of Tbl_Perimetro

these are my functions

public function edit($id)
{
    if(request()->ajax())
    {
        $data = Tbl_Perimetro::findOrFail($id);
        return response()->json(['result' => $data]);

    }
}


public function update(Request $request, Tbl_Perimetro $user)
{
    $rules = array(
        'rif'      =>  'required',
        'razon_social'  =>  'required',
        'holdings_id' => 'required',
        'pines_id' => 'required'

    );


    $error = Validator::make($request->all(), $rules);

    if($error->fails())
    {
        return response()->json(['errors' => $error->errors()->all()]);
    }

    $form_data = array(
        'rif'        =>  $request->rif,
        'razon_social'    =>  $request->razon_social,
        'holdings_id' => $request->holdings_id,
        'pines_id' => $request->pines_id

    );

    Tbl_Perimetro::whereId($request->hidden_id)->update($form_data);

    return response()->json(['success' => 'Datos actualizados satisfactoriamente.']);

}
M.abdelrhman
  • 958
  • 14
  • 24
Oscar
  • 139
  • 1
  • 13
  • "but I don't know how to call Tbl_Holding to work in the same way of Tbl_Perimetro" Couldn't you just copy what you already have and replace it? quite hard for me to see the exact problem you are facing. – mrhn Feb 11 '20 at 12:29
  • All that I want to do is Add the model Tbl_Holding in the same two functions – Oscar Feb 11 '20 at 12:32

2 Answers2

0

You don't need to use Tbl_Perimetro $user in your update() function's argument.

Instead, include the Models at the beginning of your controller:

use App\Tbl_Perimetro;
use App\Tbl_Holding;

And then you can use those models anywhere in that controller:

Tbl_Perimetro::whereId($request->hidden_id)->update($form_data);
Tbl_Holding::whereId($request->hidden_id)->update($form_data);

Hope it helps.

Qumber
  • 13,130
  • 4
  • 18
  • 33
  • You can use those models in any method that are in the same controller. For example, in `edit()`, you can do something like this: `$holding = Tbl_Holding::findOrFail($id);` – Qumber Feb 11 '20 at 12:49
  • But, Tbl_Holding have to be = to $data – Oscar Feb 11 '20 at 12:55
  • You can't assign two different collections to a single variable. That would like saying `$a = 5 = 10;`. You can however merge two collections into one. Here's how you can do it: https://stackoverflow.com/questions/30522528/how-to-merge-two-eloquent-collections – Qumber Feb 11 '20 at 13:00
0

Do like this:

$data1=tbl_Perimerto::findorfail($id1);

$data2=tbl_Holding::findorfail($id2); return reponse()->json( ['result1'=>$data1,'result2'=>$data2]);

But what is $id1 and $id2? They was passed from routing:

route::get('/yoururl/{id1}/{id2},['uses'=>'YourController@method']);

and in your controllers method you should get them in arguement like this:

public function method($id1,$id2)

akbar
  • 625
  • 6
  • 12
  • there is no a way to work the two models in the same variable? – Oscar Feb 11 '20 at 15:15
  • You can use array out of return statement and populate this array with models .and finallay pass just this array. – akbar Feb 11 '20 at 15:18