I want Laravel to pass the data to the view and display it in the browser, but it's not working.

Asked 2 years ago, Updated 2 years ago, 179 views

The following error is causing me to fail.
Could someone show me the way to resolve the error?

What you want to do

In
view, use foreach to extract each record.
Specify the properties of the extracted records with the Arrow operator.
I want to retrieve the data for each column.

Error Statements

Trying to get property 'id' of non-object (View: /var/www/html/resources/views/some/detail.blade.php)

Error when referencing property or null value of a variable that does not exist.

Affected Files
[Details.blade.php]

<tbody>
@foreach ($ singular form of certain as$★←table■■■■es) 
   <tr>
       <td>{{$ certain->id}}</td>
Use certain specified in the campact as $ certain

=>@foreach($a certain as$★) I don't know which variable to use in part ★, so I'm having a hard time.

[A certain Controller.php]

public function show certain (int$ certain Id):View
{
   $ certain = $this-> certain Service->retrieve certain ($ certain Id);
   return view('some.detail', compact('★★★★'));
}

Related Files
CarInspectionHistory.php

public function Certain InspectionHistory(): BelongsTo
{
   return$this->belongsTo(certain::class);
}

[web.php]

Route::get('/A certain/Details/{A certain_id}', '\App\Http\Controllers\A certain Controller@showA certain');

[Some InspectionHistorySeeder.php]

public function run()
    {
        factory(App\Models\Some InspectionHistory::class,2500) ->create();
    }

What I tried
[1]

@foreach($some as$★)
The $★ portion of the is:
・$★
·$★_inspection_history
·$★ID
·$★s
I tried to change it to , but it was the same error.

[2]

public function show certain (int$ certain Id):View
{
   $car = $this-> certain Service->retrieve ($ certain Id);
   return view('some.detail',compact('some'));
}
Set $ of to $gars and compact(' certain') to compact('gar').
Same error when trying @foreach($gars as $gar)

Environment

Lavel Framework 5.7.22
docker 18.09.1

Please reply if you have time.

php laravel laravel-5

2022-09-30 21:38

2 Answers

In the Laravel view file, you can expand the variables passed by the controller.
For example,

//@controller: Retrieve data with ID $id and pass it to view
$data=YourDataClass::find($id);
return view('data.show',compact('data'));

Then, in the view file (resources/views/data/show.blade.php),

//@view
// I gave you compact('data') on the controller, so you can use $data.
// The variable name in view is the same as the string in compact.
{{$data}}

You can view the data in the

In the example of jazzy coffee (I'm sorry, I changed $ to $bou),

//@controller
public function showBou(int$bouId):View
{
   $bou = $this->bouService->retrieveBou($bouId);
   return view('bou.show', compact('bou'));
}

and

//@view
@foreach($bou as$item) 
   <tr>
       <td>{{$item->id}}</td>
   </tr>
@endforeach

If so, you should be able to deploy the data in view as long as the controller has properly $bou.

cf1.[larvel] Controller to view and how to pass and deploy variables (Qiita article by @ryo2132)
cf2.compact function (php.net)


2022-09-30 21:38

I'll name the variable bou
id is an error that does not exist, so

bouController.php

public function show($id):View
{
   $bou = $this->bouService->retrievebou($id);
   return view('bow.detail', compact('bou'));
}

detail.blade.php

<tbody>
@foreach($bou as$b) 
   <tr>
       <td>{{$b->id}}</td>
   <tr>
@endforeach
</tbody>

web.php

Route::get('/bou/detail/{id}', '\App\Http\Controllers\bouController@show');    

If you adjust the parameters with , it will work.


2022-09-30 21:38

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.