How to display single encrypt record? - laravel

I use encrypt. I can display all table data. However I can'T get one record. I don't encrypt 'id'. I tried another column but same.
Here is my error
Trying to get property 'id' of non-object
Here is my code
blade file (I click this link first )
{{ $val->id }}
blade file (display page I got error at this page)
#foreach ($data as $val)
{{ $val->id }}
#endforeach
Trying to get property 'id' of non-object
web.php
Route::get('/one','MailController#onerecord');
Route::post('/one','MailController#onerecord');
Controller
public function onerecord(Request $request)
{
$id = $request['id'];
$data = Contact::where('id',$id)->get();
return view('mail.one', ['data' => $data]);
}
Could you teach me what is wrong my code please?

Use dd($data) after retrieving your Contact with Contact::where('id',$id)->get(); in order to investigate the content of $data. Most likely an object is returned and not an array, so looping through $data inside your blade #foreach will loop through the object's properties. Therefore $val->id isn't valid but the direct access of $data->id would be, without the need of looping through $data.

Related

How to show specific items of an array in Laravel

I am using Laravel.i want to show only the Name in my View Part. But facing problem.
function index() {
$data=array(
['id'=>'1','Name'=>'Debasais','sirname'=>'Acharya'],
['id'=>'2','Name'=>'prashant','sirname'=>'Mohaptra'],
['id'=>'3','Name'=>'Silu','sirname'=>'Mohaptra']);
return view("welcome")->withdata($data);
}
To show just the name in your view, just loop through $data and select only name.
#foreach($data as $names)
{{$names['Name']}}
#endforeach
I am pretty sure you are already looping through $data just make sure to output the name only

How to check data in Array by using eloquent method in Laravel

I am working on a quiz. Below is a method and I am trying to get an array that can work in in_array function:
public function mcq($id)
{
$questions = Chapter::find($id)->questions()->inRandomOrder()->limit(1)->first();
$question_check = TestsResultsAnswer::where('user_id', auth::id())->pluck('question_id')->toArray();
$sponsors = Sponsor::All();
return view('pages.mcq', compact('questions', 'sponsors', 'question_check'));
}
Below is blade code where I am passing the question_check variable by using in_array function:
#foreach ($questions as $key => $question)
#if(in_array($key, $question_check))
{{'Question Already Attempted'}}
#endif
#endforeach
But I am getting following error:
(2/2) ErrorException
Trying to get property of non-object (View: E:\xampp\htdocs\laravel\lea\resources\views\pages\mcq.blade.php)
My goal is to check if question was already attempted then print something. Please Help me to resolve this problem.
Using first() will return a Model and not a collection, so you can't loop through. Use get() to fetch the result as a collection.
I.e: Instead of this
$questions = Chapter::find($id)->questions()->inRandomOrder()->limit(1)->first();
do this
$questions = Chapter::find($id)->questions()->inRandomOrder()->limit(1)->get();
This should fix your current problem.
Considering this,
My Target is to make a check if question already attempted then print something.
I think this is what you really want to do.
In Controller
$questions = Chapter::find($id)->questions()->inRandomOrder()->get()
In Blade
#foreach ($questions as $question)
#if(in_array($question->id, $question_check))
{{'Question Already Attempted'}}
#endif
#endforeach

How do I parse this json data in view blade? Output [{"...":...}]

I am trying to display the maximum value of an attribute in a table
my controller
$member = DB::table('member')
->select(DB::raw('MAX(code) as code'))
->where('status', '=', "No")->get();
return view('member.index', compact('member'));
Currently this is my view
{{ $member }}
And this is the output
[{"code":14101234}]
I wanted to display something like this
14101234
I've tried using json_decode but the result remains.
You receive a collection there, so you will have to do
#foreach($member as $item)
{!! $item->code !!}
#endforeach
Since $member is a array of object you are getting in view.
So you can fetch a object key by -> operator. you can fetch code like this. since you are doing ->get(), so it will return array of object.
#foreach($member as $m)
{{ $m->code }}
#endforeach
$member is a collection object.You can iterate through it to get the value in your view.
example: (in view.blade.php)
#foreach($member as $individual)
{{$individual->code}}
#endforeach
this would give you the value as you want

Retrieving data from belongsToMany relationship in Laravel

I have some problems with getting data from my relationship. I need the tags of some domains.
$domains = Domains::where('customer_id', Auth::user()->customers_id)->get();
There are all the domains I need. On my Domains model I have this belongsToMany relation to my pivot table.
public function tags() {
return $this->belongsToMany('App\Models\Tags_Domains', 'domain_tag', 'domains_id', 'tags_id');
}
I was able to get all the datas from my relation with this:
dd($domains[0]->tags);
That gave me all the data I wanted but only for the very first domain. But I want this for every domain, to pass this new array to my Blade template. I tried many things out but couldn't make it work. ( $collection error, trying to get properly of non-object ... )
Can someone help me there?
Controller Code:
$domains = Domains::where('customer_id', Auth::user()->customers_id)->get();
return view('defaultLayout.domains.tagsdelete', [
'domains' => $domains
]);
This is because you use $domains[0] and you get the first domain.
You must loop through them:
foreach($domains as $domain) {
foreach($domain->tags as $tag) {
var_dump($tag);
}
}
Edit:
If you need the tags in your view here is how:
#foreach($domains as $domain)
<p>{{ $domain->name }}</p> //where name could be any field that $domain has
#foreach($domain->tags as $tag)
<p>{{ $tag->name }}</p> //where name could be any field that $tag has
#endforeach
#endforeach
Glad I was helpful :)

htmlentities() expects parameter 1 to be string, array given? Laravel

I've found many question realated to my problem but couldn't found an answer yet. It's about my foreach loop in my blade.
I want to print all product-names in my blade but I couln't figure out how to do that.
thats how I'm getting the products:
--- current code:
// controller
$id_array = Input::get('id');
$products= Products::whereIn('id', $id_array)->get();
$product_name = [];
foreach($products as $arr)
{
$product_name= $arr->lists('name');
}
returning $product_name gives me this as a output:
["football","cola","idontknow","freshunicorn","dummy-data"]
In my blade is just a simple:
#foreach($products as $product)
{{ $product}}
#endforeach
Error: htmlentities() expects parameter 1 to be string, array given
Thanks for your help and time.
It seems you are getting an object in an array in an array.
Like this:
array(
array(
object
)
)
It happens because you use the get() function to retrieve you model. The get() function always "wants" to retrieve multiple models. Instead you will have to use the first() function.
Like this:
foreach($id_array as $arr)
{
$want2editarray[] = Product::where('id', $arr)->first();
}
Hope it helps :)
Edit after #Wellno comment
That's probably because Product::where('id', $arr)->first(); returns null because it did not find anything.
I forgot to add a check after the retrieving of the product.
This can be done like this:
foreach($id_array as $arr)
{
// First try to get model from database
$product = Product::where('id', $arr)->first();
// If $product insert into array
if ($product) $want2editarray[] = $product;
}
Why do you use loop with IDs? You can find all products by IDs:
$products = Product::whereIn('id', $id_array)->get();
And then use $products in the blade template
#foreach($products as $product)
{{ $product->name }}
#endforeach
try to use Model/Eloquent to fetch data.
View should only display the data and not fetching directly from DB or do heavy calculations.

Resources