I'm having a problem to get my data from controller into my view, and I was hoping someone could help me.
Here is the code for my controller:
$Info = new Info();
$Info = DB::select('SELECT * FROM 'tblinfo');
and I only echo it in controller because my value results doesn't pass into the view, here's the additional code for my controller:
foreach ($Info as $Infos)
{
echo $Infos->fname;
echo $Infos->gender;
}
$data['result'] = $Infos->fname;
$data['result'] = $Infos->gender;
$this->view = View::make(App::make('web')->makeDeviceVersionPath('.').'.infofolder.infofile', $data);
Here is the code for my views:
#foreach $result as $info
{{ $info->name }}
{{ $info->gender }}
#endforeach
I hope someone could help me to pass my sql result to my views. Thank you! :)
if u have model u dont need to type raw sql code
just do like this
$info= Info::all();
return View('Index.index')->with('info',$info); // use ->with('*',$*) to send data to view
// Index is View folder
// .index is view name
Now in view grab $info data with for each
#foreach($info as $infodata)
<h3>{{ $infodata->name }}</h3>
<h3>{{ $infodata->gender}}</h3>
#endforeach
Adnan's answer is ok.
Op you should really check Eloquent and Response documentation from laravels website.
http://laravel.com/docs/4.2/eloquent
http://laravel.com/docs/4.2/responses
Related
Hi I connected to a third party api and trying to get the data looping through the object but I keep getting errors like the data is not there
Please see my controller and view below
Controller
function weather() {
$client = new Client();
$req = $client->get('https://api.darksky.net/forecast/6c314524bbb5e8ea74d27ea433a64a19/37.8267,-122.4233');
$response = $req->getBody()->getContents();
$data = json_decode($response, true);
return view('weather', compact('data'));
}
Blade View
#foreach($data as $r)
<span>{{ $r->latitude }}</span>
#endforeach
And this is the error
Trying to get property 'latitude' of non-object
What am I doing wrong please?
I mean the data is there, see pic
enter image description here
$data is an array, access the latitudeĀ as a key in your view
<span>{{ $data['latitude'] }}</span>
Hope this helps
I created a model relationship between User and Message. I want to implement a list of messages for the authenticated user but I get the following error.
Method Illuminate\Database\Eloquent\Collection::links does not exist
Controller
public function index()
{
$user_id = auth()->user()->id;
$user = User::find($user_id);
return view('message.index')->with('messages', $user->message);
}
Message provider
class message extends Model
{
public function user() {
return $this->belongsTo('App\User');
}
}
User provider
public function message ()
{
return $this->hasMany('App\Message');
}
index.blade.php
#extends('layouts.app')
#section('content')
<h1>messages</h1>
#if(count($messages)>0)
#foreach ($messages as $message)
<div class="well">
<h3>{{$message->user}}</h3>
<small>Written on {{$message->created_at}} </small>
</div>
#endforeach
{{$messages->links()}}
#else
<p> no post found </p>
#endif
#endsection
Error
"Method Illuminate\Database\Eloquent\Collection::links does not exist.(View: C:\xampp\htdocs\basicwebsite\resources\views\message\index.blade.php)"
Check your view blade, that method (links()) only could be used when your data model is implementing paginate() method.
If you dont use paginate(), remove this part:
{{$messages->links() }}
If you are trying to paginate your data when it gets to the view then you need to add the paginate in your controller before passing the data to the view. Example
return $users = Users::select('id','name')->paginate(10);
with that paginate method in your controller, you can call the links method to paginate your object in view as shown below
{{$users->links()}}
hope it helps you
There are 2 ways to resolve this issue:
Either use paginate function while searching data from database:
$users = DB::table('users')->where('id',$user_id)->paginate(1);
Remove links() function from index.blade.php
{{ $messages->links() }}
Remove {{ $messages->links() }} to in your index.blade.php because {{ $messages->links() }} is supported only when you use paginate
You can do something like this in your controller file.
public function index()
{
$messages = Message::all()->paginate(5);
$user_id = auth()->user()->id;
$user = User::find($user_id);
return view('message.index')->with('messages', $messages, $user->message);
}
I am successfully getting results in/from my controller like this:
$session_id and $user_id are getting passed into my method.
Controller
// get all sets belonging to the user for the given session
$session = Session::findOrFail($session_id);
$sets = Set::where('session_id', $session_id)->where('user_id', $user_id);
$user = User::findOrFail($user_id);
return View('users.index')
->with('session', $session)
->with('sets', $sets)
->with('user', $user);
View
<p>{{ $user->first_name }} has {{ $sets->count() }} existing set{{ ($sets->count() > 1 ? 's': '') }}.</p>
<ul class="list-unstyled">
#foreach ($sets as $set)
<li>
Set
</li>
#endforeach
</ul>
I am seeing "Foo has 1 existing set." and I can see the record in the DB. However, I'm not getting into the loop at all. If I add/remove records, my paragraph text updates accordingly as well - but I still never get into the loop to show each set. I'm sure it's something obvious, but I sure don't see it.
Thank you for any suggestions!
Change the query of sets to this:
$sets = Set::where('session_id', $session_id)->where('user_id', $user_id)->get();
As get() method will return you the collection object.
i'm encountered with an blade issue.
[{"banner_type":"2","content":"[\"e6aaaff3ae42af3af3a9cb6a45845da8f905c906Screenshotfrom2015-01-1917:17:31.png\",\"559a0e13473a22ac5bde041c5260257a12d1811eScreenshotfrom2015-01-2115:16:01.png\",\"559a0e13473a22ac5bde041c5260257a12d1811eScreenshotfrom2015-01-2208:49:53.png\",\"559a0e13473a22ac5bde041c5260257a12d1811eScreenshotfrom2015-01-2212:36:42.png\"]"}]
This is the json i'm getting from webservice, i need to get each image url and need to assign to a tag . how could i achive it?
#foreach($json->content as $img)
<img src="{{$img}}" alt="alt text">
#endforeach
From the information provided that is the most I can give you.
For the given json, you can try something like this on your controller:
public function index()
{
$json_string = '[{"banner_type":"2","content":"[\"e6aaaff3ae42af3af3a9cb6a45845da8f905c906Screenshotfrom2015-01-1917:17:31.png\",\"559a0e13473a22ac5bde041c5260257a12d1811eScreenshotfrom2015-01-2115:16:01.png\",\"559a0e13473a22ac5bde041c5260257a12d1811eScreenshotfrom2015-01-2208:49:53.png\",\"559a0e13473a22ac5bde041c5260257a12d1811eScreenshotfrom2015-01-2212:36:42.png\"]"}]';
$json = json_decode($json_string, true);
$images = json_decode($json[0]['content']);
return view('index', compact('images'));
}
Then, on your blade view:
#foreach($images as $image)
<img src="$image">
#endforeach
I have a problem with query on laravel.
Please show me how it work, because I can't understand doc.
For example, I have VideoController.php and I have some data from forms:
$gall = array(
'name' => Input::get('name'),
'user_id' => Auth::id()
);
now I want to add this data to DB, but I don't know how to call to create function in model (and how this function should look).
And please explain me, how I should select data from database and display it on view, where for example user_id = 15;
In your VideoController.php file you should have a method to store the data, which you should post your data to through a form or something.
In a view, this form should look something like this:
{{ Form::open(['route' => 'video.store']) }} <!-- check your routes to see if video.store exists, you'll get an error otherwise -->
... form elements ...
{{ Form::submit('Submit') }}
{{ Form::close() }}
In your store() function you should have code similar to this
$video = new Video(); // if your video model is Video.php
$video->name = Input::get('name');
$video->user_id = Auth::id();
$video->save();
And if you want to display data, i.e. your video index or something, in your VideoController.php file, in the index() function:
$videos = Video::all();
return View::make('video.index', array('videos' => $videos));
then in your view
#foreach($videos as $video)
{{ $video->name }}
#endforeach