my controller code
$posts = Categorie::where('parent_id',$id)->paginate(2);
model code :
public function post1(){
return $this->hasMany(post::class, 'category_id');
}
view page code :
#foreach ($posts as $cat)
#foreach ($cat->post1 as $post)
{{Debugbar::info($post->body)}}
<br>
#endforeach
#endforeach
and for pagination I used code :
{{ $posts->links() }}
all are working fine, data is fetched, I checked in the debugger. and pagination link also shown. I have 3 rows then fetched 2 rows on 1 page and other page shows another row. but problem is that it shows all data on page 1 when I clicked on pagination link page2 then shows a blank page.
how my pagination work properly
no method "links" you can try this
{{ $posts->render() }}
You are currently paginating categories, not posts.
Define the reverse relationship of post1 (if you haven't already):
class Post extends Model {
public function category() {
return $this->belongsTo(Categorie::class, 'category_id');
}
}
Then query the Post model and use whereHas():
$posts = Post::whereHas('category', function($query) use($id) {
$query->where('parent_id', $id);
})->paginate(2);
{{ $posts->links() }}
#foreach ($posts as $post)
{{ $post->body }}
#endforeach
I'm working on a support ticket tool.
Table design at the moment:
tickets: |id|supp_id|title|user_id|...
ticket_replies: |id|ticket_id|user_id|text
files: |id|ticket_replie_id|name
model of ticket
public function ticket_replie()
{
return $this->hasMany('App\ticket_replie', 'ticket_id', 'id');
}
model of ticket_replie
public function file()
{
return $this->hasOne('App\File', 'ticket_replie_id', 'id');
}
controller
$ticket = Auth::user()->tickets()->where('id', $id)->firstOrFail();
return view('protected.ticketDetail', compact('ticket'));
view
ID: {{$ticket->id}}
title: {{ $ticket->title}}<br>
status: {{ returnStatus($ticket->status) }}<br>
Ticket created: {{ $ticket->created_at }}<br>
#if (!$ticket->supporter)
supporter:-<br></br></br>
#else
supporter {{ $ticket->supporter->username }}<br></br>
#endif
#foreach($ticket->ticket_replie as $reply)
#if ($reply->file == null)
reply text: {{ $reply->text }}</br>
#else
reply text: {{ $reply->text }}</br>
file: Download file<br>
#endif
reply created at: {{$reply->created_at}}</br></br>
#endforeach
current screen of querys:
Each ticket_replie can contain exact one "file", which stands for an attached file. As you may see in the querys this generates much load. Is there an way to use (laravel Lazy Eager Loading) to minimize amount of querys?
Ordering the ticket_replies by:
$ticket = Auth::user()->tickets()->where('id', $id)->with(['ticket_replie.file'])->firstOrFail();
$ticket->sortBy('ticket_replie.created_at');
$ticket->values();
In the controller something like this should do the job...
$ticket = Auth::user()->tickets()->where('id', $id)->with(['ticket_replie.file'])->firstOrFail();
AdminsController
public function index(){
$someMessages=$this->blog->paginate(5);
$users=User::all();
// return 'Welcome Admin';
return View::make('admin.admin',['someMessages'=>$someMessages,'users'=>$users]);
}
admin.blade.php
Here need to display username from 'users' variable based on uid stored in 'someMessages' object.
#extends('layout.default')
#section('title')
<title>Welcome Admin</title>
#stop
#section('content')
#foreach($someMessages as $message)
<blockquote>{{$message['blog']}}
<small>
<cite>
</cite>
</small>
</blockquote>
#endforeach
{{ $someMessages->links()}}
#stop
If you have a Eloquent relation between blog messages and users defined correctly you don't need to query the users separately. What you want to do is just go with the eager loading:
//in case you call it author in your "relation definition"
$someMessages = $this->blog->with('author')->paginate(5);
And then in your Blade template:
#foreach($someMessages as $message)
<div>
<div>{{ $message->blog }}</div>
<div>{{ $message->author->name }}</div>
</div>
#endforeach
If you haven't declared the relationship yet - it's easy. Open up your user eloquent model class and add a method:
public function blogs()
{
return $this->hasMany('Blog', 'uid');
}
In the blog eloquent model class you'd add:
public function author()
{
return $this->belongsTo('User', 'uid');
}
More about eloquent and relationships can be found here.
If that's not the case you can still filter your users collection to get the one you want:
#foreach($someMessages as $message)
<?php
$user = $users->filter(function($user) use ($message)
{
return $user->id == $message->uid;
})->first();
?>
<div>
<div>{{ $message->blog }}</div>
<div>{{ $user->name }}</div>
</div>
#foreach
What happens here is you filter your users collection based on $message->uid value and take the first one from it (it should always be just one or none since user IDs are unique).
Although you need to understand that in this case you will get all the users from database and filter through them for every blog message you're outputting. Eager loading is a much better idea here and I'd stick to it if possible.
To pre-populate form field, we can add 'value' to form field in create.blade.php:
{{ Form::text('title', 'Some default title') }}
Is there a way to do that task elsewhere (maybe in model or controller?). I'd like to have code for form fields identical in create & edit view. Thanks!
Okay, so here we are... I used Laravel's form model binding in the example. (I work with User model/db table).
If this topic is not clear for you, take a look at this http://laravel.com/docs/html#form-model-binding
// Controller
class UsersController extends BaseController
{
...
// Method to show 'create' form & initialize 'blank' user's object
public function create()
{
$user = new User;
return View::make('users.form', compact('user'));
}
// This method should store data sent form form (for new user)
public function store()
{
print_r(Input::all());
}
// Retrieve user's data from DB by given ID & show 'edit' form
public function edit($id)
{
$user = User::find($id);
return View::make('users.form', compact('user'));
}
// Here you should process form data for user that exists already.
// Modify/convert some input data maybe, save it then...
public function update($id)
{
$user = User::find($id);
print_r($user->toArray());
}
...
}
And here come the view file served by controller.
// The view file - self describing I think
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
#if(!$user->id)
{{ Form::model($user, ['route' => 'admin.users.store']) }}
#else
{{ Form::model($user, ['route' => ['admin.users.update', $user->id], 'method' => 'put']) }}
#endif
{{ Form::text('firstName') }}
{{ Form::text('lastName') }}
{{ Form::submit() }}
{{ Form::close() }}
</body>
</html>
Yes, let's consider the following example:
View:
{{ Form::text('title', $title) }}
Controller:
$title = 'Some default title';
if($article) {
$title = $article->title;
}
return View::make('user.login')->with('title', $title)
Then you will have a text-input with either Some default title or the title from $article, if $article isn't equal to false
All you need to do is include a conditional in your blade template.
Lets assume your database table has a field myfield, which you want to default to mydefault.
Just include the following in a partial view which is called by the create and edit views:
#if(isset($myfield))
{{ Form::input('text','myfield') }}
#else
{{ Form::input('text','myfield','mydefault') }}
#endif
You don't have to anything else.
if you mean placeholder you can do this
{{ Form::password('password', array('placeholder' => 'password'))}}
Probably easier (Laravel 5):
{{ Form::text('title', isset($yourModel) ? null : 'Some default title') }}
That is to assume you are using the form as a partial. It should populate the value if the model for the form exists (you are editing or patching record) else it should show you the default you wish.
When you are using the Schema builder (in Migrations or somewhere else):
Schema::create(
'posts', function($table) {
$table->string('title', 30)->default('New post');
}
);
If you want to do this conditionally, an alternative method of solving this could be to perform a check instead. If this check passes, set the default (as is done below with $nom as an example). Otherwise, leave it empty by setting it to null explicitly.
{{ Form::text('title', (isset($nom)) ? $nom : null) }}
I built a simple cms in Laravel 4. At the moment the main site index shows a list of all posts including the body of the post.
I want to only show, for example, the first 100 letters of the body of my post as a preview before viewing the whole article on my show.blade.php page.
The relevant part of the index.blade looks like this:
#foreach($posts as $post)
...
<p>{{ $post->body }}</p>
And the relevant part of my DisplayController looks like this:
public function index()
{
$posts = $this->post->orderBy('id', 'DESC')->get();
return View::make('index', compact('posts'));
}
How would I display only the first 100 characters of each post on my index page (as an example)?
#foreach($posts as $post)
<p>{{ substr($post->body, 0, 100) }}</p>
#endforeach