0

If my code return null then it generates this error. If the code returns some data, then it works fine.

Controller

$profile_data= DB::table('partner_prefence')
    ->select('*')
    ->where('profile_id',$profile_id)
    ->first();

return view('partner_prefence',['profile_data' =>  $profile_data]);

View/Blade

@php($rel_status = explode(',', $profile_data->p_marital_status))

If $profile->p_marital_status has a value then there's no issue. The error only comes when its value is null.

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
Entrepreuner
  • 53
  • 4
  • 11
  • If you use `firstOrFail()` instead of `first()` it'll throw an error even before getting to the view, which you could catch and handle as required. – Tim Lewis Oct 05 '18 at 15:38
  • I am using first(), shouldI use firstorfail() ? – Entrepreuner Oct 05 '18 at 15:40
  • 1
    You don't *have* to... They both do the same thing essentially, but `firstOrFail()` will throw an error if no results are returned. You can use a `try ... catch` to handle that, or if you stick with `first()`, simply check `if($profile_data)` before passing it to the view. Ultimately it's up to you to figure out what to do if that data is `null` – Tim Lewis Oct 05 '18 at 15:44
  • I think you should follow the answer which has provided, because i think your view should show whether `profile_data` found or not. – Tharaka Dilshan Oct 05 '18 at 15:50

4 Answers4

3

Use optional() https://laravel.com/docs/5.8/helpers#method-optional

If the given object is null, properties and methods will return null instead of causing an error.

{!! old('name', optional($user)->name) !!}
wast
  • 878
  • 9
  • 27
1

First $profile_data need to be checked whether it is empty or it has some data. step2: Checking $profile_data object has property called p_material_status. Step3: If the above both are true then try to explode the data otherwise return some error message and try not explode the data would be better.

<?php

$data = (($profile_data != null) && property_exists($profile_data->p_marital_status)) ? $profile_data->p_marital_status : null)
if($data){
$rel_status = explode(',', $data);
}else{
    // Something else
}

?>
  • When posting an answer, be sure to *explain* what's happening and why this answers the question; not all users will be able to infer this information and a short explanation goes a long way. – Tim Lewis Oct 05 '18 at 18:46
1

Depending on whether the entry is vital for the page to be rendered or not, you could either perform a check in the controller.

$profile_data = DB::table('partner_prefence')
->select('*')
->where('profile_id',$profile_id)
->firstOrFail()

Or allow the null value to pass on to the view and make a ternary check.

$profile_data ? $profile_data->p_marital_status : '';

References:

Ternary Operator

Retrieving Single Models

0

Simply use an isset() or != null on the sentence like this:

@php($rel_status = $profile_data ? explode(',', $profile_data->p_marital_status?: '')) : [];
Jaime Rojas
  • 549
  • 2
  • 6