0

i am trying to sent data to view from function in controller but the variable view is not showing any result

public function goedit($id)
{
    $catg = Category::where('cat_id',$id)->first();
    return view('product.gocatedit')->with('row', $catg);
}

and my view is

 @if(isset($row))

                <form action="{{action('ProductController@edit')}}" method="post">
                  <input type="hidden" name="_token" value="{{ csrf_token() }}">
                  <div class="col-sm-12">
                    <h1 style="text-align:center;">Edit Items</h1>
                    <table  class="table">
                            <thead>
                            <tr>
                              <th>ID</th>
                              <th>Category</th>
                              <th>Item</th>
                              <th>Price</th>
                              <th></th>
                            </tr>
                            </thead>
                            <tbody>
                            <tr>
                              <input type="hidden" name="item_id" value="{{ $row->item_id}}">
                              <td><input class="form-control" name="item_name" value="{{$row->item_name}}" /></td>
                              <td><input class="form-control" name="item_price" value="{{$row->item_price}}" /></td>
                              <td><input class="btn btn-primary btn-block btn-flat" type="submit" value="Edit"></td>
                            </tr>
                            </tbody>

                    </table>
                    </div>
                </form>
            @endif  

please help thanks

Vishal B
  • 653
  • 2
  • 12
  • 31
  • Please, read this [answer](http://stackoverflow.com/questions/20563166/eloquent-collection-counting-and-detect-empty/20585483#20585483). It should help you. – Vitaliy Kravchyshyn Apr 14 '16 at 09:34

3 Answers3

0

It seems you query returning null, that's the problem (there is no such ID in a database).

If you want to check for null, use is_null:

@if(is_null($var))

Or maybe you want to use empty:

@if(!empty($row))
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • no its not null bcoz if i var_dump($row) at function it is showing the result. – Vishal B Apr 14 '16 at 10:05
  • Could you post result of `dd($row);` please? – Alexey Mezenin Apr 14 '16 at 10:14
  • item {#228 ▼ #fillable: array:4 [▶] #primaryKey: "item_id" #connection: null #table: null #perPage: 15 +incrementing: true +timestamps: true #attributes: array:6 [▼ "item_id" => "1" "item_name" => "Chinese Noodls" "item_price" => "123" "cat_name" => "Chinese" "created_at" => "2016-04-09 11:02:15" "updated_at" => "2016-04-14 06:37:54" ] #original: array:6 [▶] #relations: [] #hidden: [] #visible: [] #appends: [] #guarded: array:1 [▶] #observables: [] #with: [] #morphClass: null +exists: true +wasRecentlyCreated: false } – Vishal B Apr 14 '16 at 10:24
  • Did you run `dd()` in a view? If `$row` has right data, what variable is `null` then? – Alexey Mezenin Apr 14 '16 at 10:33
  • actually i am trying to send variable to the view from which i have called the function, and trying to show it on the modal. – Vishal B Apr 14 '16 at 10:37
  • So, in a view everthing is fine and you do not get the var in `ProductController@edit` method? If yes, use `{{ action('ProductController@edit', ['row' => $row] ) }}` to pass variable into `edit`. – Alexey Mezenin Apr 14 '16 at 10:41
  • ProductController@edit is to update into DB. it is working fine if i use different view to show data to be edit, but now i am trying to retrieve the data at same view to show that on Modal (bootstrap modal) but unfortunately m not able to receive the variable. – Vishal B Apr 14 '16 at 11:07
0
public function goedit($id)
{
    $catg = Category::where('cat_id',$id)->first();
    return view('product.gocatedit')->with('row', $catg);
}

practically should be:

public function goedit($id)
{
    $catg = Category::findOrFail($id);
    return view('product.gocatedit')->with('row', $catg);
}

and then it seems that you aren't using default primary key, which is "id", so in your model add

public class Category{

           ...................

          protected $primaryKey = "cat_id";
          .....................

}
Hans Yulian
  • 1,080
  • 8
  • 26
0

If you use ->with('something', 'data'), then the data passed will be in the session('something') and it's purpose is to 'flash' messages.

You need this (view data is supposed to be passed as an array, and as a second parameter of the view() function):

public function goedit($id)
{
    $catg = Category::where('cat_id',$id)->first();
    return view('product.gocatedit', ['row' => $catg]);
}
Oliver Maksimovic
  • 3,204
  • 3
  • 28
  • 44