Reputation: 585
If I use {{$slider->name}}
it can not retrieve the data from database. But at the same time it can retrieve data for id
. If I use {{$slider['name']}}
it can retrieve the name from database.
Can someone please tell me the reason behind this problem?
controller
public function index()
{
$sliders=Slider::all();
//($sliders);
return view('slider.list',compact('sliders'));
}
View
@extends('master')
@section('content')
<section class="content-header">
<h1>
Slider List
</h1>
</section>
<section class="content">
<div class="row">
<div class="col-xs-12">
<div class="box">
<div class="box-header">
@if($message = Session::get('success'))
<div class="alert alert-success alert-dismissible">{{ $message }}</div>
@endif
</div>
<div class="box-body">
<table id="pageList" class="table table-bordered table-striped table-responsive">
<thead>
<tr>
<th>SL</th>
<th>Slider Name</th>
<th>Image</th>
<th>Published</th>
<th>Created by</th>
<th>Created at</th>
<th>Action</th>
</tr>
</thead>
<tbody>
@foreach($sliders as $slider)
<tr>
<td>{{$slider->id}}</td>
<td>{{$slider['name']}}</td>
<td>
@if($images=\App\SliderImage::where('slider_id','=',$slider->id)->get())
@foreach($images as $image)
<img width="80px" alt="image"
src="{{ asset('images/sliderImage/'.$image['image_path']) }}">
@endforeach
@endif
</td>
<td>{{$slider->published}}</td>
<td>{{$slider->created_By}}</td>
<td>{{$slider->created_at}}</td>
<td>
<form action="{{route('slider.destroy',$slider->id)}}" method="POST">
<a href="{{route('slider.edit',$slider->id)}}"
class="btn btn-sm btn-success">Edit</a>
<a href="" class="btn btn-sm btn-info">View</a>
@CSRF
@method('DELETE')
<button type="submit" class="btn btn-sm btn-danger"
onclick="return confirm('Are you sure to delete?')">Delete
</button>
</form>
</td>
</tr>
@endforeach
</tbody>
<tfoot>
<tr>
<th>SL</th>
<th>Slider Name</th>
<th>Image</th>
<th>Published</th>
<th>Created by</th>
<th>Created at</th>
<th>Action</th>
</tr>
</tfoot>
</table>
</div>
</div>
</div>
</div>
</section>
@endsection
Upvotes: 1
Views: 194
Reputation: 719
Compact function in Laravel
We mostly use compact in Laravel to send the values to the view. Something like this:
$sliders=Slider::all();
return view('slider.list',compact('sliders'));
Laravel expects an array to be passed to the view helper function. Second argument in view helper function is an array that where keys are the names of the variable and the value are the contents of those variables. These variables will be available in our views to be used.
You can also use extract PHP
function to import variables from an associative array like the one created from the comapct()
function. extract will import variables into the current symbol table from an array.
please refer the link
Upvotes: 1
Reputation: 1156
It depends on your controller method. IF you are passing array in view like this.
$slider = array(
"id"=>1,
"name"=>"slider_name",
);
return view('view_name',['slider'=>$slider]);
You have to write $slider['name'] to retrive data.
Similarly, If you directly using Object which is generated from eloquent. Like this
$slider = Slider::where('somecondition','value')->get();
return view('view_name',['slider'=>$slider]);
Now you can access data like this => $slider->name;
Upvotes: 2