1

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);
Kashif Rafique
  • 1,273
  • 1
  • 12
  • 25
  • Hi, please amplify your question by explaining what research you have done and why that did not help. Thank you. – MandyShaw Jan 19 '19 at 19:54
  • with is used to pass varibale to the respective view file. where you can set like for single variable return view('pages.about')->with('key', value); for multiple valriable: return view('pages.about')->with(['key1'=>value,['key2'=>value]); – Prince Kumar Dwivedi Apr 11 '19 at 11:18
  • You can try ->with(compact("name")) – Chintan7027 Apr 12 '19 at 05:08

5 Answers5

1

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
Iftikhar uddin
  • 3,117
  • 3
  • 29
  • 48
1

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;}}
Jithesh Jose
  • 1,781
  • 1
  • 6
  • 17
0

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');
Tpojka
  • 6,996
  • 2
  • 29
  • 39
0

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'));

kapitan
  • 2,008
  • 3
  • 20
  • 26
0

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>
Sheetal Mehra
  • 478
  • 3
  • 13