Trying to get property of non-object - laravel 5.4 - laravel

I am using Laravel eloquent relationship, when i use
{{$subject->cat}}
i receive a json response like below
{"id":13,"name":"Fsc","created_at":"2017-10-23 00:00:00","updated_at":"2017-10-23 00:00:00"}
as i have 2nd object "name" here i tried
{{$subject->cat->name}}
but got error
Trying to get property of non-object
while i am using same approach for other table and copied same method here but getting error.
See my Blade file code, i am calling object inside foreach loop
#foreach ($subjects as $subject)
<tr>
<td width="8%">{{$subject->id}}</td>
<td width="22%">{{$subject->subject}} </td>
<td width="22%">{{$subject->cat->name}}</td>
<!-- <a class="btn btn-success btn-sm" href="{{route('subjects.show', $subject->id)}}"><i class="fa fa-eye"></i></a> | -->
<i class="fa fa-pencil"></i> |
{!! Form::open(['route' => ['subjects.destroy', $subject->id], 'method' => 'DELETE', 'class' => 'delete-form']) !!}
{{ Form::button('<i class="fa fa-times" aria-hidden="true"></i>', ['class' => 'btn btn-danger btn-sm pull-left', 'type' => 'submit']) }}
{!! Form::close() !!}
</td>
</tr>
#endforeach

First Use json_decode($json);
Takes a JSON encoded string and converts it into a PHP variable.
Then
Use {{$subject->cat}}

Just use json_decode like:
$var = json_decode($subject, true);
Then you can use:
{{$subject->cat->name}}
See more here!
Hope this helps you!

You need to decode your json and then use like this:
$result = json_decode ($subject->cat);
echo $result->name;
Use your variable name in place of $result
For insight here is pastebin demo

Try this
$cat = json_decode($subject->cat); // returns object
{{ $cat['name'] }}
or
$cat = json_decode($subject->cat, true); // returns array
{{ $cat['name'] }}

Related

Select box in form blade Laravel

how can I create a select box which is filled with values from the db?
The view is published with the $groups variable. In my select box i need $groups->id (hidden, only for storing) and $groups->name. This is currently my form.
{!! Form::open(array('route'=>'store.invitation')) !!}
<div class="form-group">
{{Form::label('username', 'Username')}}
{{Form::text('username', '', ['class' => 'form-control', 'placeholder' => 'Enter Username'])}}
{{Form::label('groupname', 'Gruppe')}}
{{Form::select($groups->name) }}
{{ csrf_field() }}
</div>
<div>
{{Form::submit('Submit',['class' => 'btn btn-primary'])}}
<a class="btn btn-default btn-close" href="{{ route('home') }}">Cancel</a>
</div>
{!! Form::close() !!}
Thanks
You should pass an array of the data that you get from the db. Like this:
Form::select('size', array('L' => 'Large', 'S' => 'Small'));
This documentation might be old, but thats how you can create Form::select in blade.

Route not going where it's supposed to

I'm getting this error. This error comes in when I hit the delete button
MethodNotAllowedHttpException in RouteCollection.php line 251:
the problem is as far as I know my routes is correct.
My cart.blade.php
#foreach($cartItems as $cartItem)
<tr>
<td>
<img src="{!! asset("product_images/$cartItem->img") !!}" alt="..." class="img-responsive">
</td>
<td>{!! $cartItem->name !!}</td>
<td>{!! $cartItem->qty !!}</td>
<td>R {!! $cartItem->qty * $cartItem->price !!}</td>
<td>
{!! Form::open(array('method' => 'Delete', 'route' => array('deleting', $cartItem->rowId))) !!}
<button class="btn btn-warning">Delete 2</button>
{!! Form::close() !!}
</td>
</tr>
#endforeach
My routes
Route::put('/product/deleting/{id}', [
'uses' => 'OpenController#deleting',
'as' => 'deleting'
]);
my controller
public function deleting($id)
{
echo "string";
}
You are using method DELETE on your form:
Form::open(array('method' => 'Delete',
but defined the method PUT in your route:
Route::put('/product/deleting/{id}', [
Try to change the route to
Route::delete('/product/deleting/{id}', [
Try using 'delete' instead of 'put' in your routes file.

laravel 5 not deleting user record

All i want to do is simply delete a user form the database.
My Route is a resource as seen below:
Route::resource('users', 'UserController');
So this should mean that the destroy action in my UserController should be the place for my code.
So my controller action is below:
public function destroy($id)
{
$user = User::find($id);
$user->delete();
return Redirect::back();
}
Now when i click the delete button, which links to /users/destroy/4
it should find the user with id 4 and then delete it.
Instead i get the error
NotFoundHttpException in RouteCollection.php line 145:
EDIT:
#foreach ($users as $user)
<tr>
<td>{{ $user->id }}</td>
<td>{{ $user->username }}</td>
<td>{{$user->HWID}}</td>
<td>{{$user->name}}</td>
<td class="tools">
<i class="fa fa-pencil-square-o fa-lg"></i>
<i class="fa fa-trash fa-lg"></i>
</td>
</tr>
#endforeach
I dont know if it's possible to directly delete a user from your database via a link as you specified in your table.
My work around for this is to first point the user to the show function in your controller. And giving the user an overview of the information of the user itself.
This page contains a form with the DELETE method. Below the information of the user I put a delete button which will submit the form with the DELETE method to the URL: /users/4
Cause the link: /users/destroy/4 is not a valid resource link.
See this link for extra information about the resource controller links: Resource Controller
Example delete/show page of my own application:
{!! Form::model($ManagementUser, array('method' => 'DELETE', 'url' => 'admin/management/' . $ManagementUser->id, 'role' => 'form')) !!}
<div class="box-body">
<div class="form-group">
<label>Name</label>
{!! Form::text('name', Input::old('name'), array('class' => 'form-control', 'placeholder' => 'Name', 'name' => 'name', 'disabled')) !!}
</div>
<div class="form-group">
<label>E-mailaddress</label>
{!! Form::text('email', Input::old('email'), array('class' => 'form-control', 'placeholder' => 'E-Mail', 'name' => 'email', 'disabled')) !!}
</div>
{!! Form::submit('Delete', array('class' => 'btn btn-block btn-default')) !!}
</div>
{!! Form::close() !!}
In Resource Controller, destroy action is handled by DELETE method. Not GET method. Currently you are accessing a route with GET method that is not registered. The following command will help you to understand Resource Routes that you registered.
php artisan route:list
GET
<i class="fa fa-trash fa-lg"></i>
DELETE (You can delete the record by using form and DELETE method as follows)
<form action="{{ route('users.destroy', $user->id) }}" method="POST">
<input type="hidden" name="_method" value="DELETE" />
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>" />
<button><i class="fa fa-trash fa-lg"></i></button>
</form>
Reference
Resource Controller
Method Spoofing

Wildcards in Laravel

With the following code, I'm trying to pass a wildcard to my controller but I'm not sure how I can pass the URL dynamically and I'm not sure how to do this without creating a route for each url, which would take forever. Currently, I'm attempting to do it like so:
<a href="{{ route('purchase-get') }}/$item->name">
Here is the rest of the code
<tbody class="text-center">
#foreach (array_chunk($items->all(), 3) as $item_each)
<tr>
#foreach($item_each as $item)
<td>
<a href="{{ route('purchase-get') }}/$item->name">
{{ HTML::image($item->image_url, 'item-image', array('class' => 'item-image-row')) }}
<h4>{{ $item->item }}</h4>
<span class="text-muted">{{ $item->cost }}</span>
</a>
</td>
#endforeach
</tr>
#endforeach
</tbody>
You can link to a wildcard with named routes:
Route::get('/', array('as' => 'index', function()
{
$slug = 'product-1';
return 'link to product';
}));
Then catch the names route with the wildcard slug:
Route::get('products/{slug}', array('as' => 'products', function($slug)
{
$product = Product::where('slug','=',$slug)->first();
return $product;
}));

Input Validation with parsley.js on Laravel Blade

first i use laravel blade for my input from, like this
{{ Form::open('practicums/'.$practicums->id, 'PUT') }}
<table align="center">
<tr>
<td>{{ Form::label('name', 'Practicum Name') }}</td>
<td width="75px"></td>
<td>{{ Form::text('name', $practicums->name) }}</td>
</tr>
</table>
i want to use parsley.js to validate the input, i add
data-validate="parsley"
as
{{ Form::open('practicums/'.$practicums->id, 'PUT', array('data-validate' => 'parsley')) }}
but when i add parsley.js parameters on input form (like: data-type,data-required, ets) it's error. then i use 'old' input form like
<input type="text" id="name" name="name" data-required="true"/>
it works.
How to use the parsley.js parameters in Laravel Blade?
Can i still use input form from Laravel Blade with parsley.js, or i should use the old method?
Thanks before.
maybe you should share what's the error it appear ?
i m using the parsley with laravel 4 without any problem, this is my sample code
{{ Form::text('name',Input::old('name'),array('id'=>'name','data-required'=>'true','data-required-message'=>'Name Required','placeholder'=>'Please enter name')) }}
You should do like this
{{ Form::open('practicums/'.$practicums->id, 'PUT', array('data' => 'parsley-validate')) }}
This might help you.
{!! Form::open(['route' => 'posts.store','data-parsley-validate'=>'']) !!}
{{Form :: label('title','Title:')}}
{{Form:: text('title',null,array('class'=>'form-control','required'=>'','max length'=>'255'))}}
{{Form :: label('slug','Slug:')}}
{{Form::text('slug',null,["class"=>'form-control','required'=>'','min length'=>'5','max length'=>'255'])}}
{{Form::label('body','Post Body:')}}
{{Form::textarea('body',null,array('class'=>'form-control','required'=>''))}}
{{Form::submit('Create Post',array('class'=>'btn btn-success btn-block','style'=>'margin-top:20px;'))}}
{!! Form::close() !!}

Resources