I'm facing problem in fetching data from controller to blade.php file they give an error of undefined variable $plans.
DalyTaskPlanController.php
public function dailyTaskPlan()
{
$plans = $this->plans = DailyTaskPlan::select(
'projects.project_name',
'daily_task_plans.*',
'tasks.heading',
'users.id')
->leftJoin('projects', 'daily_task_plans.project_id', '=', 'projects.id')
->leftJoin('tasks', 'daily_task_plans.task_id', '=', 'tasks.id')
->join('users','daily_task_plans.user_id', '=','users.id')
->take(4)
->get();
return view('dailytaskplan::dashboard', compact('plans'));
}
in view file they give an error undefined variable
@foreach ($plans as $plan)
<tr>
<td class="pl-25">{{ $plan->project_name }}</td>
<td class="pl-25">{{ $plan->heading }}</td>
<td class="pl-25">{{ $plan->date }}</td>
<td class="pl-25">{{ $plan->estimate_hours }} hrs {{ $plan->estimate_minutes }} mins</td>
<td class="pl-25">{{ $plan->memo }}</td>
</tr>
@endforeach
Additional question; how can I merge Laravel module route with project route because when I use module route they override project route?
i tried to make traits folder and give another route for module in other route they working fine. but in main route it's not working.
The $plans
variable seems to be NULL
, either provide a default, like:
return view('dailytaskplan::dashboard', [
'plans' => $plans ?? [],
]);
Or implement and use ->getOrFail()
which provides better error message.
getOrFail
<?php
namespace App\Providers;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
// ...
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
// ...
EloquentBuilder::macro('getOrFail', function () {
return tap($this->get(), function (?Collection $result) {
if ($result == null || $result->count() < 1) {
throw (new ModelNotFoundException)->setModel(
get_class($this->model)
);
}
});
});
}
}
Note that Laravel has many
OrFail
suffixed helpers, butgetOrFail
did not exist, hence I added one ;-)