0

I am trying to get information from a model using its name which is sent as parameter from blade using ajax call.

$.get("{{ url('auditInformation')}}", {modelName: modelName,versions:versions,currentData:currentData[index]});

Now i need to retrieve information using modelName from a model.

So when i tried this:

$auditInfo=Input::all();
    $modelName=$auditInfo['modelName'];
    $values=$modelName::find(1);

I got this response Class 'Designation' not found

But if i use

$modelName=new Designation();
    $values=$modelName::find(1);

then it shows data exactly what i want.

So i understand that this is all about model ( class ) object.

Is there any way to assign object to $modelName using $auditInfo['modelName'] .

Thanks

3 Answers 3

5

If you want to do that way, you should use the model's namespace.

For example, if the 'Destination' model's namespace is app\Destination, you should use like this :

$auditInfo=Input::all();
$appPrefix = 'app';
$modelName=$appPrefix . '\' . $auditInfo['modelName'];
$values=$modelName::find(1);
Sign up to request clarification or add additional context in comments.

1 Comment

I think you would need to escape the backslash like so: $modelName=$appPrefix . '\\' . $auditInfo['modelName'];
3

This seems to be working

$table_name = "App\Models\PeopleYouMayLikeModel";

$obj = $table_name::where($column_name_identifier_1, '=', $row_identifier_1)
                  ->where($column_name_identifier_2, '=', $row_identifier_2)->first();

Comments

3

The single backslash between the singe quote is considered as an escape sequence so use double backslash is 100% work. For example

public function index(Request $request) {
    $model_prefix="App\Models";
    $modal = $model_prefix.'\\'.$request->type;
    $modal::updateOrcreate(['users_id'=>session("user")->users_id],$request->all())->save();
    dd(Profile::all());
}
//Laravel 7.0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.