I want to pass data to Laravel view and do not understand certain parameters within the with()
method. What parameter name
refer to?
return view('pages.about')->with('name', $name);
I want to pass data to Laravel view and do not understand certain parameters within the with()
method. What parameter name
refer to?
return view('pages.about')->with('name', $name);
what parameter 'name' refer to
Name is the alias you give to the variable $name
which you can access in your view.
e.g
$name= 'John Doe';
return view('pages.about')->with('myName', $name);
So now you can access $myName
in about
view
From the docs it says:
As an alternative to passing a complete array of data to the view helper function, you may use the with method to add individual pieces of data to the view
Ref: Docs
UPDATE AFTER COMMENTS: In your case you should use with as below:
return view ('pages.absensi')->with('Rfidabs' => $Rfidabs);
then in your abseni
view you can loop through the array as below:
foreach ($Rfidabs as $item)
<tbody>
<td>{{$item->id}}</td>
<td>{{$item->Name}}</td>
<td>{{$item->Kelas}}</td>
</tbody>
endforeach
In your controller
$user=User::where('id','=',$id)->first();
This will load the user with the specific id to the $user object.
If we want to load this object in our view,we will pass the object to the view using 'with' function. It has 2 parameters:the object name and the object which we want to load in the view.
return view('user.list')->with('student',$user);
In this example i just took a user object and loads in the view as $student. In our view we use,
{{$student->name;}}
{{$student->age;}}
First you should ha defined $name
variable.
Then, part name
(first with()
's argument) you are calling as
{{ $name }}
.
Or from docs
As an alternative to passing a complete array of data to the viewhelper function, you may use the withmethod to add individual pieces of data to the view:
return view('greeting')->with('name', 'Victoria');
with
is a variable name you are passing to your view file.
So in your case:
return view('pages.about')->with('name', $name);
You are passing a name
variable name to your pages.about
blade file.
However, on cases where the variable name you want to pass on the blade file is the same with the variable name you have on your controller, you can just use the compact
like below:
return view('pages.about')->with('name', $name);
is the same as
return view('pages.about', compact('name'));
Using compact
will have an advantage, imagine the situation below:
return view('pages.about')->with('name', $name)->with('age', $age)->with('gender', $gender)->with('address', $address);
is the same as
return view('pages.about', compact('name', 'age', 'gender', 'address'));
According to your code name
refer to the variable through you can access data in the view.
return view('pages.about')->with('name', $name);
You can access data in view like this.
<table>
<tr><th>Name</th></tr>
<tr><td>{{$name}}</td></tr>
</table>