In my controller i'm using data variable like this
$data['some_var'] = $this->Model->some_info($param);
So if i do var_dump this $data['some_var'] it will print like this
object(StdClass)[4]
Public 'blah1' => String 'blah1' (Length=5)
Public 'blah2' => String 'blah2' (Length=5)
Public 'blah3' => String 'blah3' (Length=5)
Public 'blah4' => String 'blah4' (Length=5)
I know how to use this $data[] into view file but i want to use this $data[] into controller file,
What i want , i want to use blah1 from array
I tried like this
$var = $data['some_var']['blah1'];
$var = $some_var->blah1;
I'm not sure how to sort-out this things
ok try this it will work
$var = $data['some_var'][0]->blah1;
Try
$var = $data['some_var']->blah1;
you might want to try the solution of typecasting your object - answer from Gordon : details here
// typecast your object
$data['some_var'] = (array) $this->Model->some_info($param);
// access the index of the array
// !! this won't work if ['blah1'] contains an Object !!
$var = $data['some_var']['blah1'];
If you want to access sub-object as well I guess you'll have to write a recursive function that will typecast every level see Gordon's answer as well
Related
I'm trying to pass a varible from the Controller to my .blade.php file.
I'm returning the view and compacted variables to the .blade.php but it doens't recognize the
variable.
This is the code of the Controller.
$contents = Storage::get($request->file('csv_file_1')->store('temporaryfiles'));
$contents2 = Storage::get($request->file('csv_file_2')->store('temporaryfiles'));
return view('comparison.comparison')->with(compact('contents'),$contents)->with(compact('contents2'),$contents2);
And i'm trying every way just to get an result but instead i'm getting the "Undefined variable $contents" page. The last method i used was a simple
<p>{{$contents}}</p>
I don't think it's correct but i don't really remember how to do it.
In controller return like:
return view('comparison.comparison', compact(['contents', 'contents2']);
And make sure your file is in resources/views/comparison/comparison.blade.php
Try this
$contents = Storage::get($request->file('csv_file_1')->store('temporaryfiles'));
$contents2 = Storage::get($request->file('csv_file_2')->store('temporaryfiles'));
return view('comparison.comparison', compact('contents','contents2'));
if you have defined a veriable above just use tha name inside compact as above and it can be acced inside blade as <p>{{$contents}}</p>
You can pass variables like that it's not mandatory to use compact.
return view('comparison.comparison', [
'contents' => $contents,
'contents2' => $contents2
]);
or if you want with compact:
return view('comparison.comparison')->with(compact('contents', 'contents1'));
I have my route set as
Route::any('/{brand?}/{type?}/{city?}', 'SearchController#index')->name('search');
I want to send from my controller query strings (Form GET params)
After searching I ended up with this but it does not work properly
return redirect()->route('search', [$brand->name, $type->name, 'search_model_from' => $request->search_model_from, 'search_model_to' => $request->search_model_to]);
which returns back
localhost:8000/toyota/avalon/2018?search_model_to=2019
I want to return
localhost:8000/toyota/avalon/?search_model_from=2018&search_model_to=2019
What I am trying to achieve in general is SEO friendly search functionality
Maybe you should try to assign city as null like that :
return redirect()->route('search', [
'brand' => $brand->name, 'type' => $type->name,
'city' => '', 'search_model_from' => $request->search_model_from,
'search_model_to' => $request->search_model_to
]);
I'm not sure but this could happen because you have defined 3 optional parameters in the route and as you are sending just two of them, this might takes the next (in this case 'search_model_from') as the third parameter for url.
Maybe if you cast and set a default value to the optional parameters in your Controller, you won't have that trouble, like this:
public function index(string $brand='', string $type='', string $city='' , $other_parameters)
Find below the controller code:
public function domaincheck()
{
$response = file_get_contents('https://resellertest.enom.com/interface.asp?command=check&sld=unusualTVname&tld=tv&uid=resellid&pw=resellpw&responsetype=xml');
$data = simplexml_load_string($response);
$configdata = json_encode($data);
$final_data = json_decode($configdata,true);// Use true to get data in array rather than object
// dd($final_data);
$response1 = file_get_contents('http://resellertest.enom.com/interface.asp?command=GETNAMESUGGESTIONS&UID=resellid&PW=resellpw&SearchTerm=hand&MaxResults=5&ResponseType=XML');
$data1 = simplexml_load_string($response1);
$configdata1 = json_encode($data1);
$final_data1 = json_decode($configdata1,true);// Use true to get data in array rather than object
//dd($final_data1);
$response2 = file_get_contents('https://resellertest.enom.com/interface.asp?command=gettldlist&UID=resellid&PW=resellpw&responsetype=xml');
$data2 = simplexml_load_string($response2);
$configdata2 = json_encode($data2);
$final_data2 = json_decode($configdata2,true);
//dd($final_data2);
return view('clientlayout.main.test',array('final_data1' =>
$final_data1), array('final_data' => $final_data),
array('final_data2' => $final_data2));
}
Find the view code given below:
{{print_r($final_data)}}
<br><br><br>
{{print_r($final_data1)}}
<br>
{{print_r($final_data2)}}
Find the route code given below:
Route::get('/test','EnomController#domaincheck');
I need to return all the three arrays to the view page but When use the return view code giveb above I'm getting error as "Undefined variable: final_data2 " for the third array alone.If I declare only two array statements in the return view it works correctly. Suggest me a solution to solve this error.
in addition to #achraf answer if you dont want to use same variable names in your view as of your controller, you can pass data to your view like this. where final1, final2 and final3 will be the names of the variable in your view.
return view('clientlayout.main.test',['final1' =>$final_data ,'final2'=> $final_data1,'final3' => $final_data2]);
View take second param as array, your sending multiple arrays,
the solution is to create an array of arrays like this
return view('clientlayout.main.test',compact('final_data','final_data1','final_data2'));
I've working on User custom permission in Laravel 4, After login permissions(json string) stored in Auth::user()->permissions. as following:
$permissions =array(101,102);
DB::table('user')->where('id', Auth::user()->id)->update(['permissions' => json_encode($permissions)]);
But while I've checking permissions every time need to decode it to array:
if(in_array(1000, json_decode(Auth::user()->permissions)){
}
but I want something that make it to work like following:
if(in_array(1000, Auth::user()->usr_rights){
}
You can add a Accessor to your model :
public function getPermissionsAttribute($value)
{
return json_decode($value);
}
and all you have to do is this :
if(in_array(1000, Auth::user()->permissions){
}
You could use Implode
This isnt the nicest way of doing it but works.
Example:
$array = array('lastname', 'email', 'phone');
comma_separated = implode(",", $array);
echo $comma_separated; // lastname,email,phone
U could save this string in an additional row in user table.
and then convert it back with Explode
$pizza = "piece1, piece2, piece3, piece4, piece5, piece6";
$pieces = explode(",", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2
I have a form with input fields of type text and file. I have a problem of the filenames not saving in the database but everything else does. I vardumped $myForm and everything is there but the files, so I created another array with the filenames and merged it with $myForm. I then tried to set it to 'jform' but it doesn't seem to be working. Anyone have any ideas to why? Thanks!
controller.php
function save()
{
$jinput = JFactory::getApplication()->input;
$myForm = $jinput->get('jform', null, 'array');
//$files = $jinput->files->get('jform');
$file_array = ['image1' => 'test.png',
'image2' => 'test2.png'];
$merged_array = array_merge($myForm, $file_array);
$jinput->set('jform',$merged_array);
//or $jinput->post->set('jform',$merged_array); (this doesn't work either)
return parent::save();
}
Do not use $_POST unless you plan on writing extensive validation code to secure the user input.
Instead, use $jinput->get('jform', null, 'raw');
This will still apply some validation, but should keep your values in tact.