0

Using Laravel eloquent in Modular development. Save works well, But the Update function not working as I expected. Kindly check my coding method and error.

use Modules\Projects\Entities\Project;
public function update(Request $request, $id)
    {
        $project = Modules\Projects\Entities\Project::find($id);
        $project->project_name = request('project_name');
        $project->save();
}

Error throws like:

{
    "message": "Class 'Modules\\Projects\\Http\\Controllers\\Modules\\Projects\\Entities\\Project' not found",
    "exception": "Symfony\\Component\\Debug\\Exception\\FatalThrowableError",
    "file": "D:\\XMAPP\\htdocs\\minidmsapi\\Modules\\Projects\\Http\\Controllers\\ProjectsController.php",
    "line": 69,
    "trace": [

How to use " $flight = App\Flight::find(1);" in modular development ? Laravel Official Doc

3
  • Write this above : use App\Modules\Projects\Entities\Project; Commented Oct 17, 2019 at 10:21
  • And change to this : $project = Project::find($id); Commented Oct 17, 2019 at 10:22
  • @YasinPatel thanks. Working Fine. How to get Success Response ? Commented Oct 17, 2019 at 10:29

3 Answers 3

1

You already imported the Modules\Projects\Entities\Project;.Now you can directly use Project.

use Modules\Projects\Entities\Project;
public function update(Request $request, $id)
{
    $project = Project::find($id);
    $project->project_name = request('project_name');
    $project->save();
}

Hope this helps...

Sign up to request clarification or add additional context in comments.

2 Comments

ok. How to get Success Response? ( I mean Saved ID for check ) @jithesh
$project = Project::where('id', $id)->update(['project_name' => request('project_name')]);.Then use $project->id
1

try adding \ before Modules\Projects\Entities\Project::find($id);.

like \Modules\Projects\Entities\Project::find($id);

or you can directly use Project::find($id); as you have already used namespace.

2 Comments

@Vibhaa ok. How to get Success Response? ( I mean Saved ID for check )
just check $project->id is set or not
1

You dont' have to use full namespace on Project model, since you already imported it at the top:

use Modules\Projects\Entities\Project;

Just use:

$project = Project::find($id);

Edit: To return some response messages, below you can add something like this at the end of your function:

return redirect()->back()->with('success', 'Your message');

In order to display your message in blade, add something like this:

@if (session()->has('success'))
    <div class="alert alert-success">
        <p>{{session('success')}}</p>
    </div>
@endif

3 Comments

ok. How to get Success Response? ( I mean Saved ID for check )
I'm not sure what you're asking. You want a message that says "Updated project id 10 (for example) ? "
@zlatna Exactly

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.