You can just do this:
echo $memus[0]['name'];
Or if you want all of them
foreach ($memus as $memu) {
echo $memu['name'];
}
Answer from Laurence on Stack OverflowYou can just do this:
echo $memus[0]['name'];
Or if you want all of them
foreach ($memus as $memu) {
echo $memu['name'];
}
The arrow ($object->property) notation is for objects.
The notation for accessing array elements is $array[$index][$key].
So in your case, to access the name key in your second array would be:
echo($menu[1]['name']), in your example this would echo the string 'zxcvxc'.
php - laravel get value from array of controllers - Stack Overflow
laravel - Get data from array in Controller - Stack Overflow
How to send array data as a parameter for get value in Laravel controller? - Stack Overflow
How to extract a specific element value from an array in laravel controller - Stack Overflow
You seem to be selecting something by id, so that's probably unique. When you do ->get() by default it will return a collection of results because it assumes there's always a chance that there's more than 1. When selecting by ID however you know if something exists by that id, there's only going to be 1 of it. You can change the code to:
$detail = DB::table('restaurants')->where('id', $restaurant_id)->first();
return view ('restaurant.detail')->with('detail', $detail);
<html>
<head>
<meta charset="utf-8">
<title>{{ $detail->name }}</title>
</head>
<body>
<h1>in view</h1>
{{ $detail->name }}
<br>
{{ $detail->street }}
<br>
{{ $detail->city }}, {{ $detail->state }}. {{ $detail->zip }}
</body>
</html>
I highly recommend you look into Eloquent
Example of an eloquent model:
class Restaurant extends Model {} //The table name for model "Restaurant" is assumed to be restaurants
Then you can find a single restaurant:
$detail = Restaurant::find($restaurant_id);
Why do you want multiple title tags in your page ? It doesn't make any sense.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title><!-- What do you want to show here ? --></title>
</head>
<body>
<h1>in view</h1>
@foreach ($details as $detail)
{{ $detail->name }}
<br>
{{ $detail->street }}
<br>
{{ $detail->city }}, {{ $detail->state }}. {{ $detail->zip}}
@endforeach
</body>
</html>
try with()
$data = array(
'name' => $name,
'age' => $age
);
return View::make('view2')->with($data);
on view get :- echo $data['name']; echo $data['age'];
or
return View::make('view2')->with(array('name' =>$name, 'age' => $age));
get on view :-
echo $name;
echo $age;
For more Follow here
You need to use:
return View::make('view2')->with(['name' => $name, 'age' => $age]);
to use
$name and $age in your template
public function index()
{
$store = Store::all(); // got this from database model
return view('store')->with('store', $store);
}
You've three options in general.
return view('store')->with('store', $store);return view('store')->withStore($store);return view('store')->with(compact('store'));
1.Creates a variable named store which will be available in your view and stores the value in the variable $store in it.
2.Shorthand for the above one.
3.Also a short hand and it creates the same variable name as of the value passing one.
Now in your view you can access this variable using
{{ $store->name }}
it will be same for all 3 methods.
Try this
public function index()
{
return view('store', ['store' => Store::all()]);
}
Then you can access $store in your review.
Update: If you have your variable defined already in your code and want to use the same name in your view you can also use compact method.
Example:
public function index()
{
$store = Store::all();
return view('store', compact('store'));
}